@sprucelabs/data-stores 26.1.0 → 26.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.
@@ -39,9 +39,11 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
39
39
  private handleDidCreateForPlugins;
40
40
  private get primaryFieldName();
41
41
  findOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>> | null>;
42
+ private _findOne;
42
43
  private handleDidFindForPlugins;
43
44
  count(query?: QueryBuilder<QueryRecord>): Promise<number>;
44
45
  find<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, queryOptions?: Omit<QueryOptions, 'includeFields'>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Awaited<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>>>[]>;
46
+ private _find;
45
47
  findBatch<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query?: QueryBuilder<QueryRecord>, options?: FindBatchOptions<IncludePrivateFields, FullSchema, F>): Promise<import("../cursors/BatchCursor").BatchCursor<SchemaValues<FullSchema, CreateEntityInstances, IncludePrivateFields, false, F, PF>>>;
46
48
  upsertOne<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: UpdateRecord & CreateRecord & {
47
49
  id?: string;
@@ -169,7 +169,14 @@ export default class AbstractStore extends AbstractMutexer {
169
169
  }
170
170
  findOne(query, options = {}) {
171
171
  return __awaiter(this, void 0, void 0, function* () {
172
- const results = yield this.find(query, { limit: 1 }, options);
172
+ return this._findOne(query, options);
173
+ });
174
+ }
175
+ _findOne(query, options = {}, internalOptions = {
176
+ shouldTriggerWillQuery: true,
177
+ }) {
178
+ return __awaiter(this, void 0, void 0, function* () {
179
+ const results = yield this._find(query, { limit: 1 }, options, internalOptions);
173
180
  let match = results[0];
174
181
  if (match) {
175
182
  match = (yield this.handleDidFindForPlugins(query, match));
@@ -197,9 +204,20 @@ export default class AbstractStore extends AbstractMutexer {
197
204
  });
198
205
  }
199
206
  find(query, queryOptions, options) {
207
+ return __awaiter(this, void 0, void 0, function* () {
208
+ return this._find(query, queryOptions, options);
209
+ });
210
+ }
211
+ _find(query, queryOptions, options, internalOptions = {
212
+ shouldTriggerWillQuery: true,
213
+ }) {
200
214
  var _a, _b;
201
215
  return __awaiter(this, void 0, void 0, function* () {
202
- const mappedQuery = (_b = (yield ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query;
216
+ let mappedQuery = query;
217
+ const { shouldTriggerWillQuery } = internalOptions;
218
+ if (shouldTriggerWillQuery) {
219
+ mappedQuery = ((_b = (yield ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query);
220
+ }
203
221
  const results = yield this.db.find(this.collectionName, mappedQuery, Object.assign(Object.assign({}, queryOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }));
204
222
  if (results) {
205
223
  const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
@@ -263,15 +281,15 @@ export default class AbstractStore extends AbstractMutexer {
263
281
  });
264
282
  }
265
283
  findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
266
- var _a, _b;
284
+ var _a, _b, _c, _d;
267
285
  return __awaiter(this, void 0, void 0, function* () {
268
286
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
269
- let q = query;
287
+ let q = (_b = (yield ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : query;
270
288
  let shouldUpdate = true;
271
289
  try {
272
290
  const isScrambled = this.isScrambled(initialUpdates);
273
291
  for (const plugin of this.plugins) {
274
- const results = yield ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q, initialUpdates));
292
+ const results = yield ((_c = plugin.willUpdateOne) === null || _c === void 0 ? void 0 : _c.call(plugin, q, initialUpdates));
275
293
  if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
276
294
  shouldUpdate = false;
277
295
  }
@@ -279,7 +297,7 @@ export default class AbstractStore extends AbstractMutexer {
279
297
  q = results.query;
280
298
  }
281
299
  }
282
- let current = yield this.findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }));
300
+ let current = yield this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
283
301
  if (!current) {
284
302
  current = yield notFoundHandler();
285
303
  if (current) {
@@ -310,7 +328,7 @@ export default class AbstractStore extends AbstractMutexer {
310
328
  normalizedValues[name] = value;
311
329
  }
312
330
  const results = yield this.db.updateOne(this.collectionName, q, normalizedValues);
313
- yield ((_b = this.didUpdate) === null || _b === void 0 ? void 0 : _b.call(this, current, results));
331
+ yield ((_d = this.didUpdate) === null || _d === void 0 ? void 0 : _d.call(this, current, results));
314
332
  return this.prepareAndNormalizeRecord(results, options);
315
333
  }
316
334
  catch (err) {
@@ -374,11 +392,11 @@ export default class AbstractStore extends AbstractMutexer {
374
392
  });
375
393
  }
376
394
  deleteOne(query) {
377
- var _a, _b;
395
+ var _a, _b, _c, _d;
378
396
  return __awaiter(this, void 0, void 0, function* () {
379
- let q = Object.assign({}, query);
397
+ let q = (_b = (yield ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : Object.assign({}, query);
380
398
  for (const plugin of this.plugins) {
381
- const { query } = (_b = (yield ((_a = plugin.willDeleteOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q)))) !== null && _b !== void 0 ? _b : {};
399
+ const { query } = (_d = (yield ((_c = plugin.willDeleteOne) === null || _c === void 0 ? void 0 : _c.call(plugin, q)))) !== null && _d !== void 0 ? _d : {};
382
400
  if (query) {
383
401
  q = query;
384
402
  }
@@ -387,8 +405,10 @@ export default class AbstractStore extends AbstractMutexer {
387
405
  });
388
406
  }
389
407
  delete(query) {
408
+ var _a, _b;
390
409
  return __awaiter(this, void 0, void 0, function* () {
391
- return yield this.db.delete(this.collectionName, query);
410
+ const q = (_b = (yield ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : Object.assign({}, query);
411
+ return yield this.db.delete(this.collectionName, q);
392
412
  });
393
413
  }
394
414
  }
@@ -39,9 +39,11 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
39
39
  private handleDidCreateForPlugins;
40
40
  private get primaryFieldName();
41
41
  findOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>> | null>;
42
+ private _findOne;
42
43
  private handleDidFindForPlugins;
43
44
  count(query?: QueryBuilder<QueryRecord>): Promise<number>;
44
45
  find<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, queryOptions?: Omit<QueryOptions, 'includeFields'>, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Awaited<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>>>[]>;
46
+ private _find;
45
47
  findBatch<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query?: QueryBuilder<QueryRecord>, options?: FindBatchOptions<IncludePrivateFields, FullSchema, F>): Promise<import("../cursors/BatchCursor").BatchCursor<SchemaValues<FullSchema, CreateEntityInstances, IncludePrivateFields, false, F, PF>>>;
46
48
  upsertOne<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: UpdateRecord & CreateRecord & {
47
49
  id?: string;
@@ -177,7 +177,12 @@ class AbstractStore extends AbstractMutexer_1.default {
177
177
  return this.primaryFieldNames[0];
178
178
  }
179
179
  async findOne(query, options = {}) {
180
- const results = await this.find(query, { limit: 1 }, options);
180
+ return this._findOne(query, options);
181
+ }
182
+ async _findOne(query, options = {}, internalOptions = {
183
+ shouldTriggerWillQuery: true,
184
+ }) {
185
+ const results = await this._find(query, { limit: 1 }, options, internalOptions);
181
186
  let match = results[0];
182
187
  if (match) {
183
188
  match = (await this.handleDidFindForPlugins(query, match));
@@ -200,8 +205,17 @@ class AbstractStore extends AbstractMutexer_1.default {
200
205
  return count;
201
206
  }
202
207
  async find(query, queryOptions, options) {
208
+ return this._find(query, queryOptions, options);
209
+ }
210
+ async _find(query, queryOptions, options, internalOptions = {
211
+ shouldTriggerWillQuery: true,
212
+ }) {
203
213
  var _a, _b;
204
- const mappedQuery = (_b = (await ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query;
214
+ let mappedQuery = query;
215
+ const { shouldTriggerWillQuery } = internalOptions;
216
+ if (shouldTriggerWillQuery) {
217
+ mappedQuery = ((_b = (await ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query);
218
+ }
205
219
  const results = await this.db.find(this.collectionName, mappedQuery, Object.assign(Object.assign({}, queryOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }));
206
220
  if (results) {
207
221
  const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
@@ -256,14 +270,14 @@ class AbstractStore extends AbstractMutexer_1.default {
256
270
  return this.db.update(this.collectionName, query, updates);
257
271
  }
258
272
  async findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
259
- var _a, _b;
273
+ var _a, _b, _c, _d;
260
274
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
261
- let q = query;
275
+ let q = (_b = (await ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : query;
262
276
  let shouldUpdate = true;
263
277
  try {
264
278
  const isScrambled = this.isScrambled(initialUpdates);
265
279
  for (const plugin of this.plugins) {
266
- const results = await ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q, initialUpdates));
280
+ const results = await ((_c = plugin.willUpdateOne) === null || _c === void 0 ? void 0 : _c.call(plugin, q, initialUpdates));
267
281
  if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
268
282
  shouldUpdate = false;
269
283
  }
@@ -271,7 +285,7 @@ class AbstractStore extends AbstractMutexer_1.default {
271
285
  q = results.query;
272
286
  }
273
287
  }
274
- let current = await this.findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }));
288
+ let current = await this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
275
289
  if (!current) {
276
290
  current = await notFoundHandler();
277
291
  if (current) {
@@ -302,7 +316,7 @@ class AbstractStore extends AbstractMutexer_1.default {
302
316
  normalizedValues[name] = value;
303
317
  }
304
318
  const results = await this.db.updateOne(this.collectionName, q, normalizedValues);
305
- await ((_b = this.didUpdate) === null || _b === void 0 ? void 0 : _b.call(this, current, results));
319
+ await ((_d = this.didUpdate) === null || _d === void 0 ? void 0 : _d.call(this, current, results));
306
320
  return this.prepareAndNormalizeRecord(results, options);
307
321
  }
308
322
  catch (err) {
@@ -361,10 +375,10 @@ class AbstractStore extends AbstractMutexer_1.default {
361
375
  return cleanedValues;
362
376
  }
363
377
  async deleteOne(query) {
364
- var _a, _b;
365
- let q = Object.assign({}, query);
378
+ var _a, _b, _c, _d;
379
+ let q = (_b = (await ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : Object.assign({}, query);
366
380
  for (const plugin of this.plugins) {
367
- const { query } = (_b = (await ((_a = plugin.willDeleteOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q)))) !== null && _b !== void 0 ? _b : {};
381
+ const { query } = (_d = (await ((_c = plugin.willDeleteOne) === null || _c === void 0 ? void 0 : _c.call(plugin, q)))) !== null && _d !== void 0 ? _d : {};
368
382
  if (query) {
369
383
  q = query;
370
384
  }
@@ -372,7 +386,9 @@ class AbstractStore extends AbstractMutexer_1.default {
372
386
  return await this.db.deleteOne(this.collectionName, q);
373
387
  }
374
388
  async delete(query) {
375
- return await this.db.delete(this.collectionName, query);
389
+ var _a, _b;
390
+ const q = (_b = (await ((_a = this.willQuery) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : Object.assign({}, query);
391
+ return await this.db.delete(this.collectionName, q);
376
392
  }
377
393
  }
378
394
  exports.default = AbstractStore;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "26.1.0",
6
+ "version": "26.1.1",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",
@@ -68,23 +68,23 @@
68
68
  "upgrade.packages.test": "yarn upgrade.packages.all && yarn lint && yarn build.dev && yarn test"
69
69
  },
70
70
  "dependencies": {
71
- "@sprucelabs/error": "^5.1.49",
71
+ "@sprucelabs/error": "^5.1.50",
72
72
  "@sprucelabs/globby": "^1.0.3",
73
- "@sprucelabs/schema": "^29.0.92",
74
- "@sprucelabs/spruce-skill-utils": "^30.1.34",
73
+ "@sprucelabs/schema": "^29.0.93",
74
+ "@sprucelabs/spruce-skill-utils": "^30.1.35",
75
75
  "just-clone": "^6.2.0",
76
76
  "lodash": "^4.17.21",
77
77
  "mongodb": "^6.3.0",
78
78
  "nedb": "^1.8.0"
79
79
  },
80
80
  "devDependencies": {
81
- "@sprucelabs/esm-postbuild": "^5.0.103",
82
- "@sprucelabs/jest-json-reporter": "^7.0.128",
81
+ "@sprucelabs/esm-postbuild": "^5.0.104",
82
+ "@sprucelabs/jest-json-reporter": "^7.0.129",
83
83
  "@sprucelabs/jest-sheets-reporter": "^3.0.26",
84
- "@sprucelabs/resolve-path-aliases": "^1.1.266",
84
+ "@sprucelabs/resolve-path-aliases": "^1.1.267",
85
85
  "@sprucelabs/semantic-release": "^4.0.8",
86
- "@sprucelabs/test": "^8.0.32",
87
- "@sprucelabs/test-utils": "^4.0.81",
86
+ "@sprucelabs/test": "^8.0.33",
87
+ "@sprucelabs/test-utils": "^4.0.82",
88
88
  "@types/lodash": "^4.14.202",
89
89
  "@types/nedb": "^1.8.16",
90
90
  "@types/node": "^20.10.3",