@sprucelabs/data-stores 8.0.124 → 8.1.1

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.
@@ -27,11 +27,14 @@ export default class MongoDatabase implements Database {
27
27
  isConnected(): boolean;
28
28
  dropCollection(name: string): Promise<void>;
29
29
  dropDatabase(): Promise<void>;
30
- getUniqueIndexes(collection: string): Promise<string[][]>;
31
30
  private listIndexes;
32
31
  dropIndex(collection: string, fields: string[]): Promise<void>;
33
- private assertUniqueIndexDoesNotExist;
34
- private doesUniqueIndexExist;
32
+ getUniqueIndexes(collection: string): Promise<string[][]>;
33
+ getIndexes(collection: string, shouldIncludeUnique?: boolean): Promise<any[]>;
34
+ createIndex(collection: string, fields: string[]): Promise<void>;
35
+ private assertIndexDoesNotExist;
36
+ private doesIndexExist;
37
+ syncIndexes(collectionName: string, indexes: string[][]): Promise<void>;
35
38
  createUniqueIndex(collection: string, fields: string[]): Promise<void>;
36
39
  syncUniqueIndexes(collectionName: string, indexes: string[][]): Promise<void>;
37
40
  update(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<any>;
@@ -215,21 +215,6 @@ class MongoDatabase {
215
215
  async dropDatabase() {
216
216
  await this.assertDbWhileAttempingTo('drop the entire database.').dropDatabase();
217
217
  }
218
- async getUniqueIndexes(collection) {
219
- try {
220
- const indexes = await this.listIndexes(collection);
221
- const uniqueIndexes = [];
222
- for (const index of indexes) {
223
- if (index.unique) {
224
- uniqueIndexes.push(Object.keys(index.key));
225
- }
226
- }
227
- return uniqueIndexes;
228
- }
229
- catch (err) {
230
- return [];
231
- }
232
- }
233
218
  async listIndexes(collection) {
234
219
  try {
235
220
  return await this.assertDbWhileAttempingTo('get indexes.', collection)
@@ -256,22 +241,91 @@ class MongoDatabase {
256
241
  throw new SpruceError_1.default({ code: 'INDEX_NOT_FOUND', missingIndex: fields });
257
242
  }
258
243
  }
259
- assertUniqueIndexDoesNotExist(currentIndexes, fields) {
260
- if (this.doesUniqueIndexExist(currentIndexes, fields)) {
244
+ async getUniqueIndexes(collection) {
245
+ try {
246
+ const indexes = await this.listIndexes(collection);
247
+ const uniqueIndexes = [];
248
+ for (const index of indexes) {
249
+ if (index.unique) {
250
+ uniqueIndexes.push(Object.keys(index.key));
251
+ }
252
+ }
253
+ return uniqueIndexes;
254
+ }
255
+ catch (err) {
256
+ return [];
257
+ }
258
+ }
259
+ async getIndexes(collection, shouldIncludeUnique = false) {
260
+ try {
261
+ const indexes = await this.listIndexes(collection);
262
+ if (shouldIncludeUnique) {
263
+ return indexes;
264
+ }
265
+ const nonUniqueIndexes = [];
266
+ for (const index of indexes) {
267
+ if (!index.unique) {
268
+ nonUniqueIndexes.push(Object.keys(index.key));
269
+ }
270
+ }
271
+ return nonUniqueIndexes;
272
+ }
273
+ catch (err) {
274
+ return [];
275
+ }
276
+ }
277
+ async createIndex(collection, fields) {
278
+ const currentIndexes = await this.getIndexes(collection);
279
+ await this.assertIndexDoesNotExist(currentIndexes, fields);
280
+ const index = {};
281
+ fields.forEach((name) => {
282
+ index[name] = 1;
283
+ });
284
+ try {
285
+ await this.assertDbWhileAttempingTo('create an index.', collection)
286
+ .collection(collection)
287
+ .createIndex(index);
288
+ }
289
+ catch (err) {
290
+ if ((err === null || err === void 0 ? void 0 : err.code) === 11000) {
291
+ throw new SpruceError_1.default({
292
+ code: 'DUPLICATE_KEY',
293
+ friendlyMessage: `Could not create index! Index on '${collection}' has duplicate key for "${fields.join(',')}"`,
294
+ });
295
+ }
296
+ else {
297
+ throw err;
298
+ }
299
+ }
300
+ }
301
+ assertIndexDoesNotExist(currentIndexes, fields) {
302
+ if (this.doesIndexExist(currentIndexes, fields)) {
261
303
  throw new SpruceError_1.default({ code: 'INDEX_EXISTS', index: fields });
262
304
  }
263
305
  }
264
- doesUniqueIndexExist(currentIndexes, fields) {
265
- for (const uniq of currentIndexes !== null && currentIndexes !== void 0 ? currentIndexes : []) {
266
- if ((0, lodash_1.isEqual)(uniq, fields)) {
306
+ doesIndexExist(currentIndexes, fields) {
307
+ for (const index of currentIndexes !== null && currentIndexes !== void 0 ? currentIndexes : []) {
308
+ if ((0, lodash_1.isEqual)(index, fields)) {
267
309
  return true;
268
310
  }
269
311
  }
270
312
  return false;
271
313
  }
314
+ async syncIndexes(collectionName, indexes) {
315
+ const currentIndexes = await this.getIndexes(collectionName);
316
+ const extraIndexes = (0, lodash_1.differenceWith)(currentIndexes, indexes, lodash_1.isEqual).filter((i) => !(i.length === 1 && i[0] === '_id'));
317
+ for (const index of indexes) {
318
+ if (!this.doesIndexExist(currentIndexes, index)) {
319
+ await this.createIndex(collectionName, index);
320
+ }
321
+ }
322
+ for (const extra of extraIndexes) {
323
+ await this.dropIndex(collectionName, extra);
324
+ }
325
+ }
272
326
  async createUniqueIndex(collection, fields) {
273
327
  const currentIndexes = await this.getUniqueIndexes(collection);
274
- await this.assertUniqueIndexDoesNotExist(currentIndexes, fields);
328
+ await this.assertIndexDoesNotExist(currentIndexes, fields);
275
329
  const index = {};
276
330
  fields.forEach((name) => {
277
331
  index[name] = 1;
@@ -297,7 +351,7 @@ class MongoDatabase {
297
351
  const currentIndexes = await this.getUniqueIndexes(collectionName);
298
352
  const extraIndexes = (0, lodash_1.differenceWith)(currentIndexes, indexes, lodash_1.isEqual);
299
353
  for (const index of indexes) {
300
- if (!this.doesUniqueIndexExist(currentIndexes, index)) {
354
+ if (!this.doesIndexExist(currentIndexes, index)) {
301
355
  await this.createUniqueIndex(collectionName, index);
302
356
  }
303
357
  }
@@ -30,9 +30,12 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
30
30
  deleteOne(collection: string, query: Record<string, any>): Promise<number>;
31
31
  private assertPassesUniqueIndexes;
32
32
  getUniqueIndexes(collection: string): Promise<string[][]>;
33
+ getIndexes(collection: string, shouldIncludeUnique?: boolean): Promise<string[][]>;
33
34
  dropIndex(collection: string, fields: string[]): Promise<void>;
34
- private assertUniqueIndexDoesNotExist;
35
- private doesUniqueIndexExist;
35
+ private assertIndexDoesNotExist;
36
+ private doesIndexExist;
36
37
  createUniqueIndex(collection: string, fields: string[]): Promise<void>;
38
+ createIndex(collection: string, fields: string[]): Promise<void>;
37
39
  syncUniqueIndexes(collectionName: string, indexes: string[][]): Promise<void>;
40
+ syncIndexes(collectionName: string, indexes: string[][]): Promise<void>;
38
41
  }
@@ -298,12 +298,22 @@ class NeDbDatabase extends AbstractMutexer_1.default {
298
298
  await this.randomDelay();
299
299
  return (_a = col._uniqueIndexes) !== null && _a !== void 0 ? _a : [];
300
300
  }
301
+ async getIndexes(collection, shouldIncludeUnique = false) {
302
+ var _a, _b, _c;
303
+ const col = this.loadCollection(collection);
304
+ await this.randomDelay();
305
+ if (shouldIncludeUnique) {
306
+ const uniqIndexes = (_a = col._uniqueIndexes) !== null && _a !== void 0 ? _a : [];
307
+ return uniqIndexes.concat((_b = col._indexes) !== null && _b !== void 0 ? _b : []);
308
+ }
309
+ return (_c = col._indexes) !== null && _c !== void 0 ? _c : [];
310
+ }
301
311
  async dropIndex(collection, fields) {
302
- var _a;
312
+ var _a, _b;
303
313
  const col = this.loadCollection(collection);
304
314
  await this.randomDelay();
305
315
  let found = false;
306
- const newIndexes = [];
316
+ let newIndexes = [];
307
317
  for (const uniq of (_a = col._uniqueIndexes) !== null && _a !== void 0 ? _a : []) {
308
318
  if (!(0, lodash_1.isEqual)(uniq, fields)) {
309
319
  newIndexes.push(uniq);
@@ -312,19 +322,35 @@ class NeDbDatabase extends AbstractMutexer_1.default {
312
322
  found = true;
313
323
  }
314
324
  }
315
- if (!found) {
316
- throw new SpruceError_1.default({ code: 'INDEX_NOT_FOUND', missingIndex: fields });
325
+ if (found) {
326
+ col._uniqueIndexes = newIndexes;
327
+ return;
328
+ }
329
+ else {
330
+ newIndexes = [];
331
+ for (const index of (_b = col._indexes) !== null && _b !== void 0 ? _b : []) {
332
+ if (!(0, lodash_1.isEqual)(index, fields)) {
333
+ newIndexes.push(index);
334
+ }
335
+ else {
336
+ found = true;
337
+ }
338
+ }
339
+ if (found) {
340
+ col._indexes = newIndexes;
341
+ return;
342
+ }
317
343
  }
318
- col._uniqueIndexes = newIndexes;
344
+ throw new SpruceError_1.default({ code: 'INDEX_NOT_FOUND', missingIndex: fields });
319
345
  }
320
- assertUniqueIndexDoesNotExist(currentIndexes, fields) {
321
- if (this.doesUniqueIndexExist(currentIndexes, fields)) {
346
+ assertIndexDoesNotExist(currentIndexes, fields) {
347
+ if (this.doesIndexExist(currentIndexes, fields)) {
322
348
  throw new SpruceError_1.default({ code: 'INDEX_EXISTS', index: fields });
323
349
  }
324
350
  }
325
- doesUniqueIndexExist(currentIndexes, fields) {
326
- for (const uniq of currentIndexes !== null && currentIndexes !== void 0 ? currentIndexes : []) {
327
- if ((0, lodash_1.isEqual)(uniq, fields)) {
351
+ doesIndexExist(currentIndexes, fields) {
352
+ for (const index of currentIndexes !== null && currentIndexes !== void 0 ? currentIndexes : []) {
353
+ if ((0, lodash_1.isEqual)(index, fields)) {
328
354
  return true;
329
355
  }
330
356
  }
@@ -336,7 +362,7 @@ class NeDbDatabase extends AbstractMutexer_1.default {
336
362
  col._uniqueIndexes = [];
337
363
  }
338
364
  await this.randomDelay();
339
- this.assertUniqueIndexDoesNotExist(col._uniqueIndexes, fields);
365
+ this.assertIndexDoesNotExist(col._uniqueIndexes, fields);
340
366
  if (col._uniqueIndexes) {
341
367
  const tempUniqueIndexes = [...col._uniqueIndexes];
342
368
  tempUniqueIndexes.push(fields);
@@ -361,12 +387,21 @@ class NeDbDatabase extends AbstractMutexer_1.default {
361
387
  }
362
388
  col._uniqueIndexes.push(fields);
363
389
  }
390
+ async createIndex(collection, fields) {
391
+ const col = this.loadCollection(collection);
392
+ if (!col._indexes) {
393
+ col._indexes = [];
394
+ }
395
+ await this.randomDelay();
396
+ this.assertIndexDoesNotExist(col._indexes, fields);
397
+ col._indexes.push(fields);
398
+ }
364
399
  async syncUniqueIndexes(collectionName, indexes) {
365
400
  var _a;
366
401
  const currentIndexes = await this.getUniqueIndexes(collectionName);
367
402
  const extraIndexes = (0, lodash_1.differenceWith)(currentIndexes, indexes, lodash_1.isEqual);
368
403
  for (const index of indexes) {
369
- if (!this.doesUniqueIndexExist(currentIndexes, index)) {
404
+ if (!this.doesIndexExist(currentIndexes, index)) {
370
405
  try {
371
406
  await this.createUniqueIndex(collectionName, index);
372
407
  }
@@ -381,5 +416,25 @@ class NeDbDatabase extends AbstractMutexer_1.default {
381
416
  await this.dropIndex(collectionName, extra);
382
417
  }
383
418
  }
419
+ async syncIndexes(collectionName, indexes) {
420
+ var _a;
421
+ const currentIndexes = await this.getIndexes(collectionName);
422
+ const extraIndexes = (0, lodash_1.differenceWith)(currentIndexes, indexes, lodash_1.isEqual);
423
+ for (const index of indexes) {
424
+ if (!this.doesIndexExist(currentIndexes, index)) {
425
+ try {
426
+ await this.createIndex(collectionName, index);
427
+ }
428
+ catch (err) {
429
+ if (((_a = err.options) === null || _a === void 0 ? void 0 : _a.code) !== 'INDEX_EXISTS') {
430
+ throw err;
431
+ }
432
+ }
433
+ }
434
+ }
435
+ for (const extra of extraIndexes) {
436
+ await this.dropIndex(collectionName, extra);
437
+ }
438
+ }
384
439
  }
385
440
  exports.default = NeDbDatabase;
@@ -27,11 +27,14 @@ export default class MongoDatabase implements Database {
27
27
  isConnected(): boolean;
28
28
  dropCollection(name: string): Promise<void>;
29
29
  dropDatabase(): Promise<void>;
30
- getUniqueIndexes(collection: string): Promise<string[][]>;
31
30
  private listIndexes;
32
31
  dropIndex(collection: string, fields: string[]): Promise<void>;
33
- private assertUniqueIndexDoesNotExist;
34
- private doesUniqueIndexExist;
32
+ getUniqueIndexes(collection: string): Promise<string[][]>;
33
+ getIndexes(collection: string, shouldIncludeUnique?: boolean): Promise<any[]>;
34
+ createIndex(collection: string, fields: string[]): Promise<void>;
35
+ private assertIndexDoesNotExist;
36
+ private doesIndexExist;
37
+ syncIndexes(collectionName: string, indexes: string[][]): Promise<void>;
35
38
  createUniqueIndex(collection: string, fields: string[]): Promise<void>;
36
39
  syncUniqueIndexes(collectionName: string, indexes: string[][]): Promise<void>;
37
40
  update(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<any>;
@@ -238,23 +238,6 @@ export default class MongoDatabase {
238
238
  yield this.assertDbWhileAttempingTo('drop the entire database.').dropDatabase();
239
239
  });
240
240
  }
241
- getUniqueIndexes(collection) {
242
- return __awaiter(this, void 0, void 0, function* () {
243
- try {
244
- const indexes = yield this.listIndexes(collection);
245
- const uniqueIndexes = [];
246
- for (const index of indexes) {
247
- if (index.unique) {
248
- uniqueIndexes.push(Object.keys(index.key));
249
- }
250
- }
251
- return uniqueIndexes;
252
- }
253
- catch (err) {
254
- return [];
255
- }
256
- });
257
- }
258
241
  listIndexes(collection) {
259
242
  return __awaiter(this, void 0, void 0, function* () {
260
243
  try {
@@ -285,23 +268,100 @@ export default class MongoDatabase {
285
268
  }
286
269
  });
287
270
  }
288
- assertUniqueIndexDoesNotExist(currentIndexes, fields) {
289
- if (this.doesUniqueIndexExist(currentIndexes, fields)) {
271
+ getUniqueIndexes(collection) {
272
+ return __awaiter(this, void 0, void 0, function* () {
273
+ try {
274
+ const indexes = yield this.listIndexes(collection);
275
+ const uniqueIndexes = [];
276
+ for (const index of indexes) {
277
+ if (index.unique) {
278
+ uniqueIndexes.push(Object.keys(index.key));
279
+ }
280
+ }
281
+ return uniqueIndexes;
282
+ }
283
+ catch (err) {
284
+ return [];
285
+ }
286
+ });
287
+ }
288
+ getIndexes(collection, shouldIncludeUnique = false) {
289
+ return __awaiter(this, void 0, void 0, function* () {
290
+ try {
291
+ const indexes = yield this.listIndexes(collection);
292
+ if (shouldIncludeUnique) {
293
+ return indexes;
294
+ }
295
+ const nonUniqueIndexes = [];
296
+ for (const index of indexes) {
297
+ if (!index.unique) {
298
+ nonUniqueIndexes.push(Object.keys(index.key));
299
+ }
300
+ }
301
+ return nonUniqueIndexes;
302
+ }
303
+ catch (err) {
304
+ return [];
305
+ }
306
+ });
307
+ }
308
+ createIndex(collection, fields) {
309
+ return __awaiter(this, void 0, void 0, function* () {
310
+ const currentIndexes = yield this.getIndexes(collection);
311
+ yield this.assertIndexDoesNotExist(currentIndexes, fields);
312
+ const index = {};
313
+ fields.forEach((name) => {
314
+ index[name] = 1;
315
+ });
316
+ try {
317
+ yield this.assertDbWhileAttempingTo('create an index.', collection)
318
+ .collection(collection)
319
+ .createIndex(index);
320
+ }
321
+ catch (err) {
322
+ if ((err === null || err === void 0 ? void 0 : err.code) === 11000) {
323
+ throw new SpruceError({
324
+ code: 'DUPLICATE_KEY',
325
+ friendlyMessage: `Could not create index! Index on '${collection}' has duplicate key for "${fields.join(',')}"`,
326
+ });
327
+ }
328
+ else {
329
+ throw err;
330
+ }
331
+ }
332
+ });
333
+ }
334
+ assertIndexDoesNotExist(currentIndexes, fields) {
335
+ if (this.doesIndexExist(currentIndexes, fields)) {
290
336
  throw new SpruceError({ code: 'INDEX_EXISTS', index: fields });
291
337
  }
292
338
  }
293
- doesUniqueIndexExist(currentIndexes, fields) {
294
- for (const uniq of currentIndexes !== null && currentIndexes !== void 0 ? currentIndexes : []) {
295
- if (isEqual(uniq, fields)) {
339
+ doesIndexExist(currentIndexes, fields) {
340
+ for (const index of currentIndexes !== null && currentIndexes !== void 0 ? currentIndexes : []) {
341
+ if (isEqual(index, fields)) {
296
342
  return true;
297
343
  }
298
344
  }
299
345
  return false;
300
346
  }
347
+ syncIndexes(collectionName, indexes) {
348
+ return __awaiter(this, void 0, void 0, function* () {
349
+ const currentIndexes = yield this.getIndexes(collectionName);
350
+ const extraIndexes = differenceWith(currentIndexes, indexes, isEqual).filter((i) => !(i.length === 1 && i[0] === '_id'));
351
+ for (const index of indexes) {
352
+ if (!this.doesIndexExist(currentIndexes, index)) {
353
+ yield this.createIndex(collectionName, index);
354
+ }
355
+ }
356
+ for (const extra of extraIndexes) {
357
+ yield this.dropIndex(collectionName, extra);
358
+ }
359
+ });
360
+ }
301
361
  createUniqueIndex(collection, fields) {
302
362
  return __awaiter(this, void 0, void 0, function* () {
303
363
  const currentIndexes = yield this.getUniqueIndexes(collection);
304
- yield this.assertUniqueIndexDoesNotExist(currentIndexes, fields);
364
+ yield this.assertIndexDoesNotExist(currentIndexes, fields);
305
365
  const index = {};
306
366
  fields.forEach((name) => {
307
367
  index[name] = 1;
@@ -329,7 +389,7 @@ export default class MongoDatabase {
329
389
  const currentIndexes = yield this.getUniqueIndexes(collectionName);
330
390
  const extraIndexes = differenceWith(currentIndexes, indexes, isEqual);
331
391
  for (const index of indexes) {
332
- if (!this.doesUniqueIndexExist(currentIndexes, index)) {
392
+ if (!this.doesIndexExist(currentIndexes, index)) {
333
393
  yield this.createUniqueIndex(collectionName, index);
334
394
  }
335
395
  }
@@ -30,9 +30,12 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
30
30
  deleteOne(collection: string, query: Record<string, any>): Promise<number>;
31
31
  private assertPassesUniqueIndexes;
32
32
  getUniqueIndexes(collection: string): Promise<string[][]>;
33
+ getIndexes(collection: string, shouldIncludeUnique?: boolean): Promise<string[][]>;
33
34
  dropIndex(collection: string, fields: string[]): Promise<void>;
34
- private assertUniqueIndexDoesNotExist;
35
- private doesUniqueIndexExist;
35
+ private assertIndexDoesNotExist;
36
+ private doesIndexExist;
36
37
  createUniqueIndex(collection: string, fields: string[]): Promise<void>;
38
+ createIndex(collection: string, fields: string[]): Promise<void>;
37
39
  syncUniqueIndexes(collectionName: string, indexes: string[][]): Promise<void>;
40
+ syncIndexes(collectionName: string, indexes: string[][]): Promise<void>;
38
41
  }
@@ -336,13 +336,25 @@ export default class NeDbDatabase extends AbstractMutexer {
336
336
  return (_a = col._uniqueIndexes) !== null && _a !== void 0 ? _a : [];
337
337
  });
338
338
  }
339
+ getIndexes(collection, shouldIncludeUnique = false) {
340
+ var _a, _b, _c;
341
+ return __awaiter(this, void 0, void 0, function* () {
342
+ const col = this.loadCollection(collection);
343
+ yield this.randomDelay();
344
+ if (shouldIncludeUnique) {
345
+ const uniqIndexes = (_a = col._uniqueIndexes) !== null && _a !== void 0 ? _a : [];
346
+ return uniqIndexes.concat((_b = col._indexes) !== null && _b !== void 0 ? _b : []);
347
+ }
348
+ return (_c = col._indexes) !== null && _c !== void 0 ? _c : [];
349
+ });
350
+ }
339
351
  dropIndex(collection, fields) {
340
- var _a;
352
+ var _a, _b;
341
353
  return __awaiter(this, void 0, void 0, function* () {
342
354
  const col = this.loadCollection(collection);
343
355
  yield this.randomDelay();
344
356
  let found = false;
345
- const newIndexes = [];
357
+ let newIndexes = [];
346
358
  for (const uniq of (_a = col._uniqueIndexes) !== null && _a !== void 0 ? _a : []) {
347
359
  if (!isEqual(uniq, fields)) {
348
360
  newIndexes.push(uniq);
@@ -351,20 +363,36 @@ export default class NeDbDatabase extends AbstractMutexer {
351
363
  found = true;
352
364
  }
353
365
  }
354
- if (!found) {
355
- throw new SpruceError({ code: 'INDEX_NOT_FOUND', missingIndex: fields });
366
+ if (found) {
367
+ col._uniqueIndexes = newIndexes;
368
+ return;
356
369
  }
357
- col._uniqueIndexes = newIndexes;
370
+ else {
371
+ newIndexes = [];
372
+ for (const index of (_b = col._indexes) !== null && _b !== void 0 ? _b : []) {
373
+ if (!isEqual(index, fields)) {
374
+ newIndexes.push(index);
375
+ }
376
+ else {
377
+ found = true;
378
+ }
379
+ }
380
+ if (found) {
381
+ col._indexes = newIndexes;
382
+ return;
383
+ }
384
+ }
385
+ throw new SpruceError({ code: 'INDEX_NOT_FOUND', missingIndex: fields });
358
386
  });
359
387
  }
360
- assertUniqueIndexDoesNotExist(currentIndexes, fields) {
361
- if (this.doesUniqueIndexExist(currentIndexes, fields)) {
388
+ assertIndexDoesNotExist(currentIndexes, fields) {
389
+ if (this.doesIndexExist(currentIndexes, fields)) {
362
390
  throw new SpruceError({ code: 'INDEX_EXISTS', index: fields });
363
391
  }
364
392
  }
365
- doesUniqueIndexExist(currentIndexes, fields) {
366
- for (const uniq of currentIndexes !== null && currentIndexes !== void 0 ? currentIndexes : []) {
367
- if (isEqual(uniq, fields)) {
393
+ doesIndexExist(currentIndexes, fields) {
394
+ for (const index of currentIndexes !== null && currentIndexes !== void 0 ? currentIndexes : []) {
395
+ if (isEqual(index, fields)) {
368
396
  return true;
369
397
  }
370
398
  }
@@ -377,7 +405,7 @@ export default class NeDbDatabase extends AbstractMutexer {
377
405
  col._uniqueIndexes = [];
378
406
  }
379
407
  yield this.randomDelay();
380
- this.assertUniqueIndexDoesNotExist(col._uniqueIndexes, fields);
408
+ this.assertIndexDoesNotExist(col._uniqueIndexes, fields);
381
409
  if (col._uniqueIndexes) {
382
410
  const tempUniqueIndexes = [...col._uniqueIndexes];
383
411
  tempUniqueIndexes.push(fields);
@@ -403,13 +431,24 @@ export default class NeDbDatabase extends AbstractMutexer {
403
431
  col._uniqueIndexes.push(fields);
404
432
  });
405
433
  }
434
+ createIndex(collection, fields) {
435
+ return __awaiter(this, void 0, void 0, function* () {
436
+ const col = this.loadCollection(collection);
437
+ if (!col._indexes) {
438
+ col._indexes = [];
439
+ }
440
+ yield this.randomDelay();
441
+ this.assertIndexDoesNotExist(col._indexes, fields);
442
+ col._indexes.push(fields);
443
+ });
444
+ }
406
445
  syncUniqueIndexes(collectionName, indexes) {
407
446
  var _a;
408
447
  return __awaiter(this, void 0, void 0, function* () {
409
448
  const currentIndexes = yield this.getUniqueIndexes(collectionName);
410
449
  const extraIndexes = differenceWith(currentIndexes, indexes, isEqual);
411
450
  for (const index of indexes) {
412
- if (!this.doesUniqueIndexExist(currentIndexes, index)) {
451
+ if (!this.doesIndexExist(currentIndexes, index)) {
413
452
  try {
414
453
  yield this.createUniqueIndex(collectionName, index);
415
454
  }
@@ -425,4 +464,26 @@ export default class NeDbDatabase extends AbstractMutexer {
425
464
  }
426
465
  });
427
466
  }
467
+ syncIndexes(collectionName, indexes) {
468
+ var _a;
469
+ return __awaiter(this, void 0, void 0, function* () {
470
+ const currentIndexes = yield this.getIndexes(collectionName);
471
+ const extraIndexes = differenceWith(currentIndexes, indexes, isEqual);
472
+ for (const index of indexes) {
473
+ if (!this.doesIndexExist(currentIndexes, index)) {
474
+ try {
475
+ yield this.createIndex(collectionName, index);
476
+ }
477
+ catch (err) {
478
+ if (((_a = err.options) === null || _a === void 0 ? void 0 : _a.code) !== 'INDEX_EXISTS') {
479
+ throw err;
480
+ }
481
+ }
482
+ }
483
+ }
484
+ for (const extra of extraIndexes) {
485
+ yield this.dropIndex(collectionName, extra);
486
+ }
487
+ });
488
+ }
428
489
  }
@@ -1,9 +1,13 @@
1
1
  import { QueryOptions } from './query.types';
2
2
  export declare type UniqueIndex = string[];
3
+ export declare type Index = string[];
3
4
  export interface Database {
5
+ [x: string]: any;
4
6
  syncUniqueIndexes(collectionName: string, indexes: UniqueIndex[]): Promise<void>;
7
+ syncIndexes(collectionName: string, indexes: Index[]): Promise<void>;
5
8
  dropIndex(collectionName: string, fields: UniqueIndex): Promise<void>;
6
9
  getUniqueIndexes(collectionName: string): Promise<UniqueIndex[]>;
10
+ getIndexes(collectionName: string): Promise<Index[] | UniqueIndex[]>;
7
11
  isConnected(): boolean;
8
12
  generateId(): string;
9
13
  connect(): Promise<void>;
@@ -21,4 +25,5 @@ export interface Database {
21
25
  deleteOne(collection: string, query: Record<string, any>): Promise<number>;
22
26
  count(collection: string, query?: Record<string, any>): Promise<number>;
23
27
  createUniqueIndex(collection: string, fields: UniqueIndex): Promise<void>;
28
+ createIndex(collection: string, fields: Index): Promise<void>;
24
29
  }
@@ -1,9 +1,13 @@
1
1
  import { QueryOptions } from './query.types';
2
2
  export declare type UniqueIndex = string[];
3
+ export declare type Index = string[];
3
4
  export interface Database {
5
+ [x: string]: any;
4
6
  syncUniqueIndexes(collectionName: string, indexes: UniqueIndex[]): Promise<void>;
7
+ syncIndexes(collectionName: string, indexes: Index[]): Promise<void>;
5
8
  dropIndex(collectionName: string, fields: UniqueIndex): Promise<void>;
6
9
  getUniqueIndexes(collectionName: string): Promise<UniqueIndex[]>;
10
+ getIndexes(collectionName: string): Promise<Index[] | UniqueIndex[]>;
7
11
  isConnected(): boolean;
8
12
  generateId(): string;
9
13
  connect(): Promise<void>;
@@ -21,4 +25,5 @@ export interface Database {
21
25
  deleteOne(collection: string, query: Record<string, any>): Promise<number>;
22
26
  count(collection: string, query?: Record<string, any>): Promise<number>;
23
27
  createUniqueIndex(collection: string, fields: UniqueIndex): Promise<void>;
28
+ createIndex(collection: string, fields: Index): Promise<void>;
24
29
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "8.0.124",
6
+ "version": "8.1.1",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",
@@ -72,22 +72,22 @@
72
72
  },
73
73
  "dependencies": {
74
74
  "@sprucelabs/error": "^5.0.402",
75
- "@sprucelabs/schema": "^26.0.115",
76
- "@sprucelabs/spruce-core-schemas": "^23.0.118",
77
- "@sprucelabs/spruce-skill-utils": "^24.1.66",
75
+ "@sprucelabs/schema": "^26.0.118",
76
+ "@sprucelabs/spruce-core-schemas": "^23.0.121",
77
+ "@sprucelabs/spruce-skill-utils": "^24.1.68",
78
78
  "globby": "^11.0.4",
79
79
  "lodash": "^4.17.21",
80
80
  "mongodb": "^4.1.0",
81
81
  "nedb": "^1.8.0"
82
82
  },
83
83
  "devDependencies": {
84
- "@sprucelabs/esm-postbuild": "^1.0.383",
84
+ "@sprucelabs/esm-postbuild": "^1.0.386",
85
85
  "@sprucelabs/jest-json-reporter": "^6.0.328",
86
86
  "@sprucelabs/jest-sheets-reporter": "^1.3.12",
87
87
  "@sprucelabs/resolve-path-aliases": "^1.0.260",
88
88
  "@sprucelabs/semantic-release": "^4.0.8",
89
89
  "@sprucelabs/test": "^7.7.281",
90
- "@sprucelabs/test-utils": "^3.0.453",
90
+ "@sprucelabs/test-utils": "^3.0.456",
91
91
  "@types/lodash": "^4.14.178",
92
92
  "@types/nedb": "^1.8.12",
93
93
  "@types/node": "17.0.5",