@sprucelabs/data-stores 19.1.67 → 19.2.0

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.
Files changed (33) hide show
  1. package/build/.spruce/errors/dataStores/invalidConnectionStringScheme.schema.d.ts +3 -0
  2. package/build/.spruce/errors/dataStores/invalidConnectionStringScheme.schema.js +18 -0
  3. package/build/.spruce/errors/errors.types.d.ts +19 -0
  4. package/build/.spruce/errors/options.types.d.ts +4 -1
  5. package/build/databases/MongoDatabase.d.ts +2 -6
  6. package/build/errors/SpruceError.js +3 -0
  7. package/build/errors/invalidConnectionStringSchema.builder.d.ts +11 -0
  8. package/build/errors/invalidConnectionStringSchema.builder.js +13 -0
  9. package/build/esm/.spruce/errors/errors.types.d.ts +19 -0
  10. package/build/esm/.spruce/errors/options.types.d.ts +4 -1
  11. package/build/esm/databases/MongoDatabase.d.ts +2 -6
  12. package/build/esm/errors/SpruceError.js +3 -0
  13. package/build/esm/errors/invalidConnectionStringSchema.builder.d.ts +11 -0
  14. package/build/esm/errors/invalidConnectionStringSchema.builder.js +11 -0
  15. package/build/esm/factories/DatabaseFactory.d.ts +4 -1
  16. package/build/esm/factories/DatabaseFactory.js +18 -4
  17. package/build/esm/index.d.ts +1 -0
  18. package/build/esm/index.js +1 -0
  19. package/build/esm/tests/databaseAssertUtil.d.ts +75 -0
  20. package/build/esm/tests/databaseAssertUtil.js +1339 -0
  21. package/build/esm/tests/pluckAssertionMethods.d.ts +2 -0
  22. package/build/esm/tests/pluckAssertionMethods.js +3 -0
  23. package/build/esm/types/database.types.d.ts +5 -0
  24. package/build/factories/DatabaseFactory.d.ts +4 -1
  25. package/build/factories/DatabaseFactory.js +18 -4
  26. package/build/index.d.ts +1 -0
  27. package/build/index.js +3 -1
  28. package/build/tests/databaseAssertUtil.d.ts +75 -0
  29. package/build/tests/databaseAssertUtil.js +1199 -0
  30. package/build/tests/pluckAssertionMethods.d.ts +2 -0
  31. package/build/tests/pluckAssertionMethods.js +6 -0
  32. package/build/types/database.types.d.ts +5 -0
  33. package/package.json +3 -3
@@ -0,0 +1,1339 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { assertOptions } from '@sprucelabs/schema';
11
+ import { assert } from '@sprucelabs/test-utils';
12
+ import { errorAssert } from '@sprucelabs/test-utils';
13
+ import SpruceError from '../errors/SpruceError.js';
14
+ import generateId from '../utilities/generateId.js';
15
+ import pluckAssertionMethods from './pluckAssertionMethods.js';
16
+ const databaseAssertUtil = {
17
+ collectionName: 'test_collection',
18
+ runSuite(connect) {
19
+ return __awaiter(this, void 0, void 0, function* () {
20
+ assertOptions({ connect }, ['connect']);
21
+ const methods = pluckAssertionMethods(this);
22
+ //@ts-ignore
23
+ yield Promise.all(methods.map((method) => this[method](connect)));
24
+ });
25
+ },
26
+ _getFilteredIndexes(db) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ return this._filterIdIndex(yield db.getIndexes(this.collectionName));
29
+ });
30
+ },
31
+ _filterIdIndex(allIndexes) {
32
+ return allIndexes.filter((i) => i[0] !== '_id');
33
+ },
34
+ _assertUpdateUpdatedRightNumberOfRecords(db, search, updates, expectedUpdateCount) {
35
+ return __awaiter(this, void 0, void 0, function* () {
36
+ const updatedCount = yield db.update(this.collectionName, search, updates);
37
+ assert.isEqual(updatedCount, expectedUpdateCount);
38
+ const count = yield db.count(this.collectionName, updates);
39
+ assert.isEqual(count, expectedUpdateCount);
40
+ });
41
+ },
42
+ assertCanSortDesc(connect) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ const db = yield connect();
45
+ yield db.createOne(this.collectionName, { name: 'second', count: 1 });
46
+ yield db.createOne(this.collectionName, { name: 'third', count: 5 });
47
+ yield db.createOne(this.collectionName, { name: 'first', count: -1 });
48
+ const results = yield db.find(this.collectionName, {}, {
49
+ sort: [
50
+ {
51
+ field: 'count',
52
+ direction: 'desc',
53
+ },
54
+ ],
55
+ });
56
+ assert.isEqual(results[0].name, 'third');
57
+ assert.isEqual(results[1].name, 'second');
58
+ assert.isEqual(results[2].name, 'first');
59
+ const result = yield db.findOne(this.collectionName, undefined, {
60
+ sort: [{ field: 'count', direction: 'desc' }],
61
+ });
62
+ assert.isTruthy(result);
63
+ assert.isEqual(result.name, 'third');
64
+ yield this.shutdown(db);
65
+ });
66
+ },
67
+ assertCanSortAsc(connect) {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ const db = yield connect();
70
+ yield db.createOne(this.collectionName, { name: 'second', count: 1 });
71
+ yield db.createOne(this.collectionName, { name: 'third', count: 5 });
72
+ yield db.createOne(this.collectionName, { name: 'first', count: -1 });
73
+ const results = yield db.find(this.collectionName, {}, {
74
+ sort: [
75
+ {
76
+ field: 'count',
77
+ direction: 'asc',
78
+ },
79
+ ],
80
+ });
81
+ assert.isEqual(results[0].name, 'first');
82
+ assert.isEqual(results[1].name, 'second');
83
+ assert.isEqual(results[2].name, 'third');
84
+ const result = yield db.findOne(this.collectionName, undefined, {
85
+ sort: [{ field: 'count', direction: 'asc' }],
86
+ });
87
+ assert.isTruthy(result);
88
+ assert.isEqual(result.name, 'first');
89
+ yield this.shutdown(db);
90
+ });
91
+ },
92
+ assertInsertingGeneratesId(connect) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ const db = yield connect();
95
+ const inserted = yield db.createOne(this.collectionName, {
96
+ name: 'first',
97
+ });
98
+ assert.isTruthy(inserted);
99
+ assert.isString(inserted.id);
100
+ assert.isEqual(inserted.name, 'first');
101
+ yield this.shutdown(db);
102
+ });
103
+ },
104
+ assertThrowsWhenUpdatingRecordNotFound(connect) {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ const db = yield connect();
107
+ const err = (yield assert.doesThrowAsync(() => db.updateOne('unknown', { id: db.generateId() }, { foo: 'bar' })));
108
+ errorAssert.assertError(err, 'RECORD_NOT_FOUND');
109
+ yield this.shutdown(db);
110
+ });
111
+ },
112
+ shutdown(db) {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ yield db.dropDatabase();
115
+ yield db.close();
116
+ });
117
+ },
118
+ assertCanCreateUniqueIndex(connect) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ const db = yield connect();
121
+ yield db.createUniqueIndex(this.collectionName, ['uniqueField']);
122
+ let indexes = yield db.getUniqueIndexes(this.collectionName);
123
+ assert.isLength(indexes, 1);
124
+ assert.isLength(indexes[0], 1);
125
+ assert.isEqual(indexes[0][0], 'uniqueField');
126
+ yield db.createUniqueIndex(this.collectionName, ['uniqueField2']);
127
+ indexes = yield db.getUniqueIndexes(this.collectionName);
128
+ assert.isLength(indexes, 2);
129
+ assert.isEqual(indexes[1][0], 'uniqueField2');
130
+ yield db.createUniqueIndex(this.collectionName, [
131
+ 'uniqueField3',
132
+ 'uniqueField4',
133
+ ]);
134
+ indexes = yield db.getUniqueIndexes(this.collectionName);
135
+ assert.isLength(indexes, 3);
136
+ assert.isEqual(indexes[2][0], 'uniqueField3');
137
+ assert.isEqual(indexes[2][1], 'uniqueField4');
138
+ yield this.shutdown(db);
139
+ });
140
+ },
141
+ assertHasNoUniqueIndexToStart(connect) {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ const db = yield connect();
144
+ const indexes = yield db.getUniqueIndexes(this.collectionName);
145
+ assert.isLength(indexes, 0);
146
+ yield this.shutdown(db);
147
+ });
148
+ },
149
+ assertCanShutdown(connect) {
150
+ return __awaiter(this, void 0, void 0, function* () {
151
+ const db = yield connect();
152
+ const id = db.generateId();
153
+ const created = yield db.upsertOne(this.collectionName, { id }, {
154
+ id,
155
+ name: 'first',
156
+ });
157
+ assert.isTruthy(created);
158
+ assert.isEqual(created.name, 'first');
159
+ assert.isEqual(created.id, id);
160
+ const upserted = yield db.upsertOne(this.collectionName, { id }, { name: 'second' });
161
+ yield db.upsertOne(this.collectionName, { id: db.generateId() }, { name: 'second' });
162
+ assert.isTruthy(upserted);
163
+ assert.isEqual(created.id, upserted.id);
164
+ assert.isEqual(upserted.name, 'second');
165
+ const upserted2 = yield db.upsertOne(this.collectionName, { id }, { name: 'third' });
166
+ assert.isTruthy(upserted2);
167
+ assert.isEqual(upserted2.name, 'third');
168
+ yield this.shutdown(db);
169
+ });
170
+ },
171
+ assertCanDeleteOne(connect) {
172
+ return __awaiter(this, void 0, void 0, function* () {
173
+ const db = yield connect();
174
+ yield db.create(this.collectionName, [
175
+ {
176
+ name: 'a record',
177
+ },
178
+ {
179
+ name: 'a record',
180
+ },
181
+ {
182
+ name: 'a record',
183
+ },
184
+ ]);
185
+ yield db.deleteOne(this.collectionName, { name: 'a record' });
186
+ const count = yield db.count(this.collectionName);
187
+ assert.isEqual(count, 2);
188
+ yield this.shutdown(db);
189
+ });
190
+ },
191
+ assertCanDeleteRecord(connect) {
192
+ return __awaiter(this, void 0, void 0, function* () {
193
+ const db = yield connect();
194
+ const created = yield db.createOne(this.collectionName, {
195
+ id: db.generateId(),
196
+ name: 'first',
197
+ });
198
+ const matchedBeforeDelete = yield db.findOne(this.collectionName, {
199
+ id: created.id,
200
+ });
201
+ assert.isTruthy(matchedBeforeDelete);
202
+ assert.isEqual(matchedBeforeDelete.id, created.id);
203
+ const numDeleted = yield db.delete(this.collectionName, {
204
+ id: matchedBeforeDelete.id,
205
+ });
206
+ assert.isEqual(numDeleted, 1);
207
+ const matchedAfterDelete = yield db.findOne(this.collectionName, {
208
+ id: created.id,
209
+ });
210
+ assert.isFalsy(matchedAfterDelete);
211
+ yield db.create(this.collectionName, [
212
+ {
213
+ name: 'a record',
214
+ },
215
+ {
216
+ name: 'a record',
217
+ },
218
+ {
219
+ name: 'a record',
220
+ },
221
+ ]);
222
+ const manyDeleted = yield db.delete(this.collectionName, {});
223
+ assert.isEqual(manyDeleted, 3);
224
+ yield this.shutdown(db);
225
+ });
226
+ },
227
+ assertCanLimitResultsToZero(connect) {
228
+ return __awaiter(this, void 0, void 0, function* () {
229
+ const db = yield connect();
230
+ yield db.createOne(this.collectionName, {
231
+ id: db.generateId(),
232
+ name: 'first',
233
+ });
234
+ yield db.createOne(this.collectionName, {
235
+ id: db.generateId(),
236
+ name: 'second',
237
+ });
238
+ yield db.createOne(this.collectionName, {
239
+ id: db.generateId(),
240
+ name: 'third',
241
+ });
242
+ const results = yield db.find(this.collectionName, undefined, {
243
+ limit: 0,
244
+ sort: [{ field: 'name', direction: 'asc' }],
245
+ });
246
+ assert.isLength(results, 0);
247
+ yield this.shutdown(db);
248
+ });
249
+ },
250
+ assertCanLimitResults(connect) {
251
+ return __awaiter(this, void 0, void 0, function* () {
252
+ const db = yield connect();
253
+ yield db.createOne(this.collectionName, {
254
+ id: db.generateId(),
255
+ name: 'first',
256
+ });
257
+ yield db.createOne(this.collectionName, {
258
+ id: db.generateId(),
259
+ name: 'second',
260
+ });
261
+ yield db.createOne(this.collectionName, {
262
+ id: db.generateId(),
263
+ name: 'third',
264
+ });
265
+ const results = yield db.find(this.collectionName, undefined, {
266
+ limit: 2,
267
+ sort: [{ field: 'name', direction: 'asc' }],
268
+ });
269
+ assert.isLength(results, 2);
270
+ assert.isEqual(results[0].name, 'first');
271
+ assert.isEqual(results[1].name, 'second');
272
+ yield this.shutdown(db);
273
+ });
274
+ },
275
+ assertFindOneOnEmptyDatabaseReturnsNull(connect) {
276
+ return __awaiter(this, void 0, void 0, function* () {
277
+ const db = yield connect();
278
+ const results = yield db.findOne(this.collectionName, { id: '111' });
279
+ assert.isFalsy(results);
280
+ yield this.shutdown(db);
281
+ });
282
+ },
283
+ assertEmptyDatabaseReturnsEmptyArray(connect) {
284
+ return __awaiter(this, void 0, void 0, function* () {
285
+ const db = yield connect();
286
+ const results = yield db.find(this.collectionName, { id: '111' });
287
+ assert.isLength(results, 0);
288
+ yield this.shutdown(db);
289
+ });
290
+ },
291
+ assertCanPushOntoArrayValue(connect) {
292
+ return __awaiter(this, void 0, void 0, function* () {
293
+ const db = yield connect();
294
+ const inserted = yield db.createOne(this.collectionName, {
295
+ id: db.generateId(),
296
+ names: ['first'],
297
+ });
298
+ const updated = yield db.updateOne(this.collectionName, { id: inserted.id }, {
299
+ $push: { names: 'second' },
300
+ });
301
+ assert.isEqualDeep(updated.names, ['first', 'second']);
302
+ const matched = yield db.findOne(this.collectionName, { id: updated.id });
303
+ assert.isTruthy(matched);
304
+ assert.isEqualDeep(matched.names, ['first', 'second']);
305
+ yield this.shutdown(db);
306
+ });
307
+ },
308
+ assertCanCreateMany(connect) {
309
+ return __awaiter(this, void 0, void 0, function* () {
310
+ const db = yield connect();
311
+ const values = [
312
+ { first: 'ry' },
313
+ { first: 'tay' },
314
+ { first: 'bill' },
315
+ { first: 'bob' },
316
+ ];
317
+ const results = yield db.create(this.collectionName, values);
318
+ assert.isLength(results, values.length);
319
+ for (const val of values) {
320
+ assert.doesInclude(results, val);
321
+ }
322
+ yield this.shutdown(db);
323
+ });
324
+ },
325
+ assertCanUpdateMany(connect) {
326
+ return __awaiter(this, void 0, void 0, function* () {
327
+ const db = yield connect();
328
+ yield db.create(this.collectionName, [
329
+ {
330
+ name: 'one',
331
+ number: 1,
332
+ },
333
+ {
334
+ name: 'two',
335
+ number: 1,
336
+ },
337
+ {
338
+ name: 'three',
339
+ number: 1,
340
+ },
341
+ ]);
342
+ yield this._assertUpdateUpdatedRightNumberOfRecords(db, { name: 'one' }, { name: 'one-updated' }, 1);
343
+ yield this._assertUpdateUpdatedRightNumberOfRecords(db, { number: 1 }, { number: 2 }, 3);
344
+ yield this.shutdown(db);
345
+ });
346
+ },
347
+ assertCanQueryWithOr(connect) {
348
+ return __awaiter(this, void 0, void 0, function* () {
349
+ const db = yield connect();
350
+ const id1 = generateId();
351
+ const id2 = generateId();
352
+ yield db.create(this.collectionName, [
353
+ {
354
+ isPublic: true,
355
+ id: id1,
356
+ },
357
+ {
358
+ id: id2,
359
+ },
360
+ ]);
361
+ const matches = yield db.find(this.collectionName, {
362
+ $or: [{ isPublic: true }, { id: id2 }],
363
+ });
364
+ assert.isLength(matches, 2);
365
+ yield this.shutdown(db);
366
+ });
367
+ },
368
+ assertCanUpdate(connect) {
369
+ return __awaiter(this, void 0, void 0, function* () {
370
+ const db = yield connect();
371
+ const inserted = yield db.createOne(this.collectionName, {
372
+ id: db.generateId(),
373
+ name: 'first',
374
+ });
375
+ assert.isString(inserted.id);
376
+ assert.isEqual(inserted.name, 'first');
377
+ const updated = yield db.updateOne(this.collectionName, { id: inserted.id }, {
378
+ name: 'updated',
379
+ });
380
+ assert.isEqual(updated.id, inserted.id);
381
+ assert.isEqual(updated.name, 'updated');
382
+ yield this.shutdown(db);
383
+ });
384
+ },
385
+ assertCanSortById(connect) {
386
+ return __awaiter(this, void 0, void 0, function* () {
387
+ const db = yield connect();
388
+ yield db.createOne(this.collectionName, {
389
+ id: db.generateId(),
390
+ name: 'first',
391
+ });
392
+ yield db.createOne(this.collectionName, {
393
+ id: db.generateId(),
394
+ name: 'second',
395
+ });
396
+ yield db.createOne(this.collectionName, {
397
+ id: db.generateId(),
398
+ name: 'third',
399
+ });
400
+ const results = yield db.find(this.collectionName, {}, {
401
+ sort: [
402
+ {
403
+ field: 'id',
404
+ direction: 'desc',
405
+ },
406
+ ],
407
+ });
408
+ assert.isEqual(results[0].name, 'third');
409
+ assert.isEqual(results[1].name, 'second');
410
+ assert.isEqual(results[2].name, 'first');
411
+ const result = yield db.findOne(this.collectionName, undefined, {
412
+ sort: [{ field: 'id', direction: 'desc' }],
413
+ });
414
+ assert.isTruthy(result);
415
+ assert.isEqual(result.name, 'third');
416
+ yield this.shutdown(db);
417
+ });
418
+ },
419
+ assertCanCreateMultiFieldUniqueIndex(connect) {
420
+ return __awaiter(this, void 0, void 0, function* () {
421
+ const db = yield connect();
422
+ yield db.createUniqueIndex(this.collectionName, [
423
+ 'uniqueField',
424
+ 'uniqueField2',
425
+ ]);
426
+ yield db.createOne(this.collectionName, {
427
+ uniqueField: 'hello world',
428
+ uniqueField2: 'hello again',
429
+ });
430
+ yield db.createOne(this.collectionName, {
431
+ uniqueField: 'hello world',
432
+ uniqueField2: 'unique',
433
+ });
434
+ let err = (yield assert.doesThrowAsync(() => db.createOne(this.collectionName, {
435
+ uniqueField: 'hello world',
436
+ uniqueField2: 'unique',
437
+ })));
438
+ errorAssert.assertError(err, 'DUPLICATE_RECORD', {
439
+ collectionName: this.collectionName,
440
+ duplicateFields: ['uniqueField', 'uniqueField2'],
441
+ duplicateValues: ['hello world', 'unique'],
442
+ action: 'create',
443
+ });
444
+ yield db.upsertOne(this.collectionName, {
445
+ uniqueField: 'hello world',
446
+ uniqueField2: 'unique',
447
+ }, {
448
+ uniqueField: 'hello world',
449
+ uniqueField2: 'unique2',
450
+ });
451
+ err = (yield assert.doesThrowAsync(() => db.upsertOne(this.collectionName, {
452
+ uniqueField: 'hello world',
453
+ uniqueField2: 'unique2',
454
+ }, {
455
+ uniqueField: 'hello world',
456
+ uniqueField2: 'hello again',
457
+ })));
458
+ errorAssert.assertError(err, 'DUPLICATE_RECORD', {
459
+ collectionName: this.collectionName,
460
+ duplicateFields: ['uniqueField', 'uniqueField2'],
461
+ duplicateValues: ['hello world', 'hello again'],
462
+ action: 'upsertOne',
463
+ });
464
+ yield this.shutdown(db);
465
+ });
466
+ },
467
+ assertSettingUniqueIndexViolationThrowsSpruceError(connect) {
468
+ return __awaiter(this, void 0, void 0, function* () {
469
+ const db = yield connect();
470
+ yield db.createUniqueIndex(this.collectionName, ['randomUniqueField']);
471
+ yield db.createOne(this.collectionName, {
472
+ uniqueField: 'hello world',
473
+ randomUniqueField: '1',
474
+ });
475
+ yield db.createOne(this.collectionName, {
476
+ uniqueField: 'hello world',
477
+ randomUniqueField: '2',
478
+ });
479
+ let err = yield assert.doesThrowAsync(() => db.syncUniqueIndexes(this.collectionName, [['uniqueField']]));
480
+ assert.isTrue(err instanceof SpruceError);
481
+ errorAssert.assertError(err, 'DUPLICATE_KEY');
482
+ yield this.shutdown(db);
483
+ });
484
+ },
485
+ assertDuplicateKeyThrowsOnInsert(connect) {
486
+ return __awaiter(this, void 0, void 0, function* () {
487
+ const db = yield connect();
488
+ yield db.createUniqueIndex(this.collectionName, ['uniqueField']);
489
+ yield db.createOne(this.collectionName, {
490
+ uniqueField: 'hello world',
491
+ });
492
+ let err = yield assert.doesThrowAsync(() => db.createOne(this.collectionName, { uniqueField: 'hello world' }));
493
+ assert.isTrue(err instanceof SpruceError);
494
+ yield this.shutdown(db);
495
+ });
496
+ },
497
+ assertSyncingIndexesDoesNotAddAndRemove(connect) {
498
+ return __awaiter(this, void 0, void 0, function* () {
499
+ const db = yield connect();
500
+ yield db.createUniqueIndex(this.collectionName, ['otherField']);
501
+ yield db.createUniqueIndex(this.collectionName, ['someField']);
502
+ db.createUniqueIndex = () => {
503
+ throw new Error('Should not have been called');
504
+ };
505
+ db.dropIndex = () => {
506
+ throw new Error('Should not have been called');
507
+ };
508
+ yield db.syncUniqueIndexes(this.collectionName, [
509
+ ['someField'],
510
+ ['otherField'],
511
+ ]);
512
+ yield this.shutdown(db);
513
+ });
514
+ },
515
+ assertSyncingUniqueIndexesRemovesExtraIndexes(connect) {
516
+ return __awaiter(this, void 0, void 0, function* () {
517
+ const db = yield connect();
518
+ yield db.syncUniqueIndexes(this.collectionName, [
519
+ ['uniqueField'],
520
+ ['someField'],
521
+ ['otherField', 'otherField2'],
522
+ ]);
523
+ let indexes = yield db.getUniqueIndexes(this.collectionName);
524
+ assert.isLength(indexes, 3);
525
+ yield db.syncUniqueIndexes(this.collectionName, [['uniqueField']]);
526
+ indexes = yield db.getUniqueIndexes(this.collectionName);
527
+ assert.isLength(indexes, 1);
528
+ assert.isEqual(indexes[0][0], 'uniqueField');
529
+ yield this.shutdown(db);
530
+ });
531
+ },
532
+ assertSyncingUniqueIndexsSkipsExistingIndexs(connect) {
533
+ return __awaiter(this, void 0, void 0, function* () {
534
+ const db = yield connect();
535
+ yield db.syncUniqueIndexes(this.collectionName, [['uniqueField']]);
536
+ let indexes = yield db.getUniqueIndexes(this.collectionName);
537
+ assert.isLength(indexes, 1);
538
+ yield db.syncUniqueIndexes(this.collectionName, [
539
+ ['uniqueField'],
540
+ ['someField'],
541
+ ['otherField', 'otherField2'],
542
+ ]);
543
+ indexes = yield db.getUniqueIndexes(this.collectionName);
544
+ assert.isLength(indexes, 3);
545
+ yield this.shutdown(db);
546
+ });
547
+ },
548
+ assertSyncingUniqueIndexsAddsMissingIndexes(connect) {
549
+ return __awaiter(this, void 0, void 0, function* () {
550
+ const db = yield connect();
551
+ yield db.syncUniqueIndexes(this.collectionName, [['uniqueField']]);
552
+ let indexes = yield db.getUniqueIndexes(this.collectionName);
553
+ assert.isLength(indexes, 1);
554
+ yield db.syncUniqueIndexes(this.collectionName, [['someField']]);
555
+ indexes = yield db.getUniqueIndexes(this.collectionName);
556
+ assert.isLength(indexes, 1);
557
+ yield db.syncUniqueIndexes(this.collectionName, [
558
+ ['uniqueField'],
559
+ ['someField'],
560
+ ]);
561
+ indexes = yield db.getUniqueIndexes(this.collectionName);
562
+ assert.isLength(indexes, 2);
563
+ yield this.shutdown(db);
564
+ });
565
+ },
566
+ assertCantDropCompoundUniqueIndexThatDoesntExist(connect) {
567
+ return __awaiter(this, void 0, void 0, function* () {
568
+ const db = yield connect();
569
+ yield db.createUniqueIndex(this.collectionName, [
570
+ 'someField',
571
+ 'someOtherField',
572
+ ]);
573
+ const err = yield assert.doesThrowAsync(() => db.dropIndex(this.collectionName, ['uniqueField', 'someOtherField']));
574
+ errorAssert.assertError(err, 'INDEX_NOT_FOUND');
575
+ yield this.shutdown(db);
576
+ });
577
+ },
578
+ assertCantDropIndexWhenNoIndexExists(connect) {
579
+ return __awaiter(this, void 0, void 0, function* () {
580
+ const db = yield connect();
581
+ const err = yield assert.doesThrowAsync(() => db.dropIndex(this.collectionName, ['someOtherField']));
582
+ errorAssert.assertError(err, 'INDEX_NOT_FOUND');
583
+ yield this.shutdown(db);
584
+ });
585
+ },
586
+ assertCantDropUniqueIndexThatDoesntExist(connect) {
587
+ return __awaiter(this, void 0, void 0, function* () {
588
+ const db = yield connect();
589
+ yield db.createUniqueIndex(this.collectionName, ['someField']);
590
+ const err = yield assert.doesThrowAsync(() => db.dropIndex(this.collectionName, ['someOtherField']));
591
+ errorAssert.assertError(err, 'INDEX_NOT_FOUND');
592
+ yield this.shutdown(db);
593
+ });
594
+ },
595
+ assertCanDropCompoundUniqueIndex(connect) {
596
+ return __awaiter(this, void 0, void 0, function* () {
597
+ const db = yield connect();
598
+ yield db.createUniqueIndex(this.collectionName, ['someField', 'otherField']);
599
+ yield db.dropIndex(this.collectionName, ['someField', 'otherField']);
600
+ let indexes = yield db.getUniqueIndexes(this.collectionName);
601
+ assert.isLength(indexes, 0);
602
+ yield db.createUniqueIndex(this.collectionName, ['someField', 'someField2']);
603
+ yield db.createUniqueIndex(this.collectionName, ['someField', 'someField3']);
604
+ yield db.dropIndex(this.collectionName, ['someField', 'someField3']);
605
+ indexes = yield db.getUniqueIndexes(this.collectionName);
606
+ assert.isLength(indexes, 1);
607
+ yield this.shutdown(db);
608
+ });
609
+ },
610
+ assertCanDropUniqueIndex(connect) {
611
+ return __awaiter(this, void 0, void 0, function* () {
612
+ const db = yield connect();
613
+ yield db.createUniqueIndex(this.collectionName, ['someField']);
614
+ yield db.dropIndex(this.collectionName, ['someField']);
615
+ let indexes = yield db.getUniqueIndexes(this.collectionName);
616
+ assert.isLength(indexes, 0);
617
+ yield db.createUniqueIndex(this.collectionName, ['someField2']);
618
+ yield db.createUniqueIndex(this.collectionName, ['someField3']);
619
+ yield db.dropIndex(this.collectionName, ['someField3']);
620
+ indexes = yield db.getUniqueIndexes(this.collectionName);
621
+ assert.isLength(indexes, 1);
622
+ yield this.shutdown(db);
623
+ });
624
+ },
625
+ assertCantCreateUniqueIndexTwice(connect) {
626
+ return __awaiter(this, void 0, void 0, function* () {
627
+ const db = yield connect();
628
+ yield db.createUniqueIndex(this.collectionName, ['uniqueField']);
629
+ let indexes = yield db.getUniqueIndexes(this.collectionName);
630
+ assert.isLength(indexes, 1);
631
+ const err = yield assert.doesThrowAsync(() => db.createUniqueIndex(this.collectionName, ['uniqueField']));
632
+ errorAssert.assertError(err, 'INDEX_EXISTS');
633
+ yield this.shutdown(db);
634
+ });
635
+ },
636
+ assertSyncingUniqueIndexesIsRaceProof(connect) {
637
+ return __awaiter(this, void 0, void 0, function* () {
638
+ const db = yield connect();
639
+ const syncs = [
640
+ db.syncUniqueIndexes(this.collectionName, [
641
+ ['otherField', 'otherField2'],
642
+ ]),
643
+ db.syncUniqueIndexes(this.collectionName, [
644
+ ['otherField', 'otherField2'],
645
+ ]),
646
+ db.syncUniqueIndexes(this.collectionName, [
647
+ ['otherField', 'otherField2'],
648
+ ]),
649
+ db.syncUniqueIndexes(this.collectionName, [
650
+ ['otherField', 'otherField2'],
651
+ ]),
652
+ db.syncUniqueIndexes(this.collectionName, [
653
+ ['otherField', 'otherField2'],
654
+ ]),
655
+ db.syncUniqueIndexes(this.collectionName, [
656
+ ['otherField', 'otherField2'],
657
+ ]),
658
+ db.syncUniqueIndexes(this.collectionName, [
659
+ ['otherField', 'otherField2'],
660
+ ]),
661
+ db.syncUniqueIndexes(this.collectionName, [
662
+ ['otherField', 'otherField2'],
663
+ ]),
664
+ db.syncUniqueIndexes(this.collectionName, [
665
+ ['otherField', 'otherField2'],
666
+ ]),
667
+ db.syncUniqueIndexes(this.collectionName, [
668
+ ['otherField', 'otherField2'],
669
+ ]),
670
+ ];
671
+ yield Promise.all(syncs);
672
+ yield this.shutdown(db);
673
+ });
674
+ },
675
+ assertUniqueIndexBlocksDuplicates(connect) {
676
+ return __awaiter(this, void 0, void 0, function* () {
677
+ const db = yield connect();
678
+ yield db.createUniqueIndex(this.collectionName, ['uniqueField']);
679
+ yield db.createOne(this.collectionName, {
680
+ uniqueField: 'hello world',
681
+ });
682
+ let err = (yield assert.doesThrowAsync(() => db.createOne(this.collectionName, { uniqueField: 'hello world' })));
683
+ errorAssert.assertError(err, 'DUPLICATE_RECORD', {
684
+ collectionName: this.collectionName,
685
+ duplicateFields: ['uniqueField'],
686
+ duplicateValues: ['hello world'],
687
+ action: 'create',
688
+ });
689
+ const created = yield db.createOne(this.collectionName, {
690
+ uniqueField: 'pass',
691
+ });
692
+ err = (yield assert.doesThrowAsync(() => db.updateOne(this.collectionName, { id: created.id }, { uniqueField: 'hello world' })));
693
+ errorAssert.assertError(err, 'DUPLICATE_RECORD', {
694
+ collectionName: this.collectionName,
695
+ duplicateFields: ['uniqueField'],
696
+ duplicateValues: ['hello world'],
697
+ action: 'updateOne',
698
+ });
699
+ let promises = [];
700
+ for (let c = 0; c <= 10; c++) {
701
+ promises.push(db.createOne(this.collectionName, {
702
+ uniqueField: 'fast',
703
+ }));
704
+ }
705
+ err = (yield assert.doesThrowAsync(() => Promise.all(promises)));
706
+ errorAssert.assertError(err, 'DUPLICATE_RECORD', {
707
+ duplicateFields: ['uniqueField'],
708
+ duplicateValues: ['fast'],
709
+ });
710
+ promises = [];
711
+ for (let c = 0; c <= 10; c++) {
712
+ promises.push(db.upsertOne(this.collectionName, { uniqueField: `${c}` }, {
713
+ uniqueField: 'upsertFast',
714
+ }));
715
+ }
716
+ err = (yield assert.doesThrowAsync(() => Promise.all(promises)));
717
+ errorAssert.assertError(err, 'DUPLICATE_RECORD');
718
+ yield this.shutdown(db);
719
+ });
720
+ },
721
+ assertCanCreateUniqueIndexOnNestedField(connect) {
722
+ return __awaiter(this, void 0, void 0, function* () {
723
+ const db = yield connect();
724
+ yield db.createUniqueIndex(this.collectionName, [
725
+ 'target.organizationId',
726
+ 'slug',
727
+ ]);
728
+ yield db.createOne(this.collectionName, {
729
+ target: {
730
+ organizationId: 'go!',
731
+ locationId: null,
732
+ },
733
+ slug: 'a slug',
734
+ });
735
+ const err = yield assert.doesThrowAsync(() => db.createOne(this.collectionName, {
736
+ target: {
737
+ organizationId: 'go!',
738
+ locationId: null,
739
+ },
740
+ slug: 'a slug',
741
+ }));
742
+ errorAssert.assertError(err, 'DUPLICATE_RECORD', {
743
+ collectionName: this.collectionName,
744
+ duplicateFields: ['target.organizationId', 'slug'],
745
+ duplicateValues: ['go!', 'a slug'],
746
+ action: 'create',
747
+ });
748
+ yield db.upsertOne(this.collectionName, {
749
+ target: {
750
+ organizationId: 'go!',
751
+ },
752
+ }, {
753
+ target: {
754
+ organizationId: 'go 2!',
755
+ locationId: null,
756
+ },
757
+ slug: 'a slug',
758
+ });
759
+ yield this.shutdown(db);
760
+ });
761
+ },
762
+ assertCanPushToArrayOnUpsert(connect) {
763
+ return __awaiter(this, void 0, void 0, function* () {
764
+ const db = yield connect();
765
+ const record = yield db.createOne(this.collectionName, { names: [] });
766
+ yield db.updateOne(this.collectionName, {
767
+ id: record.id,
768
+ }, {
769
+ $push: { names: 'test' },
770
+ });
771
+ const match = yield db.findOne(this.collectionName);
772
+ assert.isEqualDeep(match === null || match === void 0 ? void 0 : match.names, ['test']);
773
+ yield this.shutdown(db);
774
+ });
775
+ },
776
+ assertCanSearchByRegex(connect) {
777
+ return __awaiter(this, void 0, void 0, function* () {
778
+ const db = yield connect();
779
+ yield db.create(this.collectionName, [
780
+ {
781
+ name: 'first',
782
+ subObject: {
783
+ score: 1,
784
+ },
785
+ },
786
+ {
787
+ name: 'second',
788
+ subObject: {
789
+ score: 2,
790
+ },
791
+ },
792
+ {
793
+ name: 'third',
794
+ subObject: {
795
+ score: 2,
796
+ },
797
+ },
798
+ ]);
799
+ const all = yield db.find(this.collectionName, { name: { $regex: /fi/ } }, { includeFields: ['name'] });
800
+ assert.isEqualDeep(all, [
801
+ {
802
+ name: 'first',
803
+ },
804
+ ]);
805
+ yield this.shutdown(db);
806
+ });
807
+ },
808
+ assertCanReturnOnlySelectFields(connect) {
809
+ return __awaiter(this, void 0, void 0, function* () {
810
+ const db = yield connect();
811
+ yield db.create(this.collectionName, [
812
+ {
813
+ name: 'first',
814
+ subObject: {
815
+ score: 1,
816
+ },
817
+ },
818
+ {
819
+ name: 'second',
820
+ subObject: {
821
+ score: 2,
822
+ },
823
+ },
824
+ {
825
+ name: 'third',
826
+ subObject: {
827
+ score: 2,
828
+ },
829
+ },
830
+ ]);
831
+ const all = yield db.find(this.collectionName, {}, { includeFields: ['name'] });
832
+ assert.isEqualDeep(all, [
833
+ {
834
+ name: 'first',
835
+ },
836
+ {
837
+ name: 'second',
838
+ },
839
+ {
840
+ name: 'third',
841
+ },
842
+ ]);
843
+ const first = yield db.findOne(this.collectionName, {}, { includeFields: ['subObject'] });
844
+ assert.isEqualDeep(first, {
845
+ subObject: {
846
+ score: 1,
847
+ },
848
+ });
849
+ yield this.shutdown(db);
850
+ });
851
+ },
852
+ assertThrowsWithoutDatabaseName(connect) {
853
+ return __awaiter(this, void 0, void 0, function* () {
854
+ const err = yield assert.doesThrowAsync(() => connect(undefined, 'undefined'));
855
+ errorAssert.assertError(err, 'INVALID_DATABASE_NAME', {
856
+ suppliedName: 'undefined',
857
+ });
858
+ });
859
+ },
860
+ assertThrowsWhenCantConnect(connect) {
861
+ return __awaiter(this, void 0, void 0, function* () {
862
+ const err = yield assert.doesThrowAsync(() => connect('mongodb://localhost:9999'));
863
+ errorAssert.assertError(err, 'UNABLE_TO_CONNECT_TO_DB');
864
+ });
865
+ },
866
+ assertThrowsWithInvalidConnectionString(connect) {
867
+ return __awaiter(this, void 0, void 0, function* () {
868
+ const err = yield assert.doesThrowAsync(() => connect('astnoehusantoheun'));
869
+ errorAssert.assertError(err, 'INVALID_DB_CONNECTION_STRING');
870
+ });
871
+ },
872
+ assertKnowsIfConnectionClosed(connect) {
873
+ return __awaiter(this, void 0, void 0, function* () {
874
+ const db = yield connect();
875
+ assert.isTrue(db.isConnected());
876
+ yield db.close();
877
+ assert.isFalse(db.isConnected());
878
+ });
879
+ },
880
+ assertCanQueryPathWithDotSyntax(connect) {
881
+ return __awaiter(this, void 0, void 0, function* () {
882
+ const db = yield connect();
883
+ yield db.create(this.collectionName, [
884
+ {
885
+ name: 'first',
886
+ subObject: {
887
+ score: 1,
888
+ },
889
+ },
890
+ {
891
+ name: 'second',
892
+ subObject: {
893
+ score: 2,
894
+ },
895
+ },
896
+ {
897
+ name: 'third',
898
+ subObject: {
899
+ score: 2,
900
+ },
901
+ },
902
+ ]);
903
+ const secondMatch = yield db.findOne(this.collectionName, {
904
+ 'subObject.score': 2,
905
+ });
906
+ assert.isTruthy(secondMatch);
907
+ assert.isEqual(secondMatch.name, 'second');
908
+ yield this.shutdown(db);
909
+ });
910
+ },
911
+ assertCanQueryByGtLtGteLte(connect) {
912
+ return __awaiter(this, void 0, void 0, function* () {
913
+ const db = yield connect();
914
+ const created = yield db.create(this.collectionName, [
915
+ {
916
+ position: 1,
917
+ },
918
+ {
919
+ position: 2,
920
+ },
921
+ {
922
+ position: 3,
923
+ },
924
+ {
925
+ position: 4,
926
+ },
927
+ ]);
928
+ const gtMatches = yield db.find(this.collectionName, {
929
+ id: { $gt: created[2].id },
930
+ });
931
+ assert.isLength(gtMatches, 1);
932
+ assert.isEqual(gtMatches[0].position, 4);
933
+ const gteMatches = yield db.find(this.collectionName, {
934
+ id: { $gte: created[2].id },
935
+ });
936
+ assert.isLength(gteMatches, 2);
937
+ assert.isEqual(gteMatches[0].position, 3);
938
+ assert.isEqual(gteMatches[1].position, 4);
939
+ const ltMatches = yield db.find(this.collectionName, {
940
+ id: { $lt: created[2].id },
941
+ });
942
+ assert.isLength(ltMatches, 2);
943
+ assert.isEqual(ltMatches[0].position, 1);
944
+ assert.isEqual(ltMatches[1].position, 2);
945
+ const lteMatches = yield db.find(this.collectionName, {
946
+ id: { $lte: created[2].id },
947
+ });
948
+ assert.isLength(lteMatches, 3);
949
+ assert.isEqual(lteMatches[0].position, 1);
950
+ assert.isEqual(lteMatches[1].position, 2);
951
+ assert.isEqual(lteMatches[2].position, 3);
952
+ yield this.shutdown(db);
953
+ });
954
+ },
955
+ assertCanFindWithNe(connect) {
956
+ return __awaiter(this, void 0, void 0, function* () {
957
+ const db = yield connect();
958
+ const record1 = yield db.createOne(this.collectionName, {
959
+ foo: 'bar',
960
+ hello: 'world',
961
+ });
962
+ yield db.createOne(this.collectionName, {
963
+ foo: 'bar2',
964
+ hello: 'world',
965
+ });
966
+ yield db.createOne(this.collectionName, {
967
+ foo: 'bar3',
968
+ hello: 'planet',
969
+ });
970
+ const query = { id: { $ne: record1.id } };
971
+ const results = yield db.find(this.collectionName, query);
972
+ assert.isLength(results, 2);
973
+ yield this.shutdown(db);
974
+ });
975
+ },
976
+ assertCanFindWithIn(connect) {
977
+ return __awaiter(this, void 0, void 0, function* () {
978
+ const db = yield connect();
979
+ const record1 = yield db.createOne(this.collectionName, {
980
+ foo: 'bar',
981
+ hello: 'world',
982
+ });
983
+ const record2 = yield db.createOne(this.collectionName, {
984
+ foo: 'bar2',
985
+ hello: 'world',
986
+ });
987
+ const record3 = yield db.createOne(this.collectionName, {
988
+ foo: 'bar3',
989
+ hello: 'planet',
990
+ });
991
+ const query = { id: { $in: [record1.id, record2.id, record3.id] } };
992
+ const results = yield db.find(this.collectionName, query);
993
+ assert.isLength(results, 3);
994
+ yield this.shutdown(db);
995
+ });
996
+ },
997
+ assertCanCountOnId(connect) {
998
+ return __awaiter(this, void 0, void 0, function* () {
999
+ const db = yield connect();
1000
+ const first = yield db.createOne(this.collectionName, {
1001
+ foo: 'bar',
1002
+ hello: 'world',
1003
+ });
1004
+ const second = yield db.createOne(this.collectionName, {
1005
+ foo: 'bar2',
1006
+ hello: 'world',
1007
+ });
1008
+ const third = yield db.createOne(this.collectionName, {
1009
+ foo: 'bar3',
1010
+ hello: 'planet',
1011
+ });
1012
+ const countFirst = yield db.count(this.collectionName, { id: first.id });
1013
+ const countSecond = yield db.count(this.collectionName, {
1014
+ id: { $in: [first.id, second.id] },
1015
+ });
1016
+ const countThird = yield db.count(this.collectionName, {
1017
+ id: { $in: [first.id, second.id, third.id] },
1018
+ });
1019
+ assert.isEqual(countFirst, 1);
1020
+ assert.isEqual(countSecond, 2);
1021
+ assert.isEqual(countThird, 3);
1022
+ yield this.shutdown(db);
1023
+ });
1024
+ },
1025
+ assertCanCount(connect) {
1026
+ return __awaiter(this, void 0, void 0, function* () {
1027
+ const db = yield connect();
1028
+ yield db.createOne(this.collectionName, { foo: 'bar', hello: 'world' });
1029
+ yield db.createOne(this.collectionName, { foo: 'bar2', hello: 'world' });
1030
+ yield db.createOne(this.collectionName, { foo: 'bar3', hello: 'planet' });
1031
+ const countAll = yield db.count(this.collectionName);
1032
+ assert.isEqual(countAll, 3);
1033
+ yield this.shutdown(db);
1034
+ });
1035
+ },
1036
+ assertCanUpsertNull(connect) {
1037
+ return __awaiter(this, void 0, void 0, function* () {
1038
+ const db = yield connect();
1039
+ const createdUndefined = yield db.upsertOne(this.collectionName, {
1040
+ undefinedField: undefined,
1041
+ }, {
1042
+ undefinedField: undefined,
1043
+ });
1044
+ assert.isTruthy(createdUndefined);
1045
+ assert.isNull(createdUndefined.undefinedField);
1046
+ const createdNull = yield db.upsertOne(this.collectionName, {
1047
+ nullField: null,
1048
+ }, {
1049
+ nullField: null,
1050
+ });
1051
+ assert.isTruthy(createdNull);
1052
+ let all = yield db.find(this.collectionName);
1053
+ assert.isLength(all, 2);
1054
+ const updatedUndefined = yield db.upsertOne(this.collectionName, {
1055
+ undefinedField: undefined,
1056
+ }, { undefinedField: 'now defined' });
1057
+ assert.isEqual(updatedUndefined.id, createdUndefined.id);
1058
+ assert.isEqual(updatedUndefined.undefinedField, 'now defined');
1059
+ const updatedNull = yield db.upsertOne(this.collectionName, {
1060
+ nullField: null,
1061
+ }, { nullField: 'now defined' });
1062
+ assert.isEqual(updatedNull.id, createdNull.id);
1063
+ assert.isEqual(updatedNull.nullField, 'now defined');
1064
+ all = yield db.find(this.collectionName);
1065
+ assert.isLength(all, 2);
1066
+ yield this.shutdown(db);
1067
+ });
1068
+ },
1069
+ assertCanSaveAndGetNullAndUndefined(connect) {
1070
+ return __awaiter(this, void 0, void 0, function* () {
1071
+ const db = yield connect();
1072
+ const created = yield db.createOne(this.collectionName, {
1073
+ undefinedField: undefined,
1074
+ nullField: null,
1075
+ });
1076
+ assert.isNull(created.undefinedField);
1077
+ assert.isNull(created.nullField);
1078
+ const matchedUndefined = yield db.findOne(this.collectionName, {
1079
+ undefinedField: undefined,
1080
+ });
1081
+ assert.isTruthy(matchedUndefined);
1082
+ assert.isNull(matchedUndefined.undefinedField);
1083
+ const matchedNull = yield db.findOne(this.collectionName, {
1084
+ nullField: null,
1085
+ });
1086
+ assert.isTruthy(matchedNull);
1087
+ assert.isNull(matchedNull.nullField);
1088
+ const updatedUndefined = yield db.updateOne(this.collectionName, {
1089
+ undefinedField: undefined,
1090
+ }, { undefinedField: 'now defined' });
1091
+ assert.isEqual(updatedUndefined.id, created.id);
1092
+ assert.isEqual(updatedUndefined.undefinedField, 'now defined');
1093
+ const updatedNull = yield db.updateOne(this.collectionName, {
1094
+ nullField: null,
1095
+ }, { nullField: 'now defined' });
1096
+ assert.isEqual(updatedNull.nullField, 'now defined');
1097
+ yield this.shutdown(db);
1098
+ });
1099
+ },
1100
+ assertSyncIndexesDoesNotRemoveExisting(connect) {
1101
+ return __awaiter(this, void 0, void 0, function* () {
1102
+ const db = yield connect();
1103
+ yield db.createIndex(this.collectionName, ['otherField']);
1104
+ yield db.createIndex(this.collectionName, ['someField']);
1105
+ db.createIndex = () => {
1106
+ throw new Error('Should not have been called');
1107
+ };
1108
+ db.dropIndex = () => {
1109
+ throw new Error('Should not have been called');
1110
+ };
1111
+ yield db.syncIndexes(this.collectionName, [['someField'], ['otherField']]);
1112
+ yield this.shutdown(db);
1113
+ });
1114
+ },
1115
+ assertSyncIndexesRemovesExtraIndexes(connect) {
1116
+ return __awaiter(this, void 0, void 0, function* () {
1117
+ const db = yield connect();
1118
+ yield db.syncIndexes(this.collectionName, [
1119
+ ['field'],
1120
+ ['someField'],
1121
+ ['otherField', 'otherField2'],
1122
+ ]);
1123
+ let indexes = yield this._getFilteredIndexes(db);
1124
+ assert.isLength(indexes, 3);
1125
+ yield db.syncIndexes(this.collectionName, [['field']]);
1126
+ indexes = yield this._getFilteredIndexes(db);
1127
+ assert.isLength(indexes, 1);
1128
+ assert.isEqual(indexes[0][0], 'field');
1129
+ yield this.shutdown(db);
1130
+ });
1131
+ },
1132
+ assertSyncIndexesSkipsExisting(connect) {
1133
+ return __awaiter(this, void 0, void 0, function* () {
1134
+ const db = yield connect();
1135
+ yield db.syncIndexes(this.collectionName, [['field']]);
1136
+ let indexes = yield this._getFilteredIndexes(db);
1137
+ assert.isLength(indexes, 1);
1138
+ yield db.syncIndexes(this.collectionName, [
1139
+ ['field'],
1140
+ ['someField'],
1141
+ ['otherField', 'otherField2'],
1142
+ ]);
1143
+ indexes = yield this._getFilteredIndexes(db);
1144
+ assert.isLength(indexes, 3);
1145
+ yield this.shutdown(db);
1146
+ });
1147
+ },
1148
+ assertCantDropCompoundIndexThatDoesNotExist(connect) {
1149
+ return __awaiter(this, void 0, void 0, function* () {
1150
+ const db = yield connect();
1151
+ yield db.createIndex(this.collectionName, ['someField', 'someOtherField']);
1152
+ const err = yield assert.doesThrowAsync(() => db.dropIndex(this.collectionName, ['uniqueField', 'someOtherField']));
1153
+ errorAssert.assertError(err, 'INDEX_NOT_FOUND');
1154
+ yield this.shutdown(db);
1155
+ });
1156
+ },
1157
+ assertCanDropCompoundIndex(connect) {
1158
+ return __awaiter(this, void 0, void 0, function* () {
1159
+ const db = yield connect();
1160
+ yield db.createIndex(this.collectionName, ['someField', 'otherField']);
1161
+ yield db.dropIndex(this.collectionName, ['someField', 'otherField']);
1162
+ let indexes = yield this._getFilteredIndexes(db);
1163
+ assert.isLength(indexes, 0);
1164
+ yield db.createIndex(this.collectionName, ['someField', 'someField2']);
1165
+ yield db.createIndex(this.collectionName, ['someField', 'someField3']);
1166
+ yield db.dropIndex(this.collectionName, ['someField', 'someField3']);
1167
+ indexes = yield this._getFilteredIndexes(db);
1168
+ assert.isLength(indexes, 1);
1169
+ yield this.shutdown(db);
1170
+ });
1171
+ },
1172
+ assertCanDropIndex(connect) {
1173
+ return __awaiter(this, void 0, void 0, function* () {
1174
+ const db = yield connect();
1175
+ yield db.createIndex(this.collectionName, ['someField']);
1176
+ yield db.dropIndex(this.collectionName, ['someField']);
1177
+ let indexes = yield this._getFilteredIndexes(db);
1178
+ assert.isLength(indexes, 0);
1179
+ yield db.createIndex(this.collectionName, ['someField2']);
1180
+ yield db.createIndex(this.collectionName, ['someField3']);
1181
+ yield db.dropIndex(this.collectionName, ['someField3']);
1182
+ indexes = yield this._getFilteredIndexes(db);
1183
+ assert.isLength(indexes, 1);
1184
+ yield this.shutdown(db);
1185
+ });
1186
+ },
1187
+ assertCanCreateMultiFieldIndex(connect, fields) {
1188
+ return __awaiter(this, void 0, void 0, function* () {
1189
+ const db = yield connect();
1190
+ yield db.createIndex(this.collectionName, fields);
1191
+ let indexes = yield this._getFilteredIndexes(db);
1192
+ assert.isLength(indexes, 1);
1193
+ assert.isEqualDeep(indexes, [fields]);
1194
+ yield this.shutdown(db);
1195
+ });
1196
+ },
1197
+ assertCantCreateSameIndexTwice(connect) {
1198
+ return __awaiter(this, void 0, void 0, function* () {
1199
+ const db = yield connect();
1200
+ yield db.createIndex(this.collectionName, ['field']);
1201
+ let indexes = yield this._getFilteredIndexes(db);
1202
+ assert.isLength(indexes, 1);
1203
+ const err = yield assert.doesThrowAsync(() => db.createIndex(this.collectionName, ['field']));
1204
+ errorAssert.assertError(err, 'INDEX_EXISTS');
1205
+ yield this.shutdown(db);
1206
+ });
1207
+ },
1208
+ assertCanCreateIndex(connect) {
1209
+ return __awaiter(this, void 0, void 0, function* () {
1210
+ const db = yield connect();
1211
+ yield db.createIndex(this.collectionName, ['field']);
1212
+ let indexes = yield this._getFilteredIndexes(db);
1213
+ assert.isLength(indexes, 1);
1214
+ assert.isLength(indexes[0], 1);
1215
+ assert.isEqual(indexes[0][0], 'field');
1216
+ yield db.createIndex(this.collectionName, ['field2']);
1217
+ indexes = yield this._getFilteredIndexes(db);
1218
+ assert.isLength(indexes, 2);
1219
+ assert.isEqual(indexes[1][0], 'field2');
1220
+ yield db.createIndex(this.collectionName, ['field3', 'field4']);
1221
+ indexes = yield this._getFilteredIndexes(db);
1222
+ assert.isLength(indexes, 3);
1223
+ assert.isEqual(indexes[2][0], 'field3');
1224
+ assert.isEqual(indexes[2][1], 'field4');
1225
+ yield this.shutdown(db);
1226
+ });
1227
+ },
1228
+ assertHasNoIndexToStart(connect) {
1229
+ return __awaiter(this, void 0, void 0, function* () {
1230
+ const db = yield connect();
1231
+ const indexes = yield db.getIndexes(this.collectionName);
1232
+ assert.isLength(indexes, 0);
1233
+ yield this.shutdown(db);
1234
+ });
1235
+ },
1236
+ assertNestedFieldIndexUpdates(connect) {
1237
+ return __awaiter(this, void 0, void 0, function* () {
1238
+ const db = yield connect();
1239
+ yield db.createUniqueIndex(this.collectionName, [
1240
+ 'target.organizationId',
1241
+ 'slug',
1242
+ ]);
1243
+ const results = yield db.createOne(this.collectionName, {
1244
+ target: {
1245
+ organizationId: 'go!',
1246
+ },
1247
+ aNonIndexedField: true,
1248
+ slug: 'a slug',
1249
+ });
1250
+ const updated = yield db.updateOne(this.collectionName, { id: results.id }, {
1251
+ target: {
1252
+ organizationId: 'go!',
1253
+ },
1254
+ aNonIndexedField: false,
1255
+ slug: 'a slug',
1256
+ });
1257
+ assert.isEqual(updated.aNonIndexedField, false);
1258
+ yield this.shutdown(db);
1259
+ });
1260
+ },
1261
+ assertUpsertWithUniqueIndex(connect) {
1262
+ return __awaiter(this, void 0, void 0, function* () {
1263
+ const db = yield connect();
1264
+ yield db.syncUniqueIndexes(this.collectionName, [
1265
+ ['name', 'target.organizationId'],
1266
+ ['slug', 'target.organizationId'],
1267
+ ]);
1268
+ const results = yield db.createOne(this.collectionName, {
1269
+ target: {
1270
+ organizationId: 'go!',
1271
+ },
1272
+ name: 'squirrel',
1273
+ slug: 'a slug',
1274
+ });
1275
+ const updated = yield db.upsertOne(this.collectionName, { target: { organizationId: 'go!' }, slug: 'a slug' }, {
1276
+ target: {
1277
+ organizationId: 'go!',
1278
+ },
1279
+ name: 'notsquirrel',
1280
+ slug: 'a slug',
1281
+ });
1282
+ assert.isEqual(results.id, updated.id);
1283
+ assert.isEqual(updated.name, 'notsquirrel');
1284
+ yield this.shutdown(db);
1285
+ });
1286
+ },
1287
+ assertSyncIndexesHandlesRaceConditions(connect) {
1288
+ return __awaiter(this, void 0, void 0, function* () {
1289
+ const db = yield connect();
1290
+ const syncs = [
1291
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1292
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1293
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1294
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1295
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1296
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1297
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1298
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1299
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1300
+ db.syncIndexes(this.collectionName, [['otherField', 'otherField2']]),
1301
+ ];
1302
+ yield Promise.all(syncs);
1303
+ yield this.shutdown(db);
1304
+ });
1305
+ },
1306
+ assertDuplicateFieldsWithMultipleUniqueIndexesWorkAsExpected(connect) {
1307
+ return __awaiter(this, void 0, void 0, function* () {
1308
+ const db = yield connect();
1309
+ yield db.createUniqueIndex(this.collectionName, ['uniqueField1']);
1310
+ yield db.createUniqueIndex(this.collectionName, ['uniqueField2']);
1311
+ yield db.createOne(this.collectionName, {
1312
+ uniqueField1: 'unique field 1',
1313
+ uniqueField2: 'unique field 2',
1314
+ });
1315
+ let err = yield assert.doesThrowAsync(() => db.createOne(this.collectionName, {
1316
+ uniqueField1: 'unique field 1',
1317
+ uniqueField2: 'unique field 2',
1318
+ }));
1319
+ errorAssert.assertError(err, 'DUPLICATE_RECORD', {
1320
+ collectionName: this.collectionName,
1321
+ duplicateFields: ['uniqueField1'],
1322
+ duplicateValues: ['unique field 1'],
1323
+ action: 'create',
1324
+ });
1325
+ err = yield assert.doesThrowAsync(() => db.createOne(this.collectionName, {
1326
+ uniqueField1: 'unique field 1.0',
1327
+ uniqueField2: 'unique field 2',
1328
+ }));
1329
+ errorAssert.assertError(err, 'DUPLICATE_RECORD', {
1330
+ collectionName: this.collectionName,
1331
+ duplicateFields: ['uniqueField2'],
1332
+ duplicateValues: ['unique field 2'],
1333
+ action: 'create',
1334
+ });
1335
+ yield this.shutdown(db);
1336
+ });
1337
+ },
1338
+ };
1339
+ export default databaseAssertUtil;