@sprucelabs/data-stores 26.0.1 → 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.
@@ -25,6 +25,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
25
25
  protected willScramble?(values: Partial<DatabaseRecord> & {
26
26
  _isScrambled: true;
27
27
  }): Promise<Partial<DatabaseRecord>>;
28
+ protected willQuery?(query: QueryBuilder<QueryRecord>): Promise<QueryBuilder<Partial<DatabaseRecord>>>;
28
29
  protected constructor(db: Database, collectionName?: string);
29
30
  protected setCollectionName(name: string): void;
30
31
  getDb(): Database;
@@ -38,9 +39,11 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
38
39
  private handleDidCreateForPlugins;
39
40
  private get primaryFieldName();
40
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;
41
43
  private handleDidFindForPlugins;
42
44
  count(query?: QueryBuilder<QueryRecord>): Promise<number>;
43
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;
44
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>>>;
45
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 & {
46
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));
@@ -198,7 +205,20 @@ export default class AbstractStore extends AbstractMutexer {
198
205
  }
199
206
  find(query, queryOptions, options) {
200
207
  return __awaiter(this, void 0, void 0, function* () {
201
- const results = yield this.db.find(this.collectionName, query, Object.assign(Object.assign({}, queryOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }));
208
+ return this._find(query, queryOptions, options);
209
+ });
210
+ }
211
+ _find(query, queryOptions, options, internalOptions = {
212
+ shouldTriggerWillQuery: true,
213
+ }) {
214
+ var _a, _b;
215
+ return __awaiter(this, void 0, void 0, function* () {
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
+ }
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 }));
202
222
  if (results) {
203
223
  const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
204
224
  const records = yield Promise.all(all);
@@ -261,15 +281,15 @@ export default class AbstractStore extends AbstractMutexer {
261
281
  });
262
282
  }
263
283
  findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
264
- var _a, _b;
284
+ var _a, _b, _c, _d;
265
285
  return __awaiter(this, void 0, void 0, function* () {
266
286
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
267
- 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;
268
288
  let shouldUpdate = true;
269
289
  try {
270
290
  const isScrambled = this.isScrambled(initialUpdates);
271
291
  for (const plugin of this.plugins) {
272
- 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));
273
293
  if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
274
294
  shouldUpdate = false;
275
295
  }
@@ -277,7 +297,7 @@ export default class AbstractStore extends AbstractMutexer {
277
297
  q = results.query;
278
298
  }
279
299
  }
280
- 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 });
281
301
  if (!current) {
282
302
  current = yield notFoundHandler();
283
303
  if (current) {
@@ -308,7 +328,7 @@ export default class AbstractStore extends AbstractMutexer {
308
328
  normalizedValues[name] = value;
309
329
  }
310
330
  const results = yield this.db.updateOne(this.collectionName, q, normalizedValues);
311
- 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));
312
332
  return this.prepareAndNormalizeRecord(results, options);
313
333
  }
314
334
  catch (err) {
@@ -372,11 +392,11 @@ export default class AbstractStore extends AbstractMutexer {
372
392
  });
373
393
  }
374
394
  deleteOne(query) {
375
- var _a, _b;
395
+ var _a, _b, _c, _d;
376
396
  return __awaiter(this, void 0, void 0, function* () {
377
- 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);
378
398
  for (const plugin of this.plugins) {
379
- 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 : {};
380
400
  if (query) {
381
401
  q = query;
382
402
  }
@@ -385,8 +405,10 @@ export default class AbstractStore extends AbstractMutexer {
385
405
  });
386
406
  }
387
407
  delete(query) {
408
+ var _a, _b;
388
409
  return __awaiter(this, void 0, void 0, function* () {
389
- 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);
390
412
  });
391
413
  }
392
414
  }
@@ -25,6 +25,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
25
25
  protected willScramble?(values: Partial<DatabaseRecord> & {
26
26
  _isScrambled: true;
27
27
  }): Promise<Partial<DatabaseRecord>>;
28
+ protected willQuery?(query: QueryBuilder<QueryRecord>): Promise<QueryBuilder<Partial<DatabaseRecord>>>;
28
29
  protected constructor(db: Database, collectionName?: string);
29
30
  protected setCollectionName(name: string): void;
30
31
  getDb(): Database;
@@ -38,9 +39,11 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
38
39
  private handleDidCreateForPlugins;
39
40
  private get primaryFieldName();
40
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;
41
43
  private handleDidFindForPlugins;
42
44
  count(query?: QueryBuilder<QueryRecord>): Promise<number>;
43
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;
44
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>>>;
45
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 & {
46
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,7 +205,18 @@ class AbstractStore extends AbstractMutexer_1.default {
200
205
  return count;
201
206
  }
202
207
  async find(query, queryOptions, options) {
203
- const results = await this.db.find(this.collectionName, query, Object.assign(Object.assign({}, queryOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }));
208
+ return this._find(query, queryOptions, options);
209
+ }
210
+ async _find(query, queryOptions, options, internalOptions = {
211
+ shouldTriggerWillQuery: true,
212
+ }) {
213
+ var _a, _b;
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
+ }
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 }));
204
220
  if (results) {
205
221
  const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
206
222
  const records = await Promise.all(all);
@@ -254,14 +270,14 @@ class AbstractStore extends AbstractMutexer_1.default {
254
270
  return this.db.update(this.collectionName, query, updates);
255
271
  }
256
272
  async findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
257
- var _a, _b;
273
+ var _a, _b, _c, _d;
258
274
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
259
- 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;
260
276
  let shouldUpdate = true;
261
277
  try {
262
278
  const isScrambled = this.isScrambled(initialUpdates);
263
279
  for (const plugin of this.plugins) {
264
- 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));
265
281
  if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
266
282
  shouldUpdate = false;
267
283
  }
@@ -269,7 +285,7 @@ class AbstractStore extends AbstractMutexer_1.default {
269
285
  q = results.query;
270
286
  }
271
287
  }
272
- 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 });
273
289
  if (!current) {
274
290
  current = await notFoundHandler();
275
291
  if (current) {
@@ -300,7 +316,7 @@ class AbstractStore extends AbstractMutexer_1.default {
300
316
  normalizedValues[name] = value;
301
317
  }
302
318
  const results = await this.db.updateOne(this.collectionName, q, normalizedValues);
303
- 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));
304
320
  return this.prepareAndNormalizeRecord(results, options);
305
321
  }
306
322
  catch (err) {
@@ -359,10 +375,10 @@ class AbstractStore extends AbstractMutexer_1.default {
359
375
  return cleanedValues;
360
376
  }
361
377
  async deleteOne(query) {
362
- var _a, _b;
363
- 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);
364
380
  for (const plugin of this.plugins) {
365
- 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 : {};
366
382
  if (query) {
367
383
  q = query;
368
384
  }
@@ -370,7 +386,9 @@ class AbstractStore extends AbstractMutexer_1.default {
370
386
  return await this.db.deleteOne(this.collectionName, q);
371
387
  }
372
388
  async delete(query) {
373
- 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);
374
392
  }
375
393
  }
376
394
  exports.default = AbstractStore;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "26.0.1",
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",