@sprucelabs/data-stores 28.1.11 → 28.1.13

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.
@@ -26,7 +26,6 @@ const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
26
26
  const AbstractMutexer_1 = __importDefault(require("../mutexers/AbstractMutexer"));
27
27
  const generateId_1 = __importDefault(require("../utilities/generateId"));
28
28
  const mongo_utility_1 = __importDefault(require("../utilities/mongo.utility"));
29
- const mapIndexFilterToNeDbQuery_1 = __importDefault(require("./mapIndexFilterToNeDbQuery"));
30
29
  const normalizeIndex_1 = __importDefault(require("./normalizeIndex"));
31
30
  dotenv_1.default.config();
32
31
  class NeDbDatabase extends AbstractMutexer_1.default {
@@ -292,22 +291,29 @@ class NeDbDatabase extends AbstractMutexer_1.default {
292
291
  if (col._uniqueIndexes) {
293
292
  for (const index of col._uniqueIndexes) {
294
293
  const { fields, filter } = (0, normalizeIndex_1.default)(index);
294
+ if (filter) {
295
+ let shouldSkip = false;
296
+ for (const key in filter) {
297
+ if (values[key] === NULL_PLACEHOLDER) {
298
+ shouldSkip = true;
299
+ break;
300
+ }
301
+ }
302
+ if (shouldSkip) {
303
+ continue;
304
+ }
305
+ }
295
306
  const existing = query
296
307
  ? await this.findOne(collection, query)
297
308
  : null;
298
- let q = filter
299
- ? (0, mapIndexFilterToNeDbQuery_1.default)(filter)
300
- : {};
309
+ let q = {};
301
310
  const duplicateFields = [];
302
311
  const duplicateValues = [];
303
312
  fields.forEach((f) => {
304
313
  let value = (0, get_1.default)(values, f);
305
- if (value === NULL_PLACEHOLDER && q[f]) {
306
- value = q[f];
307
- }
308
- q[f] = value;
309
314
  duplicateFields.push(f);
310
- duplicateValues.push(q[f]);
315
+ duplicateValues.push(value);
316
+ q[f] = value;
311
317
  });
312
318
  const destination = await this.findOne(collection, q);
313
319
  if (destination && (existing === null || existing === void 0 ? void 0 : existing.id) !== destination.id) {
@@ -30,7 +30,6 @@ import SpruceError from '../errors/SpruceError.js';
30
30
  import AbstractMutexer from '../mutexers/AbstractMutexer.js';
31
31
  import generateId from '../utilities/generateId.js';
32
32
  import mongoUtil from '../utilities/mongo.utility.js';
33
- import mapIndexFilterToNeDbQuery from './mapIndexFilterToNeDbQuery.js';
34
33
  import normalizeIndex from './normalizeIndex.js';
35
34
  dotenv.config();
36
35
  export default class NeDbDatabase extends AbstractMutexer {
@@ -327,22 +326,29 @@ export default class NeDbDatabase extends AbstractMutexer {
327
326
  if (col._uniqueIndexes) {
328
327
  for (const index of col._uniqueIndexes) {
329
328
  const { fields, filter } = normalizeIndex(index);
329
+ if (filter) {
330
+ let shouldSkip = false;
331
+ for (const key in filter) {
332
+ if (values[key] === NULL_PLACEHOLDER) {
333
+ shouldSkip = true;
334
+ break;
335
+ }
336
+ }
337
+ if (shouldSkip) {
338
+ continue;
339
+ }
340
+ }
330
341
  const existing = query
331
342
  ? yield this.findOne(collection, query)
332
343
  : null;
333
- let q = filter
334
- ? mapIndexFilterToNeDbQuery(filter)
335
- : {};
344
+ let q = {};
336
345
  const duplicateFields = [];
337
346
  const duplicateValues = [];
338
347
  fields.forEach((f) => {
339
348
  let value = get(values, f);
340
- if (value === NULL_PLACEHOLDER && q[f]) {
341
- value = q[f];
342
- }
343
- q[f] = value;
344
349
  duplicateFields.push(f);
345
- duplicateValues.push(q[f]);
350
+ duplicateValues.push(value);
351
+ q[f] = value;
346
352
  });
347
353
  const destination = yield this.findOne(collection, q);
348
354
  if (destination && (existing === null || existing === void 0 ? void 0 : existing.id) !== destination.id) {
@@ -1469,14 +1469,14 @@ const databaseAssertUtil = {
1469
1469
  },
1470
1470
  assertCanSyncUniqueIndexesWithFilterExpression(connect) {
1471
1471
  return __awaiter(this, void 0, void 0, function* () {
1472
- var _a, _b, _c, _d;
1472
+ var _a, _b, _c, _d, _e;
1473
1473
  const db = yield connectToDabatase(connect);
1474
1474
  try {
1475
1475
  yield db.syncUniqueIndexes(this.collectionName, [
1476
1476
  {
1477
1477
  fields: ['uniqueField', 'someField3'],
1478
1478
  filter: {
1479
- uniqueField: { $exists: true },
1479
+ uniqueField: { $type: 'string' },
1480
1480
  },
1481
1481
  },
1482
1482
  ]);
@@ -1518,13 +1518,13 @@ const databaseAssertUtil = {
1518
1518
  {
1519
1519
  fields: ['uniqueField', 'someField3'],
1520
1520
  filter: {
1521
- uniqueField: { $exists: true },
1521
+ uniqueField: { $type: 'string' },
1522
1522
  },
1523
1523
  },
1524
1524
  {
1525
1525
  fields: ['slug', 'someField3'],
1526
1526
  filter: {
1527
- slug: { $exists: true, $type: 'string' },
1527
+ slug: { $type: 'string' },
1528
1528
  },
1529
1529
  },
1530
1530
  ]);
@@ -1548,9 +1548,27 @@ const databaseAssertUtil = {
1548
1548
  });
1549
1549
  }
1550
1550
  catch (err) {
1551
- assert.fail((_d = `createOne() should not throw if index has filter { \$exists: true, \$type: 'string' }.\n\n` +
1551
+ assert.fail((_d = `createOne() should not throw since index has filter { slug: { \$type: 'string' } }.\n\n` +
1552
1552
  err.stack) !== null && _d !== void 0 ? _d : err.message);
1553
1553
  }
1554
+ try {
1555
+ yield db.createOne(this.collectionName, {
1556
+ name: generateId(),
1557
+ uniqueField: null,
1558
+ slug: '555-000-0002',
1559
+ someField3: 'next',
1560
+ });
1561
+ }
1562
+ catch (err) {
1563
+ assert.fail((_e = `createOne() should not throw since index has filter {uniqueField: { \$type: 'string' } }.\n\n` +
1564
+ err.stack) !== null && _e !== void 0 ? _e : err.message);
1565
+ }
1566
+ yield assert.doesThrowAsync(() => db.createOne(this.collectionName, {
1567
+ name: generateId(),
1568
+ uniqueField: null,
1569
+ slug: '555-000-0002',
1570
+ someField3: 'next',
1571
+ }), undefined, `createOne() should throw since index has filter { slug: { \$type: 'string' } }.`);
1554
1572
  yield this.shutdown(db);
1555
1573
  });
1556
1574
  },
@@ -1344,14 +1344,14 @@ const databaseAssertUtil = {
1344
1344
  await this.shutdown(db);
1345
1345
  },
1346
1346
  async assertCanSyncUniqueIndexesWithFilterExpression(connect) {
1347
- var _a, _b, _c, _d;
1347
+ var _a, _b, _c, _d, _e;
1348
1348
  const db = await connectToDabatase(connect);
1349
1349
  try {
1350
1350
  await db.syncUniqueIndexes(this.collectionName, [
1351
1351
  {
1352
1352
  fields: ['uniqueField', 'someField3'],
1353
1353
  filter: {
1354
- uniqueField: { $exists: true },
1354
+ uniqueField: { $type: 'string' },
1355
1355
  },
1356
1356
  },
1357
1357
  ]);
@@ -1393,13 +1393,13 @@ const databaseAssertUtil = {
1393
1393
  {
1394
1394
  fields: ['uniqueField', 'someField3'],
1395
1395
  filter: {
1396
- uniqueField: { $exists: true },
1396
+ uniqueField: { $type: 'string' },
1397
1397
  },
1398
1398
  },
1399
1399
  {
1400
1400
  fields: ['slug', 'someField3'],
1401
1401
  filter: {
1402
- slug: { $exists: true, $type: 'string' },
1402
+ slug: { $type: 'string' },
1403
1403
  },
1404
1404
  },
1405
1405
  ]);
@@ -1423,9 +1423,27 @@ const databaseAssertUtil = {
1423
1423
  });
1424
1424
  }
1425
1425
  catch (err) {
1426
- test_utils_1.assert.fail((_d = `createOne() should not throw if index has filter { \$exists: true, \$type: 'string' }.\n\n` +
1426
+ test_utils_1.assert.fail((_d = `createOne() should not throw since index has filter { slug: { \$type: 'string' } }.\n\n` +
1427
1427
  err.stack) !== null && _d !== void 0 ? _d : err.message);
1428
1428
  }
1429
+ try {
1430
+ await db.createOne(this.collectionName, {
1431
+ name: (0, generateId_1.default)(),
1432
+ uniqueField: null,
1433
+ slug: '555-000-0002',
1434
+ someField3: 'next',
1435
+ });
1436
+ }
1437
+ catch (err) {
1438
+ test_utils_1.assert.fail((_e = `createOne() should not throw since index has filter {uniqueField: { \$type: 'string' } }.\n\n` +
1439
+ err.stack) !== null && _e !== void 0 ? _e : err.message);
1440
+ }
1441
+ await test_utils_1.assert.doesThrowAsync(() => db.createOne(this.collectionName, {
1442
+ name: (0, generateId_1.default)(),
1443
+ uniqueField: null,
1444
+ slug: '555-000-0002',
1445
+ someField3: 'next',
1446
+ }), undefined, `createOne() should throw since index has filter { slug: { \$type: 'string' } }.`);
1429
1447
  await this.shutdown(db);
1430
1448
  },
1431
1449
  async assertSyncIndexesRemovesExtraIndexes(connect) {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "28.1.11",
6
+ "version": "28.1.13",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",
@@ -69,23 +69,23 @@
69
69
  "dependencies": {
70
70
  "@sprucelabs/error": "^6.0.36",
71
71
  "@sprucelabs/globby": "^2.0.17",
72
- "@sprucelabs/schema": "^30.0.65",
73
- "@sprucelabs/spruce-skill-utils": "^31.0.71",
72
+ "@sprucelabs/schema": "^30.0.66",
73
+ "@sprucelabs/spruce-skill-utils": "^31.0.72",
74
74
  "just-clone": "^6.2.0",
75
75
  "lodash": "^4.17.21",
76
76
  "mongodb": "^6.7.0",
77
77
  "nedb": "^1.8.0"
78
78
  },
79
79
  "devDependencies": {
80
- "@sprucelabs/esm-postbuild": "^6.0.28",
81
- "@sprucelabs/jest-json-reporter": "^8.0.40",
80
+ "@sprucelabs/esm-postbuild": "^6.0.29",
81
+ "@sprucelabs/jest-json-reporter": "^8.0.41",
82
82
  "@sprucelabs/resolve-path-aliases": "^2.0.29",
83
83
  "@sprucelabs/semantic-release": "^5.0.1",
84
84
  "@sprucelabs/test": "^9.0.19",
85
- "@sprucelabs/test-utils": "^5.0.54",
85
+ "@sprucelabs/test-utils": "^5.0.55",
86
86
  "@types/lodash": "^4.17.4",
87
87
  "@types/nedb": "^1.8.16",
88
- "@types/node": "^20.12.13",
88
+ "@types/node": "^20.13.0",
89
89
  "chokidar-cli": "^3.0.0",
90
90
  "concurrently": "^8.2.2",
91
91
  "dotenv": "^16.4.5",