@sprucelabs/data-stores 25.7.9 → 25.8.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.
@@ -22,7 +22,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
22
22
  protected prepareRecord?<IncludePrivateFields extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(record: DatabaseRecord, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<PrepareResults<FullSchema, IncludePrivateFields>>;
23
23
  protected willCreate?(values: CreateRecord): Promise<Omit<DatabaseRecord, 'id'>>;
24
24
  protected didCreate?(values: CreateRecord): Promise<void>;
25
- protected willUpdate?(updates: UpdateRecord, values: DatabaseRecord): Promise<Partial<DatabaseRecord>>;
25
+ protected willUpdate?(updates: UpdateRecord, record: DatabaseRecord): Promise<Partial<DatabaseRecord>>;
26
26
  protected didUpdate?(old: DatabaseRecord, updated: DatabaseRecord): Promise<void>;
27
27
  protected willScramble?(values: Partial<DatabaseRecord> & {
28
28
  _isScrambled: true;
@@ -222,9 +222,16 @@ export default class AbstractStore extends AbstractMutexer {
222
222
  var _a, _b;
223
223
  return __awaiter(this, void 0, void 0, function* () {
224
224
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
225
+ let q = query;
225
226
  try {
226
227
  const isScrambled = this.isScrambled(initialUpdates);
227
- let current = yield this.findOne(query, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }));
228
+ for (const plugin of this.plugins) {
229
+ const results = yield ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q, initialUpdates));
230
+ if (results === null || results === void 0 ? void 0 : results.query) {
231
+ q = results.query;
232
+ }
233
+ }
234
+ let current = yield this.findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }));
228
235
  if (!current) {
229
236
  current = yield notFoundHandler();
230
237
  if (current) {
@@ -241,7 +248,7 @@ export default class AbstractStore extends AbstractMutexer {
241
248
  const cleanedUpdates = !isScrambled && this.willUpdate
242
249
  ? yield this.willUpdate(initialUpdates, current)
243
250
  : initialUpdates;
244
- const databaseRecord = Object.assign(Object.assign({}, current), cleanedUpdates);
251
+ const databaseRecord = Object.assign({}, cleanedUpdates);
245
252
  const normalizedValues = isScrambled
246
253
  ? databaseRecord
247
254
  : normalizeSchemaValues(this.databaseSchema, databaseRecord, {
@@ -251,10 +258,7 @@ export default class AbstractStore extends AbstractMutexer {
251
258
  for (const { name, value } of ops) {
252
259
  normalizedValues[name] = value;
253
260
  }
254
- for (const plugin of this.plugins) {
255
- yield ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, normalizedValues));
256
- }
257
- const results = yield this.db.updateOne(this.collectionName, query, normalizedValues);
261
+ const results = yield this.db.updateOne(this.collectionName, q, normalizedValues);
258
262
  yield ((_b = this.didUpdate) === null || _b === void 0 ? void 0 : _b.call(this, current, results));
259
263
  return this.prepareAndNormalizeRecord(results, options);
260
264
  }
@@ -319,7 +323,11 @@ export default class AbstractStore extends AbstractMutexer {
319
323
  });
320
324
  }
321
325
  deleteOne(query) {
326
+ var _a;
322
327
  return __awaiter(this, void 0, void 0, function* () {
328
+ for (const plugin of this.plugins) {
329
+ yield ((_a = plugin.willDeleteOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query));
330
+ }
323
331
  return yield this.db.deleteOne(this.collectionName, query);
324
332
  });
325
333
  }
@@ -42,6 +42,7 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
42
42
  export interface DataStorePlugin {
43
43
  willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginHookResponse>;
44
44
  willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
45
+ willDeleteOne?: (query: Record<string, any>) => Promise<void>;
45
46
  getName(): string;
46
47
  }
47
48
  export interface DataStorePluginHookResponse {
@@ -22,7 +22,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
22
22
  protected prepareRecord?<IncludePrivateFields extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(record: DatabaseRecord, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<PrepareResults<FullSchema, IncludePrivateFields>>;
23
23
  protected willCreate?(values: CreateRecord): Promise<Omit<DatabaseRecord, 'id'>>;
24
24
  protected didCreate?(values: CreateRecord): Promise<void>;
25
- protected willUpdate?(updates: UpdateRecord, values: DatabaseRecord): Promise<Partial<DatabaseRecord>>;
25
+ protected willUpdate?(updates: UpdateRecord, record: DatabaseRecord): Promise<Partial<DatabaseRecord>>;
26
26
  protected didUpdate?(old: DatabaseRecord, updated: DatabaseRecord): Promise<void>;
27
27
  protected willScramble?(values: Partial<DatabaseRecord> & {
28
28
  _isScrambled: true;
@@ -220,9 +220,16 @@ class AbstractStore extends AbstractMutexer_1.default {
220
220
  async findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
221
221
  var _a, _b;
222
222
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
223
+ let q = query;
223
224
  try {
224
225
  const isScrambled = this.isScrambled(initialUpdates);
225
- let current = await this.findOne(query, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }));
226
+ for (const plugin of this.plugins) {
227
+ const results = await ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q, initialUpdates));
228
+ if (results === null || results === void 0 ? void 0 : results.query) {
229
+ q = results.query;
230
+ }
231
+ }
232
+ let current = await this.findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }));
226
233
  if (!current) {
227
234
  current = await notFoundHandler();
228
235
  if (current) {
@@ -239,7 +246,7 @@ class AbstractStore extends AbstractMutexer_1.default {
239
246
  const cleanedUpdates = !isScrambled && this.willUpdate
240
247
  ? await this.willUpdate(initialUpdates, current)
241
248
  : initialUpdates;
242
- const databaseRecord = Object.assign(Object.assign({}, current), cleanedUpdates);
249
+ const databaseRecord = Object.assign({}, cleanedUpdates);
243
250
  const normalizedValues = isScrambled
244
251
  ? databaseRecord
245
252
  : (0, schema_1.normalizeSchemaValues)(this.databaseSchema, databaseRecord, {
@@ -249,10 +256,7 @@ class AbstractStore extends AbstractMutexer_1.default {
249
256
  for (const { name, value } of ops) {
250
257
  normalizedValues[name] = value;
251
258
  }
252
- for (const plugin of this.plugins) {
253
- await ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, normalizedValues));
254
- }
255
- const results = await this.db.updateOne(this.collectionName, query, normalizedValues);
259
+ const results = await this.db.updateOne(this.collectionName, q, normalizedValues);
256
260
  await ((_b = this.didUpdate) === null || _b === void 0 ? void 0 : _b.call(this, current, results));
257
261
  return this.prepareAndNormalizeRecord(results, options);
258
262
  }
@@ -312,6 +316,10 @@ class AbstractStore extends AbstractMutexer_1.default {
312
316
  return cleanedValues;
313
317
  }
314
318
  async deleteOne(query) {
319
+ var _a;
320
+ for (const plugin of this.plugins) {
321
+ await ((_a = plugin.willDeleteOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query));
322
+ }
315
323
  return await this.db.deleteOne(this.collectionName, query);
316
324
  }
317
325
  async delete(query) {
@@ -42,6 +42,7 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
42
42
  export interface DataStorePlugin {
43
43
  willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginHookResponse>;
44
44
  willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
45
+ willDeleteOne?: (query: Record<string, any>) => Promise<void>;
45
46
  getName(): string;
46
47
  }
47
48
  export interface DataStorePluginHookResponse {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "25.7.9",
6
+ "version": "25.8.0",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",
@@ -68,10 +68,10 @@
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.40",
71
+ "@sprucelabs/error": "^5.1.41",
72
72
  "@sprucelabs/globby": "^1.0.3",
73
- "@sprucelabs/schema": "^29.0.78",
74
- "@sprucelabs/spruce-skill-utils": "^30.1.12",
73
+ "@sprucelabs/schema": "^29.0.79",
74
+ "@sprucelabs/spruce-skill-utils": "^30.1.13",
75
75
  "just-clone": "^6.2.0",
76
76
  "lodash": "^4.17.21",
77
77
  "mongodb": "^6.2.0",
@@ -86,7 +86,7 @@
86
86
  "@sprucelabs/test": "^8.0.25",
87
87
  "@sprucelabs/test-utils": "^4.0.67",
88
88
  "@types/lodash": "^4.14.200",
89
- "@types/nedb": "^1.8.15",
89
+ "@types/nedb": "^1.8.16",
90
90
  "@types/node": "^20.8.10",
91
91
  "chokidar-cli": "^3.0.0",
92
92
  "concurrently": "^8.2.2",