@e22m4u/js-repository-mongodb-adapter 0.0.14

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