mongodb-adapter.spec.js 97 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569
  1. import {expect} from 'chai';
  2. import {ObjectId} from 'mongodb';
  3. import {MongoClient} from 'mongodb';
  4. import {format} from '@e22m4u/js-format';
  5. import {Schema} from '@e22m4u/js-repository';
  6. import {DataType} from '@e22m4u/js-repository';
  7. import {createMongodbUrl} from './utils/index.js';
  8. import {MongodbAdapter} from './mongodb-adapter.js';
  9. import {AdapterRegistry} from '@e22m4u/js-repository';
  10. import {DEFAULT_PRIMARY_KEY_PROPERTY_NAME as DEF_PK} from '@e22m4u/js-repository';
  11. const CONFIG = {
  12. host: process.env.MONGODB_HOST || 'localhost',
  13. port: process.env.MONGODB_PORT || 27017,
  14. database: process.env.MONGODB_DATABASE,
  15. };
  16. const MDB_CLIENT = new MongoClient(createMongodbUrl(CONFIG));
  17. const ADAPTERS_STACK = [];
  18. function createSchema() {
  19. const schema = new Schema();
  20. const adapter = new MongodbAdapter(schema.container, CONFIG);
  21. ADAPTERS_STACK.push(adapter);
  22. schema.defineDatasource({name: 'mongodb', adapter: 'mongodb'});
  23. schema.getService(AdapterRegistry)._adapters['mongodb'] = adapter;
  24. return schema;
  25. }
  26. describe('MongodbAdapter', function () {
  27. this.timeout(15000);
  28. afterEach(async function () {
  29. await MDB_CLIENT.db(CONFIG.database).dropDatabase();
  30. });
  31. after(async function () {
  32. for await (const adapter of ADAPTERS_STACK) {
  33. await adapter.client.close(true);
  34. }
  35. await MDB_CLIENT.close(true);
  36. });
  37. describe('_buildProjection', function () {
  38. describe('single field', function () {
  39. it('returns undefined if the second argument is undefined', async function () {
  40. const schema = createSchema();
  41. schema.defineModel({name: 'model', datasource: 'mongodb'});
  42. const A = await schema
  43. .getService(AdapterRegistry)
  44. .getAdapter('mongodb');
  45. const res = A._buildProjection('model', undefined);
  46. expect(res).to.be.undefined;
  47. });
  48. it('returns undefined if the second argument is null', async function () {
  49. const schema = createSchema();
  50. schema.defineModel({name: 'model', datasource: 'mongodb'});
  51. const A = await schema
  52. .getService(AdapterRegistry)
  53. .getAdapter('mongodb');
  54. const res = A._buildProjection('model', null);
  55. expect(res).to.be.undefined;
  56. });
  57. it('requires the second argument to be a non-empty string', async function () {
  58. const schema = createSchema();
  59. schema.defineModel({name: 'model', datasource: 'mongodb'});
  60. const A = await schema
  61. .getService(AdapterRegistry)
  62. .getAdapter('mongodb');
  63. const throwable = v => () => A._buildProjection('model', v);
  64. const error = v =>
  65. format(
  66. 'The provided option "fields" should be a non-empty String ' +
  67. 'or an Array of non-empty String, but %s given.',
  68. v,
  69. );
  70. expect(throwable('')).to.throw(error('""'));
  71. expect(throwable(10)).to.throw(error('10'));
  72. expect(throwable(0)).to.throw(error('0'));
  73. expect(throwable(true)).to.throw(error('true'));
  74. expect(throwable(false)).to.throw(error('false'));
  75. expect(throwable({})).to.throw(error('Object'));
  76. expect(throwable('bar')()).to.be.eql({_id: 1, bar: 1});
  77. expect(throwable(undefined)()).to.be.undefined;
  78. expect(throwable(null)()).to.be.undefined;
  79. });
  80. it('converts the given property name to the column name', async function () {
  81. const schema = createSchema();
  82. schema.defineModel({
  83. name: 'model',
  84. datasource: 'mongodb',
  85. properties: {
  86. foo: {
  87. type: DataType.STRING,
  88. columnName: 'bar',
  89. },
  90. },
  91. });
  92. const A = await schema
  93. .getService(AdapterRegistry)
  94. .getAdapter('mongodb');
  95. const res = A._buildProjection('model', 'foo');
  96. expect(res).to.be.eql({_id: 1, bar: 1});
  97. });
  98. it('includes "_id" field to the projection', async function () {
  99. const schema = createSchema();
  100. schema.defineModel({name: 'model', datasource: 'mongodb'});
  101. const A = await schema
  102. .getService(AdapterRegistry)
  103. .getAdapter('mongodb');
  104. const res = A._buildProjection('model', 'foo');
  105. expect(res).to.be.eql({_id: 1, foo: 1});
  106. });
  107. it('includes "_id" as a column name of the given property', async function () {
  108. const schema = createSchema();
  109. schema.defineModel({
  110. name: 'model',
  111. datasource: 'mongodb',
  112. properties: {
  113. foo: {
  114. type: DataType.STRING,
  115. primaryKey: true,
  116. columnName: '_id',
  117. },
  118. },
  119. });
  120. const A = await schema
  121. .getService(AdapterRegistry)
  122. .getAdapter('mongodb');
  123. const res = A._buildProjection('model', 'foo');
  124. expect(res).to.be.eql({_id: 1});
  125. });
  126. });
  127. describe('multiple fields', function () {
  128. it('returns undefined if the second argument is an empty array', async function () {
  129. const schema = createSchema();
  130. schema.defineModel({name: 'model', datasource: 'mongodb'});
  131. const A = await schema
  132. .getService(AdapterRegistry)
  133. .getAdapter('mongodb');
  134. const res = A._buildProjection('model', []);
  135. expect(res).to.be.undefined;
  136. });
  137. it('requires the second argument to be an array of non-empty strings', async function () {
  138. const schema = createSchema();
  139. schema.defineModel({name: 'model', datasource: 'mongodb'});
  140. const A = await schema
  141. .getService(AdapterRegistry)
  142. .getAdapter('mongodb');
  143. const throwable = v => () => A._buildProjection('model', v);
  144. const error = v =>
  145. format(
  146. 'The provided option "fields" should be a non-empty String ' +
  147. 'or an Array of non-empty String, but %s given.',
  148. v,
  149. );
  150. expect(throwable([''])).to.throw(error('""'));
  151. expect(throwable([10])).to.throw(error('10'));
  152. expect(throwable([0])).to.throw(error('0'));
  153. expect(throwable([true])).to.throw(error('true'));
  154. expect(throwable([false])).to.throw(error('false'));
  155. expect(throwable([{}])).to.throw(error('Object'));
  156. expect(throwable([undefined])).to.throw(error('undefined'));
  157. expect(throwable([null])).to.throw(error('null'));
  158. expect(throwable([])()).to.be.undefined;
  159. expect(throwable(['bar'])()).to.be.eql({_id: 1, bar: 1});
  160. });
  161. it('converts the given property names to column names', async function () {
  162. const schema = createSchema();
  163. schema.defineModel({
  164. name: 'model',
  165. datasource: 'mongodb',
  166. properties: {
  167. foo: {
  168. type: DataType.STRING,
  169. columnName: 'bar',
  170. },
  171. baz: {
  172. type: DataType.STRING,
  173. columnName: 'qux',
  174. },
  175. },
  176. });
  177. const A = await schema
  178. .getService(AdapterRegistry)
  179. .getAdapter('mongodb');
  180. const res = A._buildProjection('model', ['foo', 'baz']);
  181. expect(res).to.be.eql({_id: 1, bar: 1, qux: 1});
  182. });
  183. it('includes "_id" field to the projection', async function () {
  184. const schema = createSchema();
  185. schema.defineModel({name: 'model', datasource: 'mongodb'});
  186. const A = await schema
  187. .getService(AdapterRegistry)
  188. .getAdapter('mongodb');
  189. const res = A._buildProjection('model', ['foo', 'bar']);
  190. expect(res).to.be.eql({_id: 1, foo: 1, bar: 1});
  191. });
  192. it('includes "_id" as a column name of the given property', async function () {
  193. const schema = createSchema();
  194. schema.defineModel({
  195. name: 'model',
  196. datasource: 'mongodb',
  197. properties: {
  198. foo: {
  199. type: DataType.STRING,
  200. primaryKey: true,
  201. columnName: '_id',
  202. },
  203. },
  204. });
  205. const A = await schema
  206. .getService(AdapterRegistry)
  207. .getAdapter('mongodb');
  208. const res = A._buildProjection('model', ['foo', 'bar']);
  209. expect(res).to.be.eql({_id: 1, bar: 1});
  210. });
  211. });
  212. });
  213. describe('create', function () {
  214. it('generates a new identifier when a value of a primary key is not provided', async function () {
  215. const schema = createSchema();
  216. schema.defineModel({name: 'model', datasource: 'mongodb'});
  217. const rep = schema.getRepository('model');
  218. const result = await rep.create({foo: 'bar'});
  219. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 'bar'});
  220. expect(typeof result[DEF_PK]).to.be.eq('string');
  221. expect(result[DEF_PK]).to.have.lengthOf(24);
  222. });
  223. it('generates a new identifier when a value of a primary key is undefined', async function () {
  224. const schema = createSchema();
  225. schema.defineModel({name: 'model', datasource: 'mongodb'});
  226. const rep = schema.getRepository('model');
  227. const result = await rep.create({[DEF_PK]: undefined, foo: 'bar'});
  228. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 'bar'});
  229. expect(typeof result[DEF_PK]).to.be.eq('string');
  230. expect(result[DEF_PK]).to.have.lengthOf(24);
  231. });
  232. it('generates a new identifier when a value of a primary key is null', async function () {
  233. const schema = createSchema();
  234. schema.defineModel({name: 'model', datasource: 'mongodb'});
  235. const rep = schema.getRepository('model');
  236. const result = await rep.create({[DEF_PK]: null, foo: 'bar'});
  237. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 'bar'});
  238. expect(typeof result[DEF_PK]).to.be.eq('string');
  239. expect(result[DEF_PK]).to.have.lengthOf(24);
  240. });
  241. it('generates a new identifier for a primary key of a "string" type', async function () {
  242. const schema = createSchema();
  243. schema.defineModel({
  244. name: 'model',
  245. datasource: 'mongodb',
  246. properties: {
  247. id: {
  248. type: DataType.STRING,
  249. primaryKey: true,
  250. },
  251. },
  252. });
  253. const rep = schema.getRepository('model');
  254. const result = await rep.create({[DEF_PK]: null, foo: 'bar'});
  255. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 'bar'});
  256. expect(typeof result[DEF_PK]).to.be.eq('string');
  257. expect(result[DEF_PK]).to.have.lengthOf(24);
  258. });
  259. it('generates a new identifier for a primary key of a "any" type', async function () {
  260. const schema = createSchema();
  261. schema.defineModel({
  262. name: 'model',
  263. datasource: 'mongodb',
  264. properties: {
  265. id: {
  266. type: DataType.ANY,
  267. primaryKey: true,
  268. },
  269. },
  270. });
  271. const rep = schema.getRepository('model');
  272. const result = await rep.create({[DEF_PK]: null, foo: 'bar'});
  273. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 'bar'});
  274. expect(typeof result[DEF_PK]).to.be.eq('string');
  275. expect(result[DEF_PK]).to.have.lengthOf(24);
  276. });
  277. it('throws an error when generating a new value for a primary key of a "number" type', async function () {
  278. const schema = createSchema();
  279. schema.defineModel({
  280. name: 'model',
  281. datasource: 'mongodb',
  282. properties: {
  283. id: {
  284. type: DataType.NUMBER,
  285. primaryKey: true,
  286. },
  287. },
  288. });
  289. const rep = schema.getRepository('model');
  290. const promise = rep.create({});
  291. expect(promise).to.be.rejectedWith(
  292. 'MongoDB unable to generate primary keys of Number. ' +
  293. 'Do provide your own value for the "id" property ' +
  294. 'or set the property type to String.',
  295. );
  296. });
  297. it('throws an error when generating a new value for a primary key of a "boolean" type', async function () {
  298. const schema = createSchema();
  299. schema.defineModel({
  300. name: 'model',
  301. datasource: 'mongodb',
  302. properties: {
  303. id: {
  304. type: DataType.BOOLEAN,
  305. primaryKey: true,
  306. },
  307. },
  308. });
  309. const rep = schema.getRepository('model');
  310. const promise = rep.create({});
  311. expect(promise).to.be.rejectedWith(
  312. 'MongoDB unable to generate primary keys of Boolean. ' +
  313. 'Do provide your own value for the "id" property ' +
  314. 'or set the property type to String.',
  315. );
  316. });
  317. it('throws an error when generating a new value for a primary key of a "array" type', async function () {
  318. const schema = createSchema();
  319. schema.defineModel({
  320. name: 'model',
  321. datasource: 'mongodb',
  322. properties: {
  323. id: {
  324. type: DataType.ARRAY,
  325. primaryKey: true,
  326. },
  327. },
  328. });
  329. const rep = schema.getRepository('model');
  330. const promise = rep.create({});
  331. expect(promise).to.be.rejectedWith(
  332. 'MongoDB unable to generate primary keys of Array. ' +
  333. 'Do provide your own value for the "id" property ' +
  334. 'or set the property type to String.',
  335. );
  336. });
  337. it('throws an error when generating a new value for a primary key of a "object" type', async function () {
  338. const schema = createSchema();
  339. schema.defineModel({
  340. name: 'model',
  341. datasource: 'mongodb',
  342. properties: {
  343. id: {
  344. type: DataType.OBJECT,
  345. primaryKey: true,
  346. },
  347. },
  348. });
  349. const rep = schema.getRepository('model');
  350. const promise = rep.create({});
  351. expect(promise).to.be.rejectedWith(
  352. 'MongoDB unable to generate primary keys of Object. ' +
  353. 'Do provide your own value for the "id" property ' +
  354. 'or set the property type to String.',
  355. );
  356. });
  357. it('allows to specify an ObjectID instance for a default primary key', async function () {
  358. const schema = createSchema();
  359. schema.defineModel({name: 'model', datasource: 'mongodb'});
  360. const rep = schema.getRepository('model');
  361. const oid = new ObjectId();
  362. const result = await rep.create({[DEF_PK]: oid});
  363. expect(result).to.be.eql({[DEF_PK]: String(oid)});
  364. const rawData = await MDB_CLIENT.db()
  365. .collection('model')
  366. .findOne({_id: oid});
  367. expect(rawData).to.be.not.null;
  368. });
  369. it('allows to specify an ObjectID string for a default primary key', async function () {
  370. const schema = createSchema();
  371. schema.defineModel({name: 'model', datasource: 'mongodb'});
  372. const rep = schema.getRepository('model');
  373. const oid = new ObjectId();
  374. const id = String(oid);
  375. const result = await rep.create({[DEF_PK]: id});
  376. expect(result).to.be.eql({[DEF_PK]: id});
  377. const rawData = await MDB_CLIENT.db()
  378. .collection('model')
  379. .findOne({_id: oid});
  380. expect(rawData).to.be.not.null;
  381. });
  382. it('allows to specify an ObjectID instance for "id" primary key', async function () {
  383. const schema = createSchema();
  384. schema.defineModel({
  385. name: 'model',
  386. datasource: 'mongodb',
  387. properties: {
  388. id: {
  389. type: DataType.ANY,
  390. primaryKey: true,
  391. },
  392. },
  393. });
  394. const rep = schema.getRepository('model');
  395. const oid = new ObjectId();
  396. const result = await rep.create({id: oid});
  397. expect(result).to.be.eql({id: String(oid)});
  398. const rawData = await MDB_CLIENT.db()
  399. .collection('model')
  400. .findOne({_id: oid});
  401. expect(rawData).to.be.not.null;
  402. });
  403. it('allows to specify an ObjectID string for "id" primary key', async function () {
  404. const schema = createSchema();
  405. schema.defineModel({
  406. name: 'model',
  407. datasource: 'mongodb',
  408. properties: {
  409. id: {
  410. type: DataType.STRING,
  411. primaryKey: true,
  412. },
  413. },
  414. });
  415. const rep = schema.getRepository('model');
  416. const oid = new ObjectId();
  417. const id = String(oid);
  418. const result = await rep.create({id});
  419. expect(result).to.be.eql({id});
  420. const rawData = await MDB_CLIENT.db()
  421. .collection('model')
  422. .findOne({_id: oid});
  423. expect(rawData).to.be.not.null;
  424. });
  425. it('allows to specify an ObjectID instance for "_id" primary key', async function () {
  426. const schema = createSchema();
  427. schema.defineModel({
  428. name: 'model',
  429. datasource: 'mongodb',
  430. properties: {
  431. _id: {
  432. type: DataType.ANY,
  433. primaryKey: true,
  434. },
  435. },
  436. });
  437. const rep = schema.getRepository('model');
  438. const oid = new ObjectId();
  439. const result = await rep.create({_id: oid});
  440. expect(result).to.be.eql({_id: String(oid)});
  441. const rawData = await MDB_CLIENT.db()
  442. .collection('model')
  443. .findOne({_id: oid});
  444. expect(rawData).to.be.not.null;
  445. });
  446. it('allows to specify an ObjectID string for "_id" primary key', async function () {
  447. const schema = createSchema();
  448. schema.defineModel({
  449. name: 'model',
  450. datasource: 'mongodb',
  451. properties: {
  452. _id: {
  453. type: DataType.STRING,
  454. primaryKey: true,
  455. },
  456. },
  457. });
  458. const rep = schema.getRepository('model');
  459. const oid = new ObjectId();
  460. const id = String(oid);
  461. const result = await rep.create({_id: id});
  462. expect(result).to.be.eql({_id: id});
  463. const rawData = await MDB_CLIENT.db()
  464. .collection('model')
  465. .findOne({_id: oid});
  466. expect(rawData).to.be.not.null;
  467. });
  468. it('throws an error for a custom primary key', async function () {
  469. const schema = createSchema();
  470. schema.defineModel({
  471. name: 'model',
  472. datasource: 'mongodb',
  473. properties: {
  474. myId: {
  475. type: DataType.ANY,
  476. primaryKey: true,
  477. },
  478. },
  479. });
  480. const rep = schema.getRepository('model');
  481. const oid = new ObjectId();
  482. const promise = rep.create({myId: oid});
  483. await expect(promise).to.be.rejectedWith(
  484. 'MongoDB is not supporting custom names of the primary key. ' +
  485. 'Do use "id" as a primary key instead of "myId".',
  486. );
  487. });
  488. it('throws an error if a given "number" identifier already exists', async function () {
  489. const schema = createSchema();
  490. schema.defineModel({name: 'model', datasource: 'mongodb'});
  491. const rep = schema.getRepository('model');
  492. await rep.create({[DEF_PK]: 10});
  493. const promise = rep.create({[DEF_PK]: 10});
  494. await expect(promise).to.be.rejectedWith(
  495. 'E11000 duplicate key error collection: test.model index: ' +
  496. '_id_ dup key: { _id: 10 }',
  497. );
  498. });
  499. it('throws an error if a given "string" identifier already exists', async function () {
  500. const schema = createSchema();
  501. schema.defineModel({name: 'model', datasource: 'mongodb'});
  502. const rep = schema.getRepository('model');
  503. await rep.create({[DEF_PK]: 'str'});
  504. const promise = rep.create({[DEF_PK]: 'str'});
  505. await expect(promise).to.be.rejectedWith(
  506. 'E11000 duplicate key error collection: test.model index: ' +
  507. '_id_ dup key: { _id: "str" }',
  508. );
  509. });
  510. it('throws an error if a given ObjectId instance identifier already exists', async function () {
  511. const schema = createSchema();
  512. schema.defineModel({name: 'model', datasource: 'mongodb'});
  513. const rep = schema.getRepository('model');
  514. const oid = new ObjectId();
  515. await rep.create({[DEF_PK]: oid});
  516. const promise = rep.create({[DEF_PK]: oid});
  517. await expect(promise).to.be.rejectedWith(
  518. format(
  519. 'E11000 duplicate key error collection: test.model index: ' +
  520. "_id_ dup key: { _id: ObjectId('%s') }",
  521. oid,
  522. ),
  523. );
  524. });
  525. it('throws an error if a given ObjectId string identifier already exists', async function () {
  526. const schema = createSchema();
  527. schema.defineModel({name: 'model', datasource: 'mongodb'});
  528. const rep = schema.getRepository('model');
  529. const oid = new ObjectId();
  530. const id = String(oid);
  531. await rep.create({[DEF_PK]: id});
  532. const promise = rep.create({[DEF_PK]: id});
  533. await expect(promise).to.be.rejectedWith(
  534. format(
  535. 'E11000 duplicate key error collection: test.model index: ' +
  536. "_id_ dup key: { _id: ObjectId('%s') }",
  537. id,
  538. ),
  539. );
  540. });
  541. it('uses a specified column name for a regular property', async function () {
  542. const schema = createSchema();
  543. schema.defineModel({
  544. name: 'model',
  545. datasource: 'mongodb',
  546. properties: {
  547. foo: {
  548. type: DataType.NUMBER,
  549. columnName: 'bar',
  550. },
  551. },
  552. });
  553. const rep = schema.getRepository('model');
  554. const result = await rep.create({foo: 10});
  555. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 10});
  556. const oid = new ObjectId(result[DEF_PK]);
  557. const rawData = await MDB_CLIENT.db()
  558. .collection('model')
  559. .findOne({_id: oid});
  560. expect(rawData).to.be.eql({_id: oid, bar: 10});
  561. });
  562. it('uses a specified column name for a regular property with a default value', async function () {
  563. const schema = createSchema();
  564. schema.defineModel({
  565. name: 'model',
  566. datasource: 'mongodb',
  567. properties: {
  568. foo: {
  569. type: DataType.NUMBER,
  570. columnName: 'bar',
  571. default: 10,
  572. },
  573. },
  574. });
  575. const rep = schema.getRepository('model');
  576. const result = await rep.create({});
  577. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 10});
  578. const oid = new ObjectId(result[DEF_PK]);
  579. const rawData = await MDB_CLIENT.db()
  580. .collection('model')
  581. .findOne({_id: oid});
  582. expect(rawData).to.be.eql({_id: oid, bar: 10});
  583. });
  584. it('stores a Date instance as date and returns string type', async function () {
  585. const schema = createSchema();
  586. schema.defineModel({name: 'model', datasource: 'mongodb'});
  587. const rep = schema.getRepository('model');
  588. const date = new Date();
  589. const dateString = date.toISOString();
  590. const result = await rep.create({date});
  591. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], date: dateString});
  592. const oid = new ObjectId(result[DEF_PK]);
  593. const rawData = await MDB_CLIENT.db()
  594. .collection('model')
  595. .findOne({_id: oid});
  596. expect(rawData).to.be.eql({_id: oid, date});
  597. });
  598. it('stores a Date string as date and returns string type', async function () {
  599. const schema = createSchema();
  600. schema.defineModel({name: 'model', datasource: 'mongodb'});
  601. const rep = schema.getRepository('model');
  602. const date = new Date();
  603. const dateString = date.toISOString();
  604. const result = await rep.create({date: dateString});
  605. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], date: dateString});
  606. const oid = new ObjectId(result[DEF_PK]);
  607. const rawData = await MDB_CLIENT.db()
  608. .collection('model')
  609. .findOne({_id: oid});
  610. expect(rawData).to.be.eql({_id: oid, date});
  611. });
  612. it('stores a string as is', async function () {
  613. const schema = createSchema();
  614. schema.defineModel({name: 'model', datasource: 'mongodb'});
  615. const rep = schema.getRepository('model');
  616. const result = await rep.create({foo: 'str'});
  617. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 'str'});
  618. const oid = new ObjectId(result[DEF_PK]);
  619. const rawData = await MDB_CLIENT.db()
  620. .collection('model')
  621. .findOne({_id: oid});
  622. expect(rawData).to.be.eql({_id: oid, foo: 'str'});
  623. });
  624. it('stores a number as is', async function () {
  625. const schema = createSchema();
  626. schema.defineModel({name: 'model', datasource: 'mongodb'});
  627. const rep = schema.getRepository('model');
  628. const result = await rep.create({foo: 10});
  629. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 10});
  630. const oid = new ObjectId(result[DEF_PK]);
  631. const rawData = await MDB_CLIENT.db()
  632. .collection('model')
  633. .findOne({_id: oid});
  634. expect(rawData).to.be.eql({_id: oid, foo: 10});
  635. });
  636. it('stores a boolean as is', async function () {
  637. const schema = createSchema();
  638. schema.defineModel({name: 'model', datasource: 'mongodb'});
  639. const rep = schema.getRepository('model');
  640. const result = await rep.create({foo: true, bar: false});
  641. expect(result).to.be.eql({
  642. [DEF_PK]: result[DEF_PK],
  643. foo: true,
  644. bar: false,
  645. });
  646. const oid = new ObjectId(result[DEF_PK]);
  647. const rawData = await MDB_CLIENT.db()
  648. .collection('model')
  649. .findOne({_id: oid});
  650. expect(rawData).to.be.eql({_id: oid, foo: true, bar: false});
  651. });
  652. it('stores an array as is', async function () {
  653. const schema = createSchema();
  654. schema.defineModel({name: 'model', datasource: 'mongodb'});
  655. const rep = schema.getRepository('model');
  656. const result = await rep.create({foo: ['bar']});
  657. expect(result).to.be.eql({
  658. [DEF_PK]: result[DEF_PK],
  659. foo: ['bar'],
  660. });
  661. const oid = new ObjectId(result[DEF_PK]);
  662. const rawData = await MDB_CLIENT.db()
  663. .collection('model')
  664. .findOne({_id: oid});
  665. expect(rawData).to.be.eql({_id: oid, foo: ['bar']});
  666. });
  667. it('stores an object as is', async function () {
  668. const schema = createSchema();
  669. schema.defineModel({name: 'model', datasource: 'mongodb'});
  670. const rep = schema.getRepository('model');
  671. const result = await rep.create({foo: {bar: 10}});
  672. expect(result).to.be.eql({
  673. [DEF_PK]: result[DEF_PK],
  674. foo: {bar: 10},
  675. });
  676. const oid = new ObjectId(result[DEF_PK]);
  677. const rawData = await MDB_CLIENT.db()
  678. .collection('model')
  679. .findOne({_id: oid});
  680. expect(rawData).to.be.eql({_id: oid, foo: {bar: 10}});
  681. });
  682. it('stores an undefined as null', async function () {
  683. const schema = createSchema();
  684. schema.defineModel({name: 'model', datasource: 'mongodb'});
  685. const rep = schema.getRepository('model');
  686. const result = await rep.create({foo: undefined});
  687. expect(result).to.be.eql({
  688. [DEF_PK]: result[DEF_PK],
  689. foo: null,
  690. });
  691. const oid = new ObjectId(result[DEF_PK]);
  692. const rawData = await MDB_CLIENT.db()
  693. .collection('model')
  694. .findOne({_id: oid});
  695. expect(rawData).to.be.eql({_id: oid, foo: null});
  696. });
  697. it('stores an null as is', async function () {
  698. const schema = createSchema();
  699. schema.defineModel({name: 'model', datasource: 'mongodb'});
  700. const rep = schema.getRepository('model');
  701. const result = await rep.create({foo: null});
  702. expect(result).to.be.eql({
  703. [DEF_PK]: result[DEF_PK],
  704. foo: null,
  705. });
  706. const oid = new ObjectId(result[DEF_PK]);
  707. const rawData = await MDB_CLIENT.db()
  708. .collection('model')
  709. .findOne({_id: oid});
  710. expect(rawData).to.be.eql({_id: oid, foo: null});
  711. });
  712. it('uses a short fields clause to filter results', async function () {
  713. const schema = createSchema();
  714. schema.defineModel({name: 'model', datasource: 'mongodb'});
  715. const rep = schema.getRepository('model');
  716. const result = await rep.create({foo: 10, bar: 20}, {fields: 'foo'});
  717. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 10});
  718. });
  719. it('uses a full fields clause to filter results', async function () {
  720. const schema = createSchema();
  721. schema.defineModel({name: 'model', datasource: 'mongodb'});
  722. const rep = schema.getRepository('model');
  723. const result = await rep.create(
  724. {foo: 10, bar: 20, baz: 30},
  725. {fields: ['foo', 'bar']},
  726. );
  727. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK], foo: 10, bar: 20});
  728. const oid = new ObjectId(result[DEF_PK]);
  729. const rawData = await MDB_CLIENT.db()
  730. .collection('model')
  731. .findOne({_id: oid});
  732. expect(rawData).to.be.eql({_id: oid, foo: 10, bar: 20, baz: 30});
  733. });
  734. it('a fields clause uses property names instead of column names', async function () {
  735. const schema = createSchema();
  736. schema.defineModel({
  737. name: 'model',
  738. datasource: 'mongodb',
  739. properties: {
  740. foo: {
  741. type: DataType.NUMBER,
  742. columnName: 'fooCol',
  743. },
  744. bar: {
  745. type: DataType.NUMBER,
  746. columnName: 'barCol',
  747. },
  748. baz: {
  749. type: DataType.NUMBER,
  750. columnName: 'bazCol',
  751. },
  752. },
  753. });
  754. const rep = schema.getRepository('model');
  755. const result = await rep.create(
  756. {foo: 10, bar: 20, baz: 30},
  757. {fields: ['fooCol', 'barCol']},
  758. );
  759. expect(result).to.be.eql({[DEF_PK]: result[DEF_PK]});
  760. });
  761. });
  762. describe('replaceById', function () {
  763. it('removes properties when replacing an item by a given identifier', async function () {
  764. const schema = createSchema();
  765. schema.defineModel({name: 'model', datasource: 'mongodb'});
  766. const rep = schema.getRepository('model');
  767. const created = await rep.create({foo: 10});
  768. const id = created[DEF_PK];
  769. const replaced = await rep.replaceById(id, {bar: 20});
  770. expect(replaced).to.be.eql({[DEF_PK]: id, bar: 20});
  771. const oid = new ObjectId(id);
  772. const rawData = await MDB_CLIENT.db()
  773. .collection('model')
  774. .findOne({_id: oid});
  775. expect(rawData).to.be.eql({_id: oid, bar: 20});
  776. });
  777. it('ignores identifier value in a given data in case of a default primary key', async function () {
  778. const schema = createSchema();
  779. schema.defineModel({name: 'model', datasource: 'mongodb'});
  780. const rep = schema.getRepository('model');
  781. await rep.create({[DEF_PK]: 'foo', prop: 10});
  782. const replaced = await rep.replaceById('foo', {
  783. [DEF_PK]: 'bar',
  784. prop: 20,
  785. });
  786. expect(replaced).to.be.eql({[DEF_PK]: 'foo', prop: 20});
  787. const rawData = await MDB_CLIENT.db()
  788. .collection('model')
  789. .findOne({_id: 'foo'});
  790. expect(rawData).to.be.eql({_id: 'foo', prop: 20});
  791. });
  792. it('ignores identifier value in a given data in case of a custom primary key', async function () {
  793. const schema = createSchema();
  794. schema.defineModel({
  795. name: 'model',
  796. datasource: 'mongodb',
  797. properties: {
  798. myId: {
  799. type: DataType.STRING,
  800. primaryKey: true,
  801. columnName: '_id',
  802. },
  803. },
  804. });
  805. const rep = schema.getRepository('model');
  806. await rep.create({myId: 'foo', prop: 10});
  807. const replaced = await rep.replaceById('foo', {myId: 'bar', prop: 20});
  808. expect(replaced).to.be.eql({myId: 'foo', prop: 20});
  809. const rawData = await MDB_CLIENT.db()
  810. .collection('model')
  811. .findOne({_id: 'foo'});
  812. expect(rawData).to.be.eql({_id: 'foo', prop: 20});
  813. });
  814. it('throws an error if a given identifier does not exist', async function () {
  815. const schema = createSchema();
  816. schema.defineModel({name: 'model', datasource: 'mongodb'});
  817. const rep = schema.getRepository('model');
  818. const oid = new ObjectId();
  819. const promise = rep.replaceById(oid, {foo: 10});
  820. await expect(promise).to.be.rejectedWith(
  821. format('Identifier "%s" is not found.', oid),
  822. );
  823. });
  824. it('throws an error for a custom primary key', async function () {
  825. const schema = createSchema();
  826. schema.defineModel({
  827. name: 'model',
  828. datasource: 'mongodb',
  829. properties: {
  830. myId: {
  831. type: DataType.ANY,
  832. primaryKey: true,
  833. },
  834. },
  835. });
  836. const rep = schema.getRepository('model');
  837. const promise = rep.replaceById('id', {foo: 10});
  838. await expect(promise).to.be.rejectedWith(
  839. 'MongoDB is not supporting custom names of the primary key. ' +
  840. 'Do use "id" as a primary key instead of "myId".',
  841. );
  842. });
  843. it('uses a specified column name for a regular property', async function () {
  844. const schema = createSchema();
  845. schema.defineModel({
  846. name: 'model',
  847. datasource: 'mongodb',
  848. properties: {
  849. foo: {
  850. type: DataType.NUMBER,
  851. columnName: 'bar',
  852. },
  853. },
  854. });
  855. const rep = schema.getRepository('model');
  856. const created = await rep.create({foo: 10});
  857. const id = created[DEF_PK];
  858. const replaced = await rep.replaceById(id, {foo: 20});
  859. expect(replaced).to.be.eql({[DEF_PK]: id, foo: 20});
  860. const oid = new ObjectId(id);
  861. const rawData = await MDB_CLIENT.db()
  862. .collection('model')
  863. .findOne({_id: oid});
  864. expect(rawData).to.be.eql({_id: oid, bar: 20});
  865. });
  866. it('stores a Date instance as date and returns string type', async function () {
  867. const schema = createSchema();
  868. schema.defineModel({name: 'model', datasource: 'mongodb'});
  869. const rep = schema.getRepository('model');
  870. const date = new Date();
  871. const dateString = date.toISOString();
  872. const created = await rep.create({date: null});
  873. const id = created[DEF_PK];
  874. const replaced = await rep.replaceById(id, {date});
  875. expect(replaced).to.be.eql({[DEF_PK]: id, date: dateString});
  876. const oid = new ObjectId(id);
  877. const rawData = await MDB_CLIENT.db()
  878. .collection('model')
  879. .findOne({_id: oid});
  880. expect(rawData).to.be.eql({_id: oid, date});
  881. });
  882. it('stores a Date string as date and returns string type', async function () {
  883. const schema = createSchema();
  884. schema.defineModel({name: 'model', datasource: 'mongodb'});
  885. const rep = schema.getRepository('model');
  886. const date = new Date();
  887. const dateString = date.toISOString();
  888. const created = await rep.create({date: null});
  889. const id = created[DEF_PK];
  890. const replaced = await rep.replaceById(id, {date: dateString});
  891. expect(replaced).to.be.eql({[DEF_PK]: id, date: dateString});
  892. const oid = new ObjectId(id);
  893. const rawData = await MDB_CLIENT.db()
  894. .collection('model')
  895. .findOne({_id: oid});
  896. expect(rawData).to.be.eql({_id: oid, date});
  897. });
  898. it('stores a string as is', async function () {
  899. const schema = createSchema();
  900. schema.defineModel({name: 'model', datasource: 'mongodb'});
  901. const rep = schema.getRepository('model');
  902. const created = await rep.create({foo: null});
  903. const id = created[DEF_PK];
  904. const replaced = await rep.replaceById(id, {foo: 'str'});
  905. expect(replaced).to.be.eql({[DEF_PK]: id, foo: 'str'});
  906. const oid = new ObjectId(id);
  907. const rawData = await MDB_CLIENT.db()
  908. .collection('model')
  909. .findOne({_id: oid});
  910. expect(rawData).to.be.eql({_id: oid, foo: 'str'});
  911. });
  912. it('stores a number as is', async function () {
  913. const schema = createSchema();
  914. schema.defineModel({name: 'model', datasource: 'mongodb'});
  915. const rep = schema.getRepository('model');
  916. const created = await rep.create({foo: null});
  917. const id = created[DEF_PK];
  918. const replaced = await rep.replaceById(id, {foo: 10});
  919. expect(replaced).to.be.eql({[DEF_PK]: id, foo: 10});
  920. const oid = new ObjectId(id);
  921. const rawData = await MDB_CLIENT.db()
  922. .collection('model')
  923. .findOne({_id: oid});
  924. expect(rawData).to.be.eql({_id: oid, foo: 10});
  925. });
  926. it('stores a boolean as is', async function () {
  927. const schema = createSchema();
  928. schema.defineModel({name: 'model', datasource: 'mongodb'});
  929. const rep = schema.getRepository('model');
  930. const created = await rep.create({foo: null, bar: null});
  931. const id = created[DEF_PK];
  932. const replaced = await rep.replaceById(id, {foo: true, bar: false});
  933. expect(replaced).to.be.eql({[DEF_PK]: id, foo: true, bar: false});
  934. const oid = new ObjectId(id);
  935. const rawData = await MDB_CLIENT.db()
  936. .collection('model')
  937. .findOne({_id: oid});
  938. expect(rawData).to.be.eql({_id: oid, foo: true, bar: false});
  939. });
  940. it('stores an array as is', async function () {
  941. const schema = createSchema();
  942. schema.defineModel({name: 'model', datasource: 'mongodb'});
  943. const rep = schema.getRepository('model');
  944. const created = await rep.create({foo: null});
  945. const id = created[DEF_PK];
  946. const replaced = await rep.replaceById(id, {foo: ['bar']});
  947. expect(replaced).to.be.eql({[DEF_PK]: id, foo: ['bar']});
  948. const oid = new ObjectId(id);
  949. const rawData = await MDB_CLIENT.db()
  950. .collection('model')
  951. .findOne({_id: oid});
  952. expect(rawData).to.be.eql({_id: oid, foo: ['bar']});
  953. });
  954. it('stores an object as is', async function () {
  955. const schema = createSchema();
  956. schema.defineModel({name: 'model', datasource: 'mongodb'});
  957. const rep = schema.getRepository('model');
  958. const created = await rep.create({foo: null});
  959. const id = created[DEF_PK];
  960. const replaced = await rep.replaceById(id, {foo: {bar: 10}});
  961. expect(replaced).to.be.eql({[DEF_PK]: id, foo: {bar: 10}});
  962. const oid = new ObjectId(id);
  963. const rawData = await MDB_CLIENT.db()
  964. .collection('model')
  965. .findOne({_id: oid});
  966. expect(rawData).to.be.eql({_id: oid, foo: {bar: 10}});
  967. });
  968. it('stores an undefined as null', async function () {
  969. const schema = createSchema();
  970. schema.defineModel({name: 'model', datasource: 'mongodb'});
  971. const rep = schema.getRepository('model');
  972. const created = await rep.create({foo: 10});
  973. const id = created[DEF_PK];
  974. const replaced = await rep.replaceById(id, {foo: undefined});
  975. expect(replaced).to.be.eql({[DEF_PK]: id, foo: null});
  976. const oid = new ObjectId(id);
  977. const rawData = await MDB_CLIENT.db()
  978. .collection('model')
  979. .findOne({_id: oid});
  980. expect(rawData).to.be.eql({_id: oid, foo: null});
  981. });
  982. it('stores an null as is', async function () {
  983. const schema = createSchema();
  984. schema.defineModel({name: 'model', datasource: 'mongodb'});
  985. const rep = schema.getRepository('model');
  986. const created = await rep.create({foo: 10});
  987. const id = created[DEF_PK];
  988. const replaced = await rep.replaceById(id, {foo: null});
  989. expect(replaced).to.be.eql({[DEF_PK]: id, foo: null});
  990. const oid = new ObjectId(id);
  991. const rawData = await MDB_CLIENT.db()
  992. .collection('model')
  993. .findOne({_id: oid});
  994. expect(rawData).to.be.eql({_id: oid, foo: null});
  995. });
  996. it('uses a short fields clause to filter results', async function () {
  997. const schema = createSchema();
  998. schema.defineModel({name: 'model', datasource: 'mongodb'});
  999. const rep = schema.getRepository('model');
  1000. const created = await rep.create({foo: 10, bar: 20});
  1001. const id = created[DEF_PK];
  1002. const replaced = await rep.replaceById(
  1003. id,
  1004. {foo: 15, bar: 25},
  1005. {fields: 'foo'},
  1006. );
  1007. expect(replaced).to.be.eql({[DEF_PK]: id, foo: 15});
  1008. const oid = new ObjectId(id);
  1009. const rawData = await MDB_CLIENT.db()
  1010. .collection('model')
  1011. .findOne({_id: oid});
  1012. expect(rawData).to.be.eql({_id: oid, foo: 15, bar: 25});
  1013. });
  1014. it('uses a full fields clause to filter results', async function () {
  1015. const schema = createSchema();
  1016. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1017. const rep = schema.getRepository('model');
  1018. const created = await rep.create({foo: 10, bar: 20, baz: 30});
  1019. const id = created[DEF_PK];
  1020. const replaced = await rep.replaceById(
  1021. id,
  1022. {foo: 15, bar: 25, baz: 35},
  1023. {fields: ['foo', 'bar']},
  1024. );
  1025. expect(replaced).to.be.eql({[DEF_PK]: id, foo: 15, bar: 25});
  1026. const oid = new ObjectId(id);
  1027. const rawData = await MDB_CLIENT.db()
  1028. .collection('model')
  1029. .findOne({_id: oid});
  1030. expect(rawData).to.be.eql({_id: oid, foo: 15, bar: 25, baz: 35});
  1031. });
  1032. it('a fields clause uses property names instead of column names', async function () {
  1033. const schema = createSchema();
  1034. schema.defineModel({
  1035. name: 'model',
  1036. datasource: 'mongodb',
  1037. properties: {
  1038. foo: {
  1039. type: DataType.NUMBER,
  1040. columnName: 'fooCol',
  1041. },
  1042. bar: {
  1043. type: DataType.NUMBER,
  1044. columnName: 'barCol',
  1045. },
  1046. baz: {
  1047. type: DataType.NUMBER,
  1048. columnName: 'bazCol',
  1049. },
  1050. },
  1051. });
  1052. const rep = schema.getRepository('model');
  1053. const created = await rep.create(
  1054. {foo: 10, bar: 20, baz: 30},
  1055. {fields: ['fooCol', 'barCol']},
  1056. );
  1057. const id = created[DEF_PK];
  1058. const replaced = await rep.replaceById(
  1059. id,
  1060. {foo: 15, bar: 25, baz: 35},
  1061. {fields: ['fooCol', 'barCol']},
  1062. );
  1063. expect(replaced).to.be.eql({[DEF_PK]: replaced[DEF_PK]});
  1064. const oid = new ObjectId(id);
  1065. const rawData = await MDB_CLIENT.db()
  1066. .collection('model')
  1067. .findOne({_id: oid});
  1068. expect(rawData).to.be.eql({_id: oid, fooCol: 15, barCol: 25, bazCol: 35});
  1069. });
  1070. });
  1071. describe('patchById', function () {
  1072. it('updates only provided properties by a given identifier', async function () {
  1073. const schema = createSchema();
  1074. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1075. const rep = schema.getRepository('model');
  1076. const created = await rep.create({foo: 10});
  1077. const id = created[DEF_PK];
  1078. const patched = await rep.patchById(id, {bar: 20});
  1079. expect(patched).to.be.eql({[DEF_PK]: id, foo: 10, bar: 20});
  1080. const oid = new ObjectId(id);
  1081. const rawData = await MDB_CLIENT.db()
  1082. .collection('model')
  1083. .findOne({_id: oid});
  1084. expect(rawData).to.be.eql({_id: oid, foo: 10, bar: 20});
  1085. });
  1086. it('does not throw an error if a partial data does not have required property', async function () {
  1087. const schema = createSchema();
  1088. schema.defineModel({
  1089. name: 'model',
  1090. datasource: 'mongodb',
  1091. properties: {
  1092. foo: {
  1093. type: DataType.NUMBER,
  1094. required: true,
  1095. },
  1096. },
  1097. });
  1098. const rep = schema.getRepository('model');
  1099. const {insertedId: oid} = await MDB_CLIENT.db()
  1100. .collection('model')
  1101. .insertOne({bar: 10});
  1102. const patched = await rep.patchById(oid, {baz: 20});
  1103. const id = String(oid);
  1104. expect(patched).to.be.eql({[DEF_PK]: id, bar: 10, baz: 20});
  1105. const rawData = await MDB_CLIENT.db()
  1106. .collection('model')
  1107. .findOne({_id: oid});
  1108. expect(rawData).to.be.eql({_id: oid, bar: 10, baz: 20});
  1109. });
  1110. it('ignores identifier value in a given data in case of a default primary key', async function () {
  1111. const schema = createSchema();
  1112. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1113. const rep = schema.getRepository('model');
  1114. await rep.create({[DEF_PK]: 'foo', prop: 10});
  1115. const patched = await rep.patchById('foo', {
  1116. [DEF_PK]: 'bar',
  1117. prop: 20,
  1118. });
  1119. expect(patched).to.be.eql({[DEF_PK]: 'foo', prop: 20});
  1120. const rawData = await MDB_CLIENT.db()
  1121. .collection('model')
  1122. .findOne({_id: 'foo'});
  1123. expect(rawData).to.be.eql({_id: 'foo', prop: 20});
  1124. });
  1125. it('ignores identifier value in a given data in case of a custom primary key', async function () {
  1126. const schema = createSchema();
  1127. schema.defineModel({
  1128. name: 'model',
  1129. datasource: 'mongodb',
  1130. properties: {
  1131. myId: {
  1132. type: DataType.STRING,
  1133. primaryKey: true,
  1134. columnName: '_id',
  1135. },
  1136. },
  1137. });
  1138. const rep = schema.getRepository('model');
  1139. await rep.create({myId: 'foo', prop: 10});
  1140. const patched = await rep.patchById('foo', {myId: 'bar', prop: 20});
  1141. expect(patched).to.be.eql({myId: 'foo', prop: 20});
  1142. const rawData = await MDB_CLIENT.db()
  1143. .collection('model')
  1144. .findOne({_id: 'foo'});
  1145. expect(rawData).to.be.eql({_id: 'foo', prop: 20});
  1146. });
  1147. it('throws an error if a given identifier does not exist', async function () {
  1148. const schema = createSchema();
  1149. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1150. const rep = schema.getRepository('model');
  1151. const oid = new ObjectId();
  1152. const promise = rep.patchById(oid, {foo: 10});
  1153. await expect(promise).to.be.rejectedWith(
  1154. format('Identifier "%s" is not found.', oid),
  1155. );
  1156. });
  1157. it('throws an error for a custom primary key', async function () {
  1158. const schema = createSchema();
  1159. schema.defineModel({
  1160. name: 'model',
  1161. datasource: 'mongodb',
  1162. properties: {
  1163. myId: {
  1164. type: DataType.ANY,
  1165. primaryKey: true,
  1166. },
  1167. },
  1168. });
  1169. const rep = schema.getRepository('model');
  1170. const promise = rep.patchById('id', {foo: 10});
  1171. await expect(promise).to.be.rejectedWith(
  1172. 'MongoDB is not supporting custom names of the primary key. ' +
  1173. 'Do use "id" as a primary key instead of "myId".',
  1174. );
  1175. });
  1176. it('uses a specified column name for a regular property', async function () {
  1177. const schema = createSchema();
  1178. schema.defineModel({
  1179. name: 'model',
  1180. datasource: 'mongodb',
  1181. properties: {
  1182. foo: {
  1183. type: DataType.NUMBER,
  1184. columnName: 'bar',
  1185. },
  1186. },
  1187. });
  1188. const rep = schema.getRepository('model');
  1189. const created = await rep.create({foo: 10});
  1190. const id = created[DEF_PK];
  1191. const patched = await rep.replaceById(id, {foo: 20});
  1192. expect(patched).to.be.eql({[DEF_PK]: id, foo: 20});
  1193. const oid = new ObjectId(id);
  1194. const rawData = await MDB_CLIENT.db()
  1195. .collection('model')
  1196. .findOne({_id: oid});
  1197. expect(rawData).to.be.eql({_id: oid, bar: 20});
  1198. });
  1199. it('stores a Date instance as date and returns string type', async function () {
  1200. const schema = createSchema();
  1201. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1202. const rep = schema.getRepository('model');
  1203. const date = new Date();
  1204. const dateString = date.toISOString();
  1205. const created = await rep.create({date: null});
  1206. const id = created[DEF_PK];
  1207. const patched = await rep.patchById(id, {date});
  1208. expect(patched).to.be.eql({[DEF_PK]: id, date: dateString});
  1209. const oid = new ObjectId(id);
  1210. const rawData = await MDB_CLIENT.db()
  1211. .collection('model')
  1212. .findOne({_id: oid});
  1213. expect(rawData).to.be.eql({_id: oid, date});
  1214. });
  1215. it('stores a Date string as date and returns string type', async function () {
  1216. const schema = createSchema();
  1217. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1218. const rep = schema.getRepository('model');
  1219. const date = new Date();
  1220. const dateString = date.toISOString();
  1221. const created = await rep.create({date: null});
  1222. const id = created[DEF_PK];
  1223. const patched = await rep.patchById(id, {date: dateString});
  1224. expect(patched).to.be.eql({[DEF_PK]: id, date: dateString});
  1225. const oid = new ObjectId(id);
  1226. const rawData = await MDB_CLIENT.db()
  1227. .collection('model')
  1228. .findOne({_id: oid});
  1229. expect(rawData).to.be.eql({_id: oid, date});
  1230. });
  1231. it('stores a string as is', async function () {
  1232. const schema = createSchema();
  1233. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1234. const rep = schema.getRepository('model');
  1235. const created = await rep.create({foo: null});
  1236. const id = created[DEF_PK];
  1237. const patched = await rep.patchById(id, {foo: 'str'});
  1238. expect(patched).to.be.eql({[DEF_PK]: id, foo: 'str'});
  1239. const oid = new ObjectId(id);
  1240. const rawData = await MDB_CLIENT.db()
  1241. .collection('model')
  1242. .findOne({_id: oid});
  1243. expect(rawData).to.be.eql({_id: oid, foo: 'str'});
  1244. });
  1245. it('stores a number as is', async function () {
  1246. const schema = createSchema();
  1247. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1248. const rep = schema.getRepository('model');
  1249. const created = await rep.create({foo: null});
  1250. const id = created[DEF_PK];
  1251. const patched = await rep.patchById(id, {foo: 10});
  1252. expect(patched).to.be.eql({[DEF_PK]: id, foo: 10});
  1253. const oid = new ObjectId(id);
  1254. const rawData = await MDB_CLIENT.db()
  1255. .collection('model')
  1256. .findOne({_id: oid});
  1257. expect(rawData).to.be.eql({_id: oid, foo: 10});
  1258. });
  1259. it('stores a boolean as is', async function () {
  1260. const schema = createSchema();
  1261. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1262. const rep = schema.getRepository('model');
  1263. const created = await rep.create({foo: null, bar: null});
  1264. const id = created[DEF_PK];
  1265. const patched = await rep.patchById(id, {foo: true, bar: false});
  1266. expect(patched).to.be.eql({[DEF_PK]: id, foo: true, bar: false});
  1267. const oid = new ObjectId(id);
  1268. const rawData = await MDB_CLIENT.db()
  1269. .collection('model')
  1270. .findOne({_id: oid});
  1271. expect(rawData).to.be.eql({_id: oid, foo: true, bar: false});
  1272. });
  1273. it('stores an array as is', async function () {
  1274. const schema = createSchema();
  1275. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1276. const rep = schema.getRepository('model');
  1277. const created = await rep.create({foo: null});
  1278. const id = created[DEF_PK];
  1279. const patched = await rep.patchById(id, {foo: ['bar']});
  1280. expect(patched).to.be.eql({[DEF_PK]: id, foo: ['bar']});
  1281. const oid = new ObjectId(id);
  1282. const rawData = await MDB_CLIENT.db()
  1283. .collection('model')
  1284. .findOne({_id: oid});
  1285. expect(rawData).to.be.eql({_id: oid, foo: ['bar']});
  1286. });
  1287. it('stores an object as is', async function () {
  1288. const schema = createSchema();
  1289. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1290. const rep = schema.getRepository('model');
  1291. const created = await rep.create({foo: null});
  1292. const id = created[DEF_PK];
  1293. const patched = await rep.patchById(id, {foo: {bar: 10}});
  1294. expect(patched).to.be.eql({[DEF_PK]: id, foo: {bar: 10}});
  1295. const oid = new ObjectId(id);
  1296. const rawData = await MDB_CLIENT.db()
  1297. .collection('model')
  1298. .findOne({_id: oid});
  1299. expect(rawData).to.be.eql({_id: oid, foo: {bar: 10}});
  1300. });
  1301. it('stores an undefined as null', async function () {
  1302. const schema = createSchema();
  1303. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1304. const rep = schema.getRepository('model');
  1305. const created = await rep.create({foo: 10});
  1306. const id = created[DEF_PK];
  1307. const patched = await rep.patchById(id, {foo: undefined});
  1308. expect(patched).to.be.eql({[DEF_PK]: id, foo: null});
  1309. const oid = new ObjectId(id);
  1310. const rawData = await MDB_CLIENT.db()
  1311. .collection('model')
  1312. .findOne({_id: oid});
  1313. expect(rawData).to.be.eql({_id: oid, foo: null});
  1314. });
  1315. it('stores an null as is', async function () {
  1316. const schema = createSchema();
  1317. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1318. const rep = schema.getRepository('model');
  1319. const created = await rep.create({foo: 10});
  1320. const id = created[DEF_PK];
  1321. const patched = await rep.patchById(id, {foo: null});
  1322. expect(patched).to.be.eql({[DEF_PK]: id, foo: null});
  1323. const oid = new ObjectId(id);
  1324. const rawData = await MDB_CLIENT.db()
  1325. .collection('model')
  1326. .findOne({_id: oid});
  1327. expect(rawData).to.be.eql({_id: oid, foo: null});
  1328. });
  1329. it('uses a short fields clause to filter results', async function () {
  1330. const schema = createSchema();
  1331. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1332. const rep = schema.getRepository('model');
  1333. const created = await rep.create({foo: 10, bar: 20});
  1334. const id = created[DEF_PK];
  1335. const patched = await rep.patchById(
  1336. id,
  1337. {foo: 15, bar: 25},
  1338. {fields: 'foo'},
  1339. );
  1340. expect(patched).to.be.eql({[DEF_PK]: id, foo: 15});
  1341. const oid = new ObjectId(id);
  1342. const rawData = await MDB_CLIENT.db()
  1343. .collection('model')
  1344. .findOne({_id: oid});
  1345. expect(rawData).to.be.eql({_id: oid, foo: 15, bar: 25});
  1346. });
  1347. it('uses a full fields clause to filter results', async function () {
  1348. const schema = createSchema();
  1349. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1350. const rep = schema.getRepository('model');
  1351. const created = await rep.create({foo: 10, bar: 20, baz: 30});
  1352. const id = created[DEF_PK];
  1353. const patched = await rep.patchById(
  1354. id,
  1355. {foo: 15, bar: 25, baz: 35},
  1356. {fields: ['foo', 'bar']},
  1357. );
  1358. expect(patched).to.be.eql({[DEF_PK]: id, foo: 15, bar: 25});
  1359. const oid = new ObjectId(id);
  1360. const rawData = await MDB_CLIENT.db()
  1361. .collection('model')
  1362. .findOne({_id: oid});
  1363. expect(rawData).to.be.eql({_id: oid, foo: 15, bar: 25, baz: 35});
  1364. });
  1365. it('a fields clause uses property names instead of column names', async function () {
  1366. const schema = createSchema();
  1367. schema.defineModel({
  1368. name: 'model',
  1369. datasource: 'mongodb',
  1370. properties: {
  1371. foo: {
  1372. type: DataType.NUMBER,
  1373. columnName: 'fooCol',
  1374. },
  1375. bar: {
  1376. type: DataType.NUMBER,
  1377. columnName: 'barCol',
  1378. },
  1379. baz: {
  1380. type: DataType.NUMBER,
  1381. columnName: 'bazCol',
  1382. },
  1383. },
  1384. });
  1385. const rep = schema.getRepository('model');
  1386. const created = await rep.create(
  1387. {foo: 10, bar: 20, baz: 30},
  1388. {fields: ['fooCol', 'barCol']},
  1389. );
  1390. const id = created[DEF_PK];
  1391. const patched = await rep.patchById(
  1392. id,
  1393. {foo: 15, bar: 25, baz: 35},
  1394. {fields: ['fooCol', 'barCol']},
  1395. );
  1396. expect(patched).to.be.eql({[DEF_PK]: patched[DEF_PK]});
  1397. const oid = new ObjectId(id);
  1398. const rawData = await MDB_CLIENT.db()
  1399. .collection('model')
  1400. .findOne({_id: oid});
  1401. expect(rawData).to.be.eql({_id: oid, fooCol: 15, barCol: 25, bazCol: 35});
  1402. });
  1403. });
  1404. describe('find', function () {
  1405. it('returns an empty array', async function () {
  1406. const schema = createSchema();
  1407. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1408. const rep = schema.getRepository('model');
  1409. const result = await rep.find();
  1410. expect(result).to.be.empty;
  1411. expect(result).to.be.instanceOf(Array);
  1412. });
  1413. it('returns all items', async function () {
  1414. const schema = createSchema();
  1415. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1416. const rep = schema.getRepository('model');
  1417. const doc1 = await rep.create({foo: 1});
  1418. const doc2 = await rep.create({bar: 2});
  1419. const doc3 = await rep.create({baz: 3});
  1420. const result = await rep.find();
  1421. expect(result).to.be.eql([doc1, doc2, doc3]);
  1422. });
  1423. it('uses a short fields clause to filter results', async function () {
  1424. const schema = createSchema();
  1425. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1426. const rep = schema.getRepository('model');
  1427. const created1 = await rep.create({foo: 10, bar: 20});
  1428. const created2 = await rep.create({foo: 20, bar: 30});
  1429. const id1 = created1[DEF_PK];
  1430. const id2 = created2[DEF_PK];
  1431. const result = await rep.find({fields: 'foo'});
  1432. expect(result).to.be.eql([
  1433. {[DEF_PK]: id1, foo: 10},
  1434. {[DEF_PK]: id2, foo: 20},
  1435. ]);
  1436. });
  1437. it('uses a full fields clause to filter results', async function () {
  1438. const schema = createSchema();
  1439. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1440. const rep = schema.getRepository('model');
  1441. const created1 = await rep.create({foo: 10, bar: 20, baz: 30});
  1442. const created2 = await rep.create({foo: 20, bar: 30, baz: 40});
  1443. const id1 = created1[DEF_PK];
  1444. const id2 = created2[DEF_PK];
  1445. const result = await rep.find({fields: ['foo', 'bar']});
  1446. expect(result).to.be.eql([
  1447. {[DEF_PK]: id1, foo: 10, bar: 20},
  1448. {[DEF_PK]: id2, foo: 20, bar: 30},
  1449. ]);
  1450. });
  1451. it('a fields clause uses property names instead of column names', async function () {
  1452. const schema = createSchema();
  1453. schema.defineModel({
  1454. name: 'model',
  1455. datasource: 'mongodb',
  1456. properties: {
  1457. foo: {
  1458. type: DataType.NUMBER,
  1459. columnName: 'fooCol',
  1460. },
  1461. bar: {
  1462. type: DataType.NUMBER,
  1463. columnName: 'barCol',
  1464. },
  1465. baz: {
  1466. type: DataType.NUMBER,
  1467. columnName: 'bazCol',
  1468. },
  1469. },
  1470. });
  1471. const rep = schema.getRepository('model');
  1472. const created1 = await rep.create({foo: 10, bar: 20, baz: 30});
  1473. const created2 = await rep.create({foo: 20, bar: 30, baz: 40});
  1474. const id1 = created1[DEF_PK];
  1475. const id2 = created2[DEF_PK];
  1476. const result = await rep.find({fields: ['fooCol', 'barCol']});
  1477. expect(result).to.be.eql([{[DEF_PK]: id1}, {[DEF_PK]: id2}]);
  1478. });
  1479. it('uses a short order clause to sort results', async function () {
  1480. const schema = createSchema();
  1481. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1482. const rep = schema.getRepository('model');
  1483. const created1 = await rep.create({foo: 20});
  1484. const created2 = await rep.create({foo: 5});
  1485. const created3 = await rep.create({foo: 10});
  1486. const result1 = await rep.find({order: 'foo'});
  1487. const result2 = await rep.find({order: 'foo ASC'});
  1488. const result3 = await rep.find({order: 'foo DESC'});
  1489. expect(result1).to.be.eql([
  1490. {[DEF_PK]: created2[DEF_PK], foo: 5},
  1491. {[DEF_PK]: created3[DEF_PK], foo: 10},
  1492. {[DEF_PK]: created1[DEF_PK], foo: 20},
  1493. ]);
  1494. expect(result2).to.be.eql([
  1495. {[DEF_PK]: created2[DEF_PK], foo: 5},
  1496. {[DEF_PK]: created3[DEF_PK], foo: 10},
  1497. {[DEF_PK]: created1[DEF_PK], foo: 20},
  1498. ]);
  1499. expect(result3).to.be.eql([
  1500. {[DEF_PK]: created1[DEF_PK], foo: 20},
  1501. {[DEF_PK]: created3[DEF_PK], foo: 10},
  1502. {[DEF_PK]: created2[DEF_PK], foo: 5},
  1503. ]);
  1504. });
  1505. it('uses a full order clause to sort results', async function () {
  1506. const schema = createSchema();
  1507. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1508. const rep = schema.getRepository('model');
  1509. const created1 = await rep.create({foo: 20, bar: 'b'});
  1510. const created2 = await rep.create({foo: 5, bar: 'b'});
  1511. const created3 = await rep.create({foo: 10, bar: 'a'});
  1512. const result1 = await rep.find({order: ['bar DESC', 'foo']});
  1513. const result2 = await rep.find({order: ['bar', 'foo ASC']});
  1514. expect(result1).to.be.eql([
  1515. {[DEF_PK]: created2[DEF_PK], foo: 5, bar: 'b'},
  1516. {[DEF_PK]: created1[DEF_PK], foo: 20, bar: 'b'},
  1517. {[DEF_PK]: created3[DEF_PK], foo: 10, bar: 'a'},
  1518. ]);
  1519. expect(result2).to.be.eql([
  1520. {[DEF_PK]: created3[DEF_PK], foo: 10, bar: 'a'},
  1521. {[DEF_PK]: created2[DEF_PK], foo: 5, bar: 'b'},
  1522. {[DEF_PK]: created1[DEF_PK], foo: 20, bar: 'b'},
  1523. ]);
  1524. });
  1525. it('an order clause uses property names instead of column names', async function () {
  1526. const schema = createSchema();
  1527. schema.defineModel({
  1528. name: 'model',
  1529. datasource: 'mongodb',
  1530. properties: {
  1531. foo: {
  1532. type: DataType.NUMBER,
  1533. columnName: 'fooCol',
  1534. },
  1535. bar: {
  1536. type: DataType.STRING,
  1537. columnName: 'barCol',
  1538. },
  1539. },
  1540. });
  1541. const rep = schema.getRepository('model');
  1542. const created1 = await rep.create({foo: 20, bar: 'b'});
  1543. const created2 = await rep.create({foo: 5, bar: 'b'});
  1544. const created3 = await rep.create({foo: 10, bar: 'a'});
  1545. const result1 = await rep.find({order: ['bar DESC', 'foo']});
  1546. const result2 = await rep.find({order: ['bar', 'foo ASC']});
  1547. expect(result1).to.be.eql([
  1548. {[DEF_PK]: created2[DEF_PK], foo: 5, bar: 'b'},
  1549. {[DEF_PK]: created1[DEF_PK], foo: 20, bar: 'b'},
  1550. {[DEF_PK]: created3[DEF_PK], foo: 10, bar: 'a'},
  1551. ]);
  1552. expect(result2).to.be.eql([
  1553. {[DEF_PK]: created3[DEF_PK], foo: 10, bar: 'a'},
  1554. {[DEF_PK]: created2[DEF_PK], foo: 5, bar: 'b'},
  1555. {[DEF_PK]: created1[DEF_PK], foo: 20, bar: 'b'},
  1556. ]);
  1557. });
  1558. it('uses a limit clause to filter results', async function () {
  1559. const schema = createSchema();
  1560. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1561. const rep = schema.getRepository('model');
  1562. const created1 = await rep.create({foo: 10});
  1563. const created2 = await rep.create({foo: 20});
  1564. await rep.create({foo: 30});
  1565. const result = await rep.find({limit: 2});
  1566. expect(result).to.be.eql([created1, created2]);
  1567. });
  1568. it('uses a skip clause to filter results', async function () {
  1569. const schema = createSchema();
  1570. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1571. const rep = schema.getRepository('model');
  1572. await rep.create({foo: 10});
  1573. const created2 = await rep.create({foo: 20});
  1574. const created3 = await rep.create({foo: 30});
  1575. const result = await rep.find({skip: 1});
  1576. expect(result).to.be.eql([created2, created3]);
  1577. });
  1578. describe('uses a where clause to filter results', function () {
  1579. it('matches by a document subset', async function () {
  1580. const schema = createSchema();
  1581. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1582. const rep = schema.getRepository('model');
  1583. const created1 = await rep.create({foo: 5, bar: 'b'});
  1584. const created2 = await rep.create({foo: 10, bar: 'a'});
  1585. const result1 = await rep.find({where: {foo: 10}});
  1586. const result2 = await rep.find({where: {foo: 5, bar: 'b'}});
  1587. expect(result1).to.be.eql([
  1588. {
  1589. [DEF_PK]: created2[DEF_PK],
  1590. foo: 10,
  1591. bar: 'a',
  1592. },
  1593. ]);
  1594. expect(result2).to.be.eql([
  1595. {
  1596. [DEF_PK]: created1[DEF_PK],
  1597. foo: 5,
  1598. bar: 'b',
  1599. },
  1600. ]);
  1601. });
  1602. it('matches by the "eq" operator', async function () {
  1603. const schema = createSchema();
  1604. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1605. const rep = schema.getRepository('model');
  1606. await rep.create({foo: 5});
  1607. const created2 = await rep.create({foo: 10});
  1608. const created3 = await rep.create({foo: 10});
  1609. const result = await rep.find({where: {foo: {eq: 10}}});
  1610. expect(result).to.be.eql([
  1611. {[DEF_PK]: created2[DEF_PK], foo: 10},
  1612. {[DEF_PK]: created3[DEF_PK], foo: 10},
  1613. ]);
  1614. });
  1615. it('matches by the "neq" operator', async function () {
  1616. const schema = createSchema();
  1617. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1618. const rep = schema.getRepository('model');
  1619. const created1 = await rep.create({foo: 5});
  1620. await rep.create({foo: 10});
  1621. await rep.create({foo: 10});
  1622. const result = await rep.find({where: {foo: {neq: 10}}});
  1623. expect(result).to.be.eql([{[DEF_PK]: created1[DEF_PK], foo: 5}]);
  1624. });
  1625. it('matches by the "gt" operator', async function () {
  1626. const schema = createSchema();
  1627. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1628. const rep = schema.getRepository('model');
  1629. await rep.create({foo: 5});
  1630. await rep.create({foo: 10});
  1631. const created3 = await rep.create({foo: 15});
  1632. const result = await rep.find({where: {foo: {gt: 10}}});
  1633. expect(result).to.be.eql([{[DEF_PK]: created3[DEF_PK], foo: 15}]);
  1634. });
  1635. it('matches by the "lt" operator', async function () {
  1636. const schema = createSchema();
  1637. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1638. const rep = schema.getRepository('model');
  1639. const created1 = await rep.create({foo: 5});
  1640. await rep.create({foo: 10});
  1641. await rep.create({foo: 15});
  1642. const result = await rep.find({where: {foo: {lt: 10}}});
  1643. expect(result).to.be.eql([{[DEF_PK]: created1[DEF_PK], foo: 5}]);
  1644. });
  1645. it('matches by the "gte" operator', async function () {
  1646. const schema = createSchema();
  1647. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1648. const rep = schema.getRepository('model');
  1649. await rep.create({foo: 5});
  1650. const created2 = await rep.create({foo: 10});
  1651. const created3 = await rep.create({foo: 15});
  1652. const result = await rep.find({where: {foo: {gte: 10}}});
  1653. expect(result).to.be.eql([
  1654. {[DEF_PK]: created2[DEF_PK], foo: 10},
  1655. {[DEF_PK]: created3[DEF_PK], foo: 15},
  1656. ]);
  1657. });
  1658. it('matches by the "lte" operator', async function () {
  1659. const schema = createSchema();
  1660. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1661. const rep = schema.getRepository('model');
  1662. const created1 = await rep.create({foo: 5});
  1663. const created2 = await rep.create({foo: 10});
  1664. await rep.create({foo: 15});
  1665. const result = await rep.find({where: {foo: {lte: 10}}});
  1666. expect(result).to.be.eql([
  1667. {[DEF_PK]: created1[DEF_PK], foo: 5},
  1668. {[DEF_PK]: created2[DEF_PK], foo: 10},
  1669. ]);
  1670. });
  1671. it('matches by the "inq" operator', async function () {
  1672. const schema = createSchema();
  1673. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1674. const rep = schema.getRepository('model');
  1675. const created1 = await rep.create({foo: 5});
  1676. const created2 = await rep.create({foo: 10});
  1677. await rep.create({foo: 15});
  1678. const result = await rep.find({where: {foo: {inq: [5, 10]}}});
  1679. expect(result).to.be.eql([
  1680. {[DEF_PK]: created1[DEF_PK], foo: 5},
  1681. {[DEF_PK]: created2[DEF_PK], foo: 10},
  1682. ]);
  1683. });
  1684. it('matches by the "nin" operator', async function () {
  1685. const schema = createSchema();
  1686. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1687. const rep = schema.getRepository('model');
  1688. await rep.create({foo: 5});
  1689. await rep.create({foo: 10});
  1690. const created3 = await rep.create({foo: 15});
  1691. const result = await rep.find({where: {foo: {nin: [5, 10]}}});
  1692. expect(result).to.be.eql([{[DEF_PK]: created3[DEF_PK], foo: 15}]);
  1693. });
  1694. it('matches by the "between" operator', async function () {
  1695. const schema = createSchema();
  1696. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1697. const rep = schema.getRepository('model');
  1698. await rep.create({foo: 5});
  1699. const created2 = await rep.create({foo: 10});
  1700. const created3 = await rep.create({foo: 15});
  1701. await rep.create({foo: 20});
  1702. const result = await rep.find({where: {foo: {between: [9, 16]}}});
  1703. expect(result).to.be.eql([
  1704. {[DEF_PK]: created2[DEF_PK], foo: 10},
  1705. {[DEF_PK]: created3[DEF_PK], foo: 15},
  1706. ]);
  1707. });
  1708. it('matches by the "exists" operator', async function () {
  1709. const schema = createSchema();
  1710. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1711. const rep = schema.getRepository('model');
  1712. const created1 = await rep.create({});
  1713. const created2 = await rep.create({foo: undefined});
  1714. const created3 = await rep.create({foo: null});
  1715. const created4 = await rep.create({foo: 10});
  1716. const result1 = await rep.find({where: {foo: {exists: true}}});
  1717. const result2 = await rep.find({where: {foo: {exists: false}}});
  1718. expect(result1).to.be.eql([
  1719. {[DEF_PK]: created2[DEF_PK], foo: null},
  1720. {[DEF_PK]: created3[DEF_PK], foo: null},
  1721. {[DEF_PK]: created4[DEF_PK], foo: 10},
  1722. ]);
  1723. expect(result2).to.be.eql([{[DEF_PK]: created1[DEF_PK]}]);
  1724. });
  1725. it('matches by the "like" operator', async function () {
  1726. const schema = createSchema();
  1727. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1728. const rep = schema.getRepository('model');
  1729. await rep.create({foo: 'lorem ipsum'});
  1730. const created2 = await rep.create({foo: 'dolor sit amet'});
  1731. await rep.create({foo: 'DOLOR SIT AMET'});
  1732. const created4 = await rep.create({foo: 'sit amet'});
  1733. const result = await rep.find({where: {foo: {like: 'sit amet'}}});
  1734. expect(result).to.be.eql([
  1735. {[DEF_PK]: created2[DEF_PK], foo: 'dolor sit amet'},
  1736. {[DEF_PK]: created4[DEF_PK], foo: 'sit amet'},
  1737. ]);
  1738. });
  1739. it('matches by the "ilike" operator', async function () {
  1740. const schema = createSchema();
  1741. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1742. const rep = schema.getRepository('model');
  1743. await rep.create({foo: 'lorem ipsum'});
  1744. const created2 = await rep.create({foo: 'dolor sit amet'});
  1745. const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
  1746. const created4 = await rep.create({foo: 'sit amet'});
  1747. const result = await rep.find({where: {foo: {ilike: 'sit amet'}}});
  1748. expect(result).to.be.eql([
  1749. {[DEF_PK]: created2[DEF_PK], foo: 'dolor sit amet'},
  1750. {[DEF_PK]: created3[DEF_PK], foo: 'DOLOR SIT AMET'},
  1751. {[DEF_PK]: created4[DEF_PK], foo: 'sit amet'},
  1752. ]);
  1753. });
  1754. it('matches by the "nlike" operator', async function () {
  1755. const schema = createSchema();
  1756. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1757. const rep = schema.getRepository('model');
  1758. const created1 = await rep.create({foo: 'lorem ipsum'});
  1759. await rep.create({foo: 'dolor sit amet'});
  1760. const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
  1761. await rep.create({foo: 'sit amet'});
  1762. const result = await rep.find({where: {foo: {nlike: 'sit amet'}}});
  1763. expect(result).to.be.eql([
  1764. {[DEF_PK]: created1[DEF_PK], foo: 'lorem ipsum'},
  1765. {[DEF_PK]: created3[DEF_PK], foo: 'DOLOR SIT AMET'},
  1766. ]);
  1767. });
  1768. it('matches by the "nilike" operator', async function () {
  1769. const schema = createSchema();
  1770. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1771. const rep = schema.getRepository('model');
  1772. const created1 = await rep.create({foo: 'lorem ipsum'});
  1773. await rep.create({foo: 'dolor sit amet'});
  1774. await rep.create({foo: 'DOLOR SIT AMET'});
  1775. await rep.create({foo: 'sit amet'});
  1776. const result = await rep.find({where: {foo: {nilike: 'sit amet'}}});
  1777. expect(result).to.be.eql([
  1778. {[DEF_PK]: created1[DEF_PK], foo: 'lorem ipsum'},
  1779. ]);
  1780. });
  1781. it('matches by the "regexp" operator', async function () {
  1782. const schema = createSchema();
  1783. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1784. const rep = schema.getRepository('model');
  1785. await rep.create({foo: 'lorem ipsum'});
  1786. const created2 = await rep.create({foo: 'dolor sit amet'});
  1787. await rep.create({foo: 'DOLOR SIT AMET'});
  1788. const created4 = await rep.create({foo: 'sit amet'});
  1789. const result = await rep.find({where: {foo: {regexp: 'sit am+'}}});
  1790. expect(result).to.be.eql([
  1791. {[DEF_PK]: created2[DEF_PK], foo: 'dolor sit amet'},
  1792. {[DEF_PK]: created4[DEF_PK], foo: 'sit amet'},
  1793. ]);
  1794. });
  1795. it('matches by the "regexp" operator with flags', async function () {
  1796. const schema = createSchema();
  1797. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1798. const rep = schema.getRepository('model');
  1799. await rep.create({foo: 'lorem ipsum'});
  1800. const created2 = await rep.create({foo: 'dolor sit amet'});
  1801. const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
  1802. const created4 = await rep.create({foo: 'sit amet'});
  1803. const result = await rep.find({
  1804. where: {
  1805. foo: {regexp: 'sit am+', flags: 'i'},
  1806. },
  1807. });
  1808. expect(result).to.be.eql([
  1809. {[DEF_PK]: created2[DEF_PK], foo: 'dolor sit amet'},
  1810. {[DEF_PK]: created3[DEF_PK], foo: 'DOLOR SIT AMET'},
  1811. {[DEF_PK]: created4[DEF_PK], foo: 'sit amet'},
  1812. ]);
  1813. });
  1814. });
  1815. });
  1816. describe('findById', function () {
  1817. it('throws an error if a given identifier does not exist', async function () {
  1818. const schema = createSchema();
  1819. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1820. const rep = schema.getRepository('model');
  1821. const oid = new ObjectId();
  1822. const promise = rep.findById(oid);
  1823. await expect(promise).to.be.rejectedWith(
  1824. format('Identifier "%s" is not found.', oid),
  1825. );
  1826. });
  1827. it('uses a short fields clause to filter results', async function () {
  1828. const schema = createSchema();
  1829. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1830. const rep = schema.getRepository('model');
  1831. const created = await rep.create({foo: 10, bar: 20});
  1832. const id = created[DEF_PK];
  1833. const result = await rep.findById(id, {fields: 'foo'});
  1834. expect(result).to.be.eql({[DEF_PK]: id, foo: 10});
  1835. });
  1836. it('uses a full fields clause to filter results', async function () {
  1837. const schema = createSchema();
  1838. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1839. const rep = schema.getRepository('model');
  1840. const created = await rep.create({foo: 10, bar: 20, baz: 30});
  1841. const id = created[DEF_PK];
  1842. const result = await rep.findById(id, {fields: ['foo', 'bar']});
  1843. expect(result).to.be.eql({[DEF_PK]: id, foo: 10, bar: 20});
  1844. });
  1845. });
  1846. describe('delete', function () {
  1847. it('removes all table items and returns their number', async function () {
  1848. const schema = createSchema();
  1849. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1850. const rep = schema.getRepository('model');
  1851. await rep.create({foo: 1});
  1852. await rep.create({foo: 2});
  1853. await rep.create({foo: 3});
  1854. const result = await rep.delete();
  1855. expect(result).to.be.eql(3);
  1856. const rawData = await MDB_CLIENT.db()
  1857. .collection('model')
  1858. .find()
  1859. .toArray();
  1860. expect(rawData).to.be.empty;
  1861. });
  1862. describe('removes by a where clause', function () {
  1863. it('removes by a document subset', async function () {
  1864. const schema = createSchema();
  1865. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1866. const rep = schema.getRepository('model');
  1867. const created1 = await rep.create({foo: 5});
  1868. await rep.create({foo: 10});
  1869. const result = await rep.delete({foo: 10});
  1870. expect(result).to.be.eq(1);
  1871. const rawData = await MDB_CLIENT.db()
  1872. .collection('model')
  1873. .find()
  1874. .toArray();
  1875. expect(rawData).to.be.eql([
  1876. {_id: new ObjectId(created1[DEF_PK]), foo: 5},
  1877. ]);
  1878. });
  1879. it('matches by the "eq" operator', async function () {
  1880. const schema = createSchema();
  1881. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1882. const rep = schema.getRepository('model');
  1883. const created1 = await rep.create({foo: 5});
  1884. await rep.create({foo: 10});
  1885. await rep.create({foo: 10});
  1886. const result = await rep.delete({foo: {eq: 10}});
  1887. expect(result).to.be.eq(2);
  1888. const rawData = await MDB_CLIENT.db()
  1889. .collection('model')
  1890. .find()
  1891. .toArray();
  1892. expect(rawData).to.be.eql([
  1893. {_id: new ObjectId(created1[DEF_PK]), foo: 5},
  1894. ]);
  1895. });
  1896. it('matches by the "neq" operator', async function () {
  1897. const schema = createSchema();
  1898. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1899. const rep = schema.getRepository('model');
  1900. await rep.create({foo: 5});
  1901. await rep.create({foo: 5});
  1902. const created3 = await rep.create({foo: 10});
  1903. const result = await rep.delete({foo: {neq: 10}});
  1904. expect(result).to.be.eq(2);
  1905. const rawData = await MDB_CLIENT.db()
  1906. .collection('model')
  1907. .find()
  1908. .toArray();
  1909. expect(rawData).to.be.eql([
  1910. {_id: new ObjectId(created3[DEF_PK]), foo: 10},
  1911. ]);
  1912. });
  1913. it('matches by the "gt" operator', async function () {
  1914. const schema = createSchema();
  1915. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1916. const rep = schema.getRepository('model');
  1917. const created1 = await rep.create({foo: 5});
  1918. const created2 = await rep.create({foo: 10});
  1919. await rep.create({foo: 15});
  1920. const result = await rep.delete({foo: {gt: 10}});
  1921. expect(result).to.be.eq(1);
  1922. const rawData = await MDB_CLIENT.db()
  1923. .collection('model')
  1924. .find()
  1925. .toArray();
  1926. expect(rawData).to.be.eql([
  1927. {_id: new ObjectId(created1[DEF_PK]), foo: 5},
  1928. {_id: new ObjectId(created2[DEF_PK]), foo: 10},
  1929. ]);
  1930. });
  1931. it('matches by the "lt" operator', async function () {
  1932. const schema = createSchema();
  1933. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1934. const rep = schema.getRepository('model');
  1935. await rep.create({foo: 5});
  1936. const created2 = await rep.create({foo: 10});
  1937. const created3 = await rep.create({foo: 15});
  1938. const result = await rep.delete({foo: {lt: 10}});
  1939. expect(result).to.be.eq(1);
  1940. const rawData = await MDB_CLIENT.db()
  1941. .collection('model')
  1942. .find()
  1943. .toArray();
  1944. expect(rawData).to.be.eql([
  1945. {_id: new ObjectId(created2[DEF_PK]), foo: 10},
  1946. {_id: new ObjectId(created3[DEF_PK]), foo: 15},
  1947. ]);
  1948. });
  1949. it('matches by the "gte" operator', async function () {
  1950. const schema = createSchema();
  1951. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1952. const rep = schema.getRepository('model');
  1953. const created1 = await rep.create({foo: 5});
  1954. await rep.create({foo: 10});
  1955. await rep.create({foo: 15});
  1956. const result = await rep.delete({foo: {gte: 10}});
  1957. expect(result).to.be.eq(2);
  1958. const rawData = await MDB_CLIENT.db()
  1959. .collection('model')
  1960. .find()
  1961. .toArray();
  1962. expect(rawData).to.be.eql([
  1963. {_id: new ObjectId(created1[DEF_PK]), foo: 5},
  1964. ]);
  1965. });
  1966. it('matches by the "lte" operator', async function () {
  1967. const schema = createSchema();
  1968. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1969. const rep = schema.getRepository('model');
  1970. await rep.create({foo: 5});
  1971. await rep.create({foo: 10});
  1972. const created3 = await rep.create({foo: 15});
  1973. const result = await rep.delete({foo: {lte: 10}});
  1974. expect(result).to.be.eq(2);
  1975. const rawData = await MDB_CLIENT.db()
  1976. .collection('model')
  1977. .find()
  1978. .toArray();
  1979. expect(rawData).to.be.eql([
  1980. {_id: new ObjectId(created3[DEF_PK]), foo: 15},
  1981. ]);
  1982. });
  1983. it('matches by the "inq" operator', async function () {
  1984. const schema = createSchema();
  1985. schema.defineModel({name: 'model', datasource: 'mongodb'});
  1986. const rep = schema.getRepository('model');
  1987. await rep.create({foo: 5});
  1988. await rep.create({foo: 10});
  1989. const created3 = await rep.create({foo: 15});
  1990. const result = await rep.delete({foo: {inq: [5, 10]}});
  1991. expect(result).to.be.eq(2);
  1992. const rawData = await MDB_CLIENT.db()
  1993. .collection('model')
  1994. .find()
  1995. .toArray();
  1996. expect(rawData).to.be.eql([
  1997. {_id: new ObjectId(created3[DEF_PK]), foo: 15},
  1998. ]);
  1999. });
  2000. it('matches by the "nin" operator', async function () {
  2001. const schema = createSchema();
  2002. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2003. const rep = schema.getRepository('model');
  2004. const created1 = await rep.create({foo: 5});
  2005. const created2 = await rep.create({foo: 10});
  2006. await rep.create({foo: 15});
  2007. const result = await rep.delete({foo: {nin: [5, 10]}});
  2008. expect(result).to.be.eq(1);
  2009. const rawData = await MDB_CLIENT.db()
  2010. .collection('model')
  2011. .find()
  2012. .toArray();
  2013. expect(rawData).to.be.eql([
  2014. {_id: new ObjectId(created1[DEF_PK]), foo: 5},
  2015. {_id: new ObjectId(created2[DEF_PK]), foo: 10},
  2016. ]);
  2017. });
  2018. it('matches by the "between" operator', async function () {
  2019. const schema = createSchema();
  2020. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2021. const rep = schema.getRepository('model');
  2022. const created1 = await rep.create({foo: 5});
  2023. await rep.create({foo: 10});
  2024. await rep.create({foo: 15});
  2025. const created4 = await rep.create({foo: 20});
  2026. const result = await rep.delete({foo: {between: [9, 16]}});
  2027. expect(result).to.be.eq(2);
  2028. const rawData = await MDB_CLIENT.db()
  2029. .collection('model')
  2030. .find()
  2031. .toArray();
  2032. expect(rawData).to.be.eql([
  2033. {_id: new ObjectId(created1[DEF_PK]), foo: 5},
  2034. {_id: new ObjectId(created4[DEF_PK]), foo: 20},
  2035. ]);
  2036. });
  2037. it('matches by the "exists" operator', async function () {
  2038. const schema = createSchema();
  2039. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2040. const rep = schema.getRepository('model');
  2041. const created1 = await rep.create({});
  2042. await rep.create({foo: undefined});
  2043. await rep.create({foo: null});
  2044. await rep.create({foo: 10});
  2045. const result = await rep.delete({foo: {exists: true}});
  2046. expect(result).to.be.eq(3);
  2047. const rawData = await MDB_CLIENT.db()
  2048. .collection('model')
  2049. .find()
  2050. .toArray();
  2051. expect(rawData).to.be.eql([{_id: new ObjectId(created1[DEF_PK])}]);
  2052. });
  2053. it('matches by the "like" operator', async function () {
  2054. const schema = createSchema();
  2055. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2056. const rep = schema.getRepository('model');
  2057. const created1 = await rep.create({foo: 'lorem ipsum'});
  2058. await rep.create({foo: 'dolor sit amet'});
  2059. const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
  2060. await rep.create({foo: 'sit amet'});
  2061. const result = await rep.delete({foo: {like: 'sit amet'}});
  2062. expect(result).to.be.eq(2);
  2063. const rawData = await MDB_CLIENT.db()
  2064. .collection('model')
  2065. .find()
  2066. .toArray();
  2067. expect(rawData).to.be.eql([
  2068. {_id: new ObjectId(created1[DEF_PK]), foo: 'lorem ipsum'},
  2069. {_id: new ObjectId(created3[DEF_PK]), foo: 'DOLOR SIT AMET'},
  2070. ]);
  2071. });
  2072. it('matches by the "nlike" operator', async function () {
  2073. const schema = createSchema();
  2074. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2075. const rep = schema.getRepository('model');
  2076. await rep.create({foo: 'lorem ipsum'});
  2077. const created2 = await rep.create({foo: 'dolor sit amet'});
  2078. await rep.create({foo: 'DOLOR SIT AMET'});
  2079. const created4 = await rep.create({foo: 'sit amet'});
  2080. const result = await rep.delete({foo: {nlike: 'sit amet'}});
  2081. expect(result).to.be.eq(2);
  2082. const rawData = await MDB_CLIENT.db()
  2083. .collection('model')
  2084. .find()
  2085. .toArray();
  2086. expect(rawData).to.be.eql([
  2087. {_id: new ObjectId(created2[DEF_PK]), foo: 'dolor sit amet'},
  2088. {_id: new ObjectId(created4[DEF_PK]), foo: 'sit amet'},
  2089. ]);
  2090. });
  2091. it('matches by the "ilike" operator', async function () {
  2092. const schema = createSchema();
  2093. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2094. const rep = schema.getRepository('model');
  2095. const created1 = await rep.create({foo: 'lorem ipsum'});
  2096. await rep.create({foo: 'dolor sit amet'});
  2097. await rep.create({foo: 'DOLOR SIT AMET'});
  2098. await rep.create({foo: 'sit amet'});
  2099. const result = await rep.delete({foo: {ilike: 'sit amet'}});
  2100. expect(result).to.be.eq(3);
  2101. const rawData = await MDB_CLIENT.db()
  2102. .collection('model')
  2103. .find()
  2104. .toArray();
  2105. expect(rawData).to.be.eql([
  2106. {_id: new ObjectId(created1[DEF_PK]), foo: 'lorem ipsum'},
  2107. ]);
  2108. });
  2109. it('matches by the "nilike" operator', async function () {
  2110. const schema = createSchema();
  2111. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2112. const rep = schema.getRepository('model');
  2113. await rep.create({foo: 'lorem ipsum'});
  2114. const created2 = await rep.create({foo: 'dolor sit amet'});
  2115. const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
  2116. const created4 = await rep.create({foo: 'sit amet'});
  2117. const result = await rep.delete({foo: {nilike: 'sit amet'}});
  2118. expect(result).to.be.eq(1);
  2119. const rawData = await MDB_CLIENT.db()
  2120. .collection('model')
  2121. .find()
  2122. .toArray();
  2123. expect(rawData).to.be.eql([
  2124. {_id: new ObjectId(created2[DEF_PK]), foo: 'dolor sit amet'},
  2125. {_id: new ObjectId(created3[DEF_PK]), foo: 'DOLOR SIT AMET'},
  2126. {_id: new ObjectId(created4[DEF_PK]), foo: 'sit amet'},
  2127. ]);
  2128. });
  2129. it('matches by the "regexp" operator', async function () {
  2130. const schema = createSchema();
  2131. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2132. const rep = schema.getRepository('model');
  2133. const created1 = await rep.create({foo: 'lorem ipsum'});
  2134. await rep.create({foo: 'dolor sit amet'});
  2135. const created3 = await rep.create({foo: 'DOLOR SIT AMET'});
  2136. await rep.create({foo: 'sit amet'});
  2137. const result = await rep.delete({foo: {regexp: 'sit am+'}});
  2138. expect(result).to.be.eq(2);
  2139. const rawData = await MDB_CLIENT.db()
  2140. .collection('model')
  2141. .find()
  2142. .toArray();
  2143. expect(rawData).to.be.eql([
  2144. {_id: new ObjectId(created1[DEF_PK]), foo: 'lorem ipsum'},
  2145. {_id: new ObjectId(created3[DEF_PK]), foo: 'DOLOR SIT AMET'},
  2146. ]);
  2147. });
  2148. it('matches by the "regexp" operator with flags', async function () {
  2149. const schema = createSchema();
  2150. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2151. const rep = schema.getRepository('model');
  2152. const created1 = await rep.create({foo: 'lorem ipsum'});
  2153. await rep.create({foo: 'dolor sit amet'});
  2154. await rep.create({foo: 'DOLOR SIT AMET'});
  2155. await rep.create({foo: 'sit amet'});
  2156. const result = await rep.delete({foo: {regexp: 'sit am+', flags: 'i'}});
  2157. expect(result).to.be.eq(3);
  2158. const rawData = await MDB_CLIENT.db()
  2159. .collection('model')
  2160. .find()
  2161. .toArray();
  2162. expect(rawData).to.be.eql([
  2163. {_id: new ObjectId(created1[DEF_PK]), foo: 'lorem ipsum'},
  2164. ]);
  2165. });
  2166. });
  2167. });
  2168. describe('deleteById', function () {
  2169. it('returns false if a given identifier is not exist', async function () {
  2170. const schema = createSchema();
  2171. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2172. const rep = schema.getRepository('model');
  2173. const oid = new ObjectId();
  2174. const result = await rep.deleteById(oid);
  2175. expect(result).to.be.false;
  2176. });
  2177. it('returns true if an item has removed by a given identifier', async function () {
  2178. const schema = createSchema();
  2179. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2180. const rep = schema.getRepository('model');
  2181. const oid = new ObjectId();
  2182. await rep.create({[DEF_PK]: oid});
  2183. const result = await rep.deleteById(oid);
  2184. expect(result).to.be.true;
  2185. const rawData = await MDB_CLIENT.db()
  2186. .collection('model')
  2187. .find()
  2188. .toArray();
  2189. expect(rawData).to.be.empty;
  2190. });
  2191. });
  2192. describe('exists', function () {
  2193. it('returns false if a given identifier is not exist', async function () {
  2194. const schema = createSchema();
  2195. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2196. const rep = schema.getRepository('model');
  2197. const oid = new ObjectId();
  2198. const result = await rep.exists(oid);
  2199. expect(result).to.be.false;
  2200. });
  2201. it('returns true if a given identifier is exist', async function () {
  2202. const schema = createSchema();
  2203. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2204. const rep = schema.getRepository('model');
  2205. const oid = new ObjectId();
  2206. await rep.create({[DEF_PK]: oid});
  2207. const result = await rep.exists(oid);
  2208. expect(result).to.be.true;
  2209. });
  2210. });
  2211. describe('count', function () {
  2212. it('returns zero if nothing to count', async function () {
  2213. const schema = createSchema();
  2214. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2215. const rep = schema.getRepository('model');
  2216. const result = await rep.count();
  2217. expect(result).to.be.eq(0);
  2218. });
  2219. it('returns a number of table items', async function () {
  2220. const schema = createSchema();
  2221. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2222. const rep = schema.getRepository('model');
  2223. await rep.create({});
  2224. await rep.create({});
  2225. await rep.create({});
  2226. const result = await rep.count();
  2227. expect(result).to.be.eq(3);
  2228. });
  2229. describe('uses a where clause to count items', function () {
  2230. it('matches by a document subset', async function () {
  2231. const schema = createSchema();
  2232. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2233. const rep = schema.getRepository('model');
  2234. await rep.create({foo: 'a'});
  2235. await rep.create({foo: 'b'});
  2236. await rep.create({foo: 'b'});
  2237. const result = await rep.count({foo: 'b'});
  2238. expect(result).to.be.eql(2);
  2239. });
  2240. it('matches by the "eq" operator', async function () {
  2241. const schema = createSchema();
  2242. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2243. const rep = schema.getRepository('model');
  2244. await rep.create({foo: 5});
  2245. await rep.create({foo: 10});
  2246. await rep.create({foo: 10});
  2247. const result = await rep.count({foo: {eq: 10}});
  2248. expect(result).to.be.eql(2);
  2249. });
  2250. it('matches by the "neq" operator', async function () {
  2251. const schema = createSchema();
  2252. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2253. const rep = schema.getRepository('model');
  2254. await rep.create({foo: 5});
  2255. await rep.create({foo: 10});
  2256. await rep.create({foo: 10});
  2257. const result = await rep.count({foo: {neq: 10}});
  2258. expect(result).to.be.eq(1);
  2259. });
  2260. it('matches by the "gt" operator', async function () {
  2261. const schema = createSchema();
  2262. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2263. const rep = schema.getRepository('model');
  2264. await rep.create({foo: 5});
  2265. await rep.create({foo: 10});
  2266. await rep.create({foo: 15});
  2267. const result = await rep.count({foo: {gt: 10}});
  2268. expect(result).to.be.eq(1);
  2269. });
  2270. it('matches by the "lt" operator', async function () {
  2271. const schema = createSchema();
  2272. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2273. const rep = schema.getRepository('model');
  2274. await rep.create({foo: 5});
  2275. await rep.create({foo: 10});
  2276. await rep.create({foo: 15});
  2277. const result = await rep.count({foo: {lt: 10}});
  2278. expect(result).to.be.eq(1);
  2279. });
  2280. it('matches by the "gte" operator', async function () {
  2281. const schema = createSchema();
  2282. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2283. const rep = schema.getRepository('model');
  2284. await rep.create({foo: 5});
  2285. await rep.create({foo: 10});
  2286. await rep.create({foo: 15});
  2287. const result = await rep.count({foo: {gte: 10}});
  2288. expect(result).to.be.eq(2);
  2289. });
  2290. it('matches by the "lte" operator', async function () {
  2291. const schema = createSchema();
  2292. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2293. const rep = schema.getRepository('model');
  2294. await rep.create({foo: 5});
  2295. await rep.create({foo: 10});
  2296. await rep.create({foo: 15});
  2297. const result = await rep.count({foo: {lte: 10}});
  2298. expect(result).to.be.eq(2);
  2299. });
  2300. it('matches by the "inq" operator', async function () {
  2301. const schema = createSchema();
  2302. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2303. const rep = schema.getRepository('model');
  2304. await rep.create({foo: 5});
  2305. await rep.create({foo: 10});
  2306. await rep.create({foo: 15});
  2307. const result = await rep.count({foo: {inq: [5, 10]}});
  2308. expect(result).to.be.eq(2);
  2309. });
  2310. it('matches by the "nin" operator', async function () {
  2311. const schema = createSchema();
  2312. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2313. const rep = schema.getRepository('model');
  2314. await rep.create({foo: 5});
  2315. await rep.create({foo: 10});
  2316. await rep.create({foo: 15});
  2317. const result = await rep.count({foo: {nin: [5, 10]}});
  2318. expect(result).to.be.eq(1);
  2319. });
  2320. it('matches by the "between" operator', async function () {
  2321. const schema = createSchema();
  2322. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2323. const rep = schema.getRepository('model');
  2324. await rep.create({foo: 5});
  2325. await rep.create({foo: 10});
  2326. await rep.create({foo: 15});
  2327. await rep.create({foo: 20});
  2328. const result = await rep.count({foo: {between: [9, 16]}});
  2329. expect(result).to.be.eq(2);
  2330. });
  2331. it('matches by the "exists" operator', async function () {
  2332. const schema = createSchema();
  2333. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2334. const rep = schema.getRepository('model');
  2335. await rep.create({});
  2336. await rep.create({foo: undefined});
  2337. await rep.create({foo: null});
  2338. await rep.create({foo: 10});
  2339. const result1 = await rep.count({foo: {exists: true}});
  2340. expect(result1).to.be.eq(3);
  2341. });
  2342. it('matches by the "like" operator', async function () {
  2343. const schema = createSchema();
  2344. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2345. const rep = schema.getRepository('model');
  2346. await rep.create({foo: 'lorem ipsum'});
  2347. await rep.create({foo: 'dolor sit amet'});
  2348. await rep.create({foo: 'DOLOR SIT AMET'});
  2349. await rep.create({foo: 'sit amet'});
  2350. const result = await rep.count({foo: {like: 'sit amet'}});
  2351. expect(result).to.be.eql(2);
  2352. });
  2353. it('matches by the "nlike" operator', async function () {
  2354. const schema = createSchema();
  2355. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2356. const rep = schema.getRepository('model');
  2357. await rep.create({foo: 'lorem ipsum'});
  2358. await rep.create({foo: 'dolor sit amet'});
  2359. await rep.create({foo: 'DOLOR SIT AMET'});
  2360. await rep.create({foo: 'sit amet'});
  2361. const result = await rep.count({foo: {nlike: 'sit amet'}});
  2362. expect(result).to.be.eql(2);
  2363. });
  2364. it('matches by the "ilike" operator', async function () {
  2365. const schema = createSchema();
  2366. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2367. const rep = schema.getRepository('model');
  2368. await rep.create({foo: 'lorem ipsum'});
  2369. await rep.create({foo: 'dolor sit amet'});
  2370. await rep.create({foo: 'DOLOR SIT AMET'});
  2371. await rep.create({foo: 'sit amet'});
  2372. const result = await rep.count({foo: {ilike: 'sit amet'}});
  2373. expect(result).to.be.eql(3);
  2374. });
  2375. it('matches by the "nilike" operator', async function () {
  2376. const schema = createSchema();
  2377. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2378. const rep = schema.getRepository('model');
  2379. await rep.create({foo: 'lorem ipsum'});
  2380. await rep.create({foo: 'dolor sit amet'});
  2381. await rep.create({foo: 'DOLOR SIT AMET'});
  2382. await rep.create({foo: 'sit amet'});
  2383. const result = await rep.count({foo: {nilike: 'sit amet'}});
  2384. expect(result).to.be.eql(1);
  2385. });
  2386. it('matches by the "regexp" operator', async function () {
  2387. const schema = createSchema();
  2388. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2389. const rep = schema.getRepository('model');
  2390. await rep.create({foo: 'lorem ipsum'});
  2391. await rep.create({foo: 'dolor sit amet'});
  2392. await rep.create({foo: 'DOLOR SIT AMET'});
  2393. await rep.create({foo: 'sit amet'});
  2394. const result = await rep.count({foo: {regexp: 'sit am+'}});
  2395. expect(result).to.be.eql(2);
  2396. });
  2397. it('matches by the "regexp" operator with flags', async function () {
  2398. const schema = createSchema();
  2399. schema.defineModel({name: 'model', datasource: 'mongodb'});
  2400. const rep = schema.getRepository('model');
  2401. await rep.create({foo: 'lorem ipsum'});
  2402. await rep.create({foo: 'dolor sit amet'});
  2403. await rep.create({foo: 'DOLOR SIT AMET'});
  2404. await rep.create({foo: 'sit amet'});
  2405. const result = await rep.count({foo: {regexp: 'sit am+', flags: 'i'}});
  2406. expect(result).to.be.eql(3);
  2407. });
  2408. });
  2409. });
  2410. });