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