mongodb-adapter.spec.js 103 KB

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