@sprucelabs/data-stores 26.2.0 → 26.2.2

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.
@@ -20,6 +20,7 @@ export { default as CursorPager } from './cursors/CursorPager';
20
20
  export * from './cursors/CursorPager';
21
21
  export { default as CursorPagerFaker } from './cursors/CursorPagerFaker';
22
22
  export * from './cursors/CursorPager';
23
+ export { default as DatabaseFieldMapperPlugin } from './plugins/DatabaseFieldMapperPlugin';
23
24
  /**
24
25
  * @deprecated databaseAssertUtil -> databaseAssert
25
26
  */
@@ -20,6 +20,7 @@ export { default as CursorPager } from './cursors/CursorPager.js';
20
20
  export * from './cursors/CursorPager.js';
21
21
  export { default as CursorPagerFaker } from './cursors/CursorPagerFaker.js';
22
22
  export * from './cursors/CursorPager.js';
23
+ export { default as DatabaseFieldMapperPlugin } from './plugins/DatabaseFieldMapperPlugin.js';
23
24
  /**
24
25
  * @deprecated databaseAssertUtil -> databaseAssert
25
26
  */
@@ -1,10 +1,11 @@
1
1
  import { QueryOptions } from '../types/query.types';
2
- import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse } from '../types/stores.types';
2
+ import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse, DataStorePluginWillUpdateOneResponse } from '../types/stores.types';
3
3
  export default class DatabaseFieldMapperPlugin implements DataStorePlugin {
4
4
  private mapper;
5
5
  constructor(map: Record<string, any>);
6
6
  getName(): string;
7
7
  willCreateOne(values: Record<string, any>): Promise<void | DataStorePluginWillCreateOneResponse>;
8
8
  prepareRecord(record: Record<string, any>): Promise<void | DataStorePluginPrepareResponse>;
9
+ willUpdateOne(query: Record<string, any>, updates: Record<string, any>): Promise<void | DataStorePluginWillUpdateOneResponse>;
9
10
  willFind(query: Record<string, any>, options?: QueryOptions): Promise<void | DataStorePluginWillFindResponse>;
10
11
  }
@@ -29,6 +29,14 @@ export default class DatabaseFieldMapperPlugin {
29
29
  };
30
30
  });
31
31
  }
32
+ willUpdateOne(query, updates) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ return {
35
+ query: this.mapper.mapTo(query),
36
+ newValues: this.mapper.mapTo(updates),
37
+ };
38
+ });
39
+ }
32
40
  willFind(query, options) {
33
41
  return __awaiter(this, void 0, void 0, function* () {
34
42
  let { sort } = options !== null && options !== void 0 ? options : {};
@@ -52,6 +52,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
52
52
  updateOne<IncludePrivateFields extends boolean = false, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: UpdateRecord, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
53
53
  update(query: QueryBuilder<QueryRecord>, updates: UpdateRecord): Promise<number>;
54
54
  private findOneAndUpdate;
55
+ private handleWillUpdateOnePlugins;
55
56
  private pluckOperations;
56
57
  scramble(id: string): Promise<SchemaValues<FullSchema, false, false, false, Extract<keyof FullSchema["fields"], string>, SchemaPublicFieldNames<FullSchema>>>;
57
58
  private isScrambled;
@@ -306,22 +306,19 @@ export default class AbstractStore extends AbstractMutexer {
306
306
  });
307
307
  }
308
308
  findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
309
- var _a, _b, _c, _d;
309
+ var _a, _b, _c;
310
310
  return __awaiter(this, void 0, void 0, function* () {
311
- const { ops, updates: initialUpdates } = this.pluckOperations(updates);
311
+ let { ops, updates: initialUpdates } = this.pluckOperations(updates);
312
312
  let q = (_b = (yield ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : query;
313
- let shouldUpdate = true;
314
313
  try {
315
314
  const isScrambled = this.isScrambled(initialUpdates);
316
- for (const plugin of this.plugins) {
317
- const results = yield ((_c = plugin.willUpdateOne) === null || _c === void 0 ? void 0 : _c.call(plugin, q, initialUpdates));
318
- if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
319
- shouldUpdate = false;
320
- }
321
- if (results === null || results === void 0 ? void 0 : results.query) {
322
- q = results.query;
323
- }
315
+ if (!isScrambled) {
316
+ //@ts-ignore
317
+ validateSchemaValues(this.updateSchema, initialUpdates);
324
318
  }
319
+ const { query: qPlugins, shouldUpdate, updates: qUpdates, } = yield this.handleWillUpdateOnePlugins(q, initialUpdates);
320
+ q = qPlugins;
321
+ initialUpdates = qUpdates;
325
322
  let current = yield this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
326
323
  if (!current) {
327
324
  current = yield notFoundHandler();
@@ -332,10 +329,6 @@ export default class AbstractStore extends AbstractMutexer {
332
329
  throw new Error('Make sure your notFoundHandler returns a record or throws');
333
330
  }
334
331
  }
335
- if (!isScrambled) {
336
- //@ts-ignore
337
- validateSchemaValues(this.updateSchema, initialUpdates);
338
- }
339
332
  if (!shouldUpdate) {
340
333
  return current;
341
334
  }
@@ -353,7 +346,7 @@ export default class AbstractStore extends AbstractMutexer {
353
346
  normalizedValues[name] = value;
354
347
  }
355
348
  const results = yield this.db.updateOne(this.collectionName, q, normalizedValues);
356
- yield ((_d = this.didUpdate) === null || _d === void 0 ? void 0 : _d.call(this, current, results));
349
+ yield ((_c = this.didUpdate) === null || _c === void 0 ? void 0 : _c.call(this, current, results));
357
350
  return this.prepareAndNormalizeRecord(results, options);
358
351
  }
359
352
  catch (err) {
@@ -367,6 +360,27 @@ export default class AbstractStore extends AbstractMutexer {
367
360
  }
368
361
  });
369
362
  }
363
+ handleWillUpdateOnePlugins(query, updates) {
364
+ var _a;
365
+ return __awaiter(this, void 0, void 0, function* () {
366
+ let shouldUpdate = true;
367
+ let resolvedQuery = query;
368
+ let resolvedUpdates = updates;
369
+ for (const plugin of this.plugins) {
370
+ const results = yield ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
371
+ if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
372
+ shouldUpdate = false;
373
+ }
374
+ if (results === null || results === void 0 ? void 0 : results.query) {
375
+ resolvedQuery = results.query;
376
+ }
377
+ if (results === null || results === void 0 ? void 0 : results.newValues) {
378
+ resolvedUpdates = results.newValues;
379
+ }
380
+ }
381
+ return { query: resolvedQuery, shouldUpdate, updates: resolvedUpdates };
382
+ });
383
+ }
370
384
  pluckOperations(updates) {
371
385
  const initialUpdates = __rest(updates, []);
372
386
  const ops = saveOperations
@@ -54,6 +54,7 @@ export interface DataStorePluginWillCreateOneResponse {
54
54
  export interface DataStorePluginWillUpdateOneResponse {
55
55
  query?: Record<string, any>;
56
56
  shouldUpdate?: boolean;
57
+ newValues?: Record<string, any>;
57
58
  }
58
59
  export interface DataStorePluginWillDeleteOneResponse {
59
60
  query?: Record<string, any>;
package/build/index.d.ts CHANGED
@@ -20,6 +20,7 @@ export { default as CursorPager } from './cursors/CursorPager';
20
20
  export * from './cursors/CursorPager';
21
21
  export { default as CursorPagerFaker } from './cursors/CursorPagerFaker';
22
22
  export * from './cursors/CursorPager';
23
+ export { default as DatabaseFieldMapperPlugin } from './plugins/DatabaseFieldMapperPlugin';
23
24
  /**
24
25
  * @deprecated databaseAssertUtil -> databaseAssert
25
26
  */
package/build/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.BatchArrayCursor = exports.BatchCursorImpl = exports.storePluginAssert = exports.databaseAssert = exports.databaseAssertUtil = exports.CursorPagerFaker = exports.CursorPager = exports.generateId = exports.DataStoresError = exports.mongoUtil = exports.NeDbDatabase = exports.MongoDatabase = exports.StoreLoader = exports.DatabaseFactory = exports.StoreFactory = exports.AbstractStore = exports.DatabaseFixture = exports.AbstractDatabaseTest = void 0;
20
+ exports.BatchArrayCursor = exports.BatchCursorImpl = exports.storePluginAssert = exports.databaseAssert = exports.databaseAssertUtil = exports.DatabaseFieldMapperPlugin = exports.CursorPagerFaker = exports.CursorPager = exports.generateId = exports.DataStoresError = exports.mongoUtil = exports.NeDbDatabase = exports.MongoDatabase = exports.StoreLoader = exports.DatabaseFactory = exports.StoreFactory = exports.AbstractStore = exports.DatabaseFixture = exports.AbstractDatabaseTest = void 0;
21
21
  var AbstractDatabaseTest_1 = require("./tests/AbstractDatabaseTest");
22
22
  Object.defineProperty(exports, "AbstractDatabaseTest", { enumerable: true, get: function () { return __importDefault(AbstractDatabaseTest_1).default; } });
23
23
  var DatabaseFixture_1 = require("./fixtures/DatabaseFixture");
@@ -53,6 +53,8 @@ __exportStar(require("./cursors/CursorPager"), exports);
53
53
  var CursorPagerFaker_1 = require("./cursors/CursorPagerFaker");
54
54
  Object.defineProperty(exports, "CursorPagerFaker", { enumerable: true, get: function () { return __importDefault(CursorPagerFaker_1).default; } });
55
55
  __exportStar(require("./cursors/CursorPager"), exports);
56
+ var DatabaseFieldMapperPlugin_1 = require("./plugins/DatabaseFieldMapperPlugin");
57
+ Object.defineProperty(exports, "DatabaseFieldMapperPlugin", { enumerable: true, get: function () { return __importDefault(DatabaseFieldMapperPlugin_1).default; } });
56
58
  /**
57
59
  * @deprecated databaseAssertUtil -> databaseAssert
58
60
  */
@@ -1,10 +1,11 @@
1
1
  import { QueryOptions } from '../types/query.types';
2
- import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse } from '../types/stores.types';
2
+ import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse, DataStorePluginWillUpdateOneResponse } from '../types/stores.types';
3
3
  export default class DatabaseFieldMapperPlugin implements DataStorePlugin {
4
4
  private mapper;
5
5
  constructor(map: Record<string, any>);
6
6
  getName(): string;
7
7
  willCreateOne(values: Record<string, any>): Promise<void | DataStorePluginWillCreateOneResponse>;
8
8
  prepareRecord(record: Record<string, any>): Promise<void | DataStorePluginPrepareResponse>;
9
+ willUpdateOne(query: Record<string, any>, updates: Record<string, any>): Promise<void | DataStorePluginWillUpdateOneResponse>;
9
10
  willFind(query: Record<string, any>, options?: QueryOptions): Promise<void | DataStorePluginWillFindResponse>;
10
11
  }
@@ -18,6 +18,12 @@ class DatabaseFieldMapperPlugin {
18
18
  newValues: this.mapper.mapFrom(record),
19
19
  };
20
20
  }
21
+ async willUpdateOne(query, updates) {
22
+ return {
23
+ query: this.mapper.mapTo(query),
24
+ newValues: this.mapper.mapTo(updates),
25
+ };
26
+ }
21
27
  async willFind(query, options) {
22
28
  let { sort } = options !== null && options !== void 0 ? options : {};
23
29
  sort = sort === null || sort === void 0 ? void 0 : sort.map((s) => (Object.assign(Object.assign({}, s), { field: this.mapper.mapFieldNameTo(s.field) })));
@@ -52,6 +52,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
52
52
  updateOne<IncludePrivateFields extends boolean = false, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: UpdateRecord, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
53
53
  update(query: QueryBuilder<QueryRecord>, updates: UpdateRecord): Promise<number>;
54
54
  private findOneAndUpdate;
55
+ private handleWillUpdateOnePlugins;
55
56
  private pluckOperations;
56
57
  scramble(id: string): Promise<SchemaValues<FullSchema, false, false, false, Extract<keyof FullSchema["fields"], string>, SchemaPublicFieldNames<FullSchema>>>;
57
58
  private isScrambled;
@@ -293,21 +293,18 @@ class AbstractStore extends AbstractMutexer_1.default {
293
293
  return this.db.update(this.collectionName, query, updates);
294
294
  }
295
295
  async findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
296
- var _a, _b, _c, _d;
297
- const { ops, updates: initialUpdates } = this.pluckOperations(updates);
296
+ var _a, _b, _c;
297
+ let { ops, updates: initialUpdates } = this.pluckOperations(updates);
298
298
  let q = (_b = (await ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : query;
299
- let shouldUpdate = true;
300
299
  try {
301
300
  const isScrambled = this.isScrambled(initialUpdates);
302
- for (const plugin of this.plugins) {
303
- const results = await ((_c = plugin.willUpdateOne) === null || _c === void 0 ? void 0 : _c.call(plugin, q, initialUpdates));
304
- if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
305
- shouldUpdate = false;
306
- }
307
- if (results === null || results === void 0 ? void 0 : results.query) {
308
- q = results.query;
309
- }
301
+ if (!isScrambled) {
302
+ //@ts-ignore
303
+ (0, schema_1.validateSchemaValues)(this.updateSchema, initialUpdates);
310
304
  }
305
+ const { query: qPlugins, shouldUpdate, updates: qUpdates, } = await this.handleWillUpdateOnePlugins(q, initialUpdates);
306
+ q = qPlugins;
307
+ initialUpdates = qUpdates;
311
308
  let current = await this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
312
309
  if (!current) {
313
310
  current = await notFoundHandler();
@@ -318,10 +315,6 @@ class AbstractStore extends AbstractMutexer_1.default {
318
315
  throw new Error('Make sure your notFoundHandler returns a record or throws');
319
316
  }
320
317
  }
321
- if (!isScrambled) {
322
- //@ts-ignore
323
- (0, schema_1.validateSchemaValues)(this.updateSchema, initialUpdates);
324
- }
325
318
  if (!shouldUpdate) {
326
319
  return current;
327
320
  }
@@ -339,7 +332,7 @@ class AbstractStore extends AbstractMutexer_1.default {
339
332
  normalizedValues[name] = value;
340
333
  }
341
334
  const results = await this.db.updateOne(this.collectionName, q, normalizedValues);
342
- await ((_d = this.didUpdate) === null || _d === void 0 ? void 0 : _d.call(this, current, results));
335
+ await ((_c = this.didUpdate) === null || _c === void 0 ? void 0 : _c.call(this, current, results));
343
336
  return this.prepareAndNormalizeRecord(results, options);
344
337
  }
345
338
  catch (err) {
@@ -352,6 +345,25 @@ class AbstractStore extends AbstractMutexer_1.default {
352
345
  throw coded[0];
353
346
  }
354
347
  }
348
+ async handleWillUpdateOnePlugins(query, updates) {
349
+ var _a;
350
+ let shouldUpdate = true;
351
+ let resolvedQuery = query;
352
+ let resolvedUpdates = updates;
353
+ for (const plugin of this.plugins) {
354
+ const results = await ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
355
+ if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
356
+ shouldUpdate = false;
357
+ }
358
+ if (results === null || results === void 0 ? void 0 : results.query) {
359
+ resolvedQuery = results.query;
360
+ }
361
+ if (results === null || results === void 0 ? void 0 : results.newValues) {
362
+ resolvedUpdates = results.newValues;
363
+ }
364
+ }
365
+ return { query: resolvedQuery, shouldUpdate, updates: resolvedUpdates };
366
+ }
355
367
  pluckOperations(updates) {
356
368
  const initialUpdates = __rest(updates, []);
357
369
  const ops = stores_types_1.saveOperations
@@ -54,6 +54,7 @@ export interface DataStorePluginWillCreateOneResponse {
54
54
  export interface DataStorePluginWillUpdateOneResponse {
55
55
  query?: Record<string, any>;
56
56
  shouldUpdate?: boolean;
57
+ newValues?: Record<string, any>;
57
58
  }
58
59
  export interface DataStorePluginWillDeleteOneResponse {
59
60
  query?: Record<string, any>;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "26.2.0",
6
+ "version": "26.2.2",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",