@sprucelabs/data-stores 25.11.2 → 25.13.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.
@@ -33,7 +33,8 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
33
33
  private mapCasing;
34
34
  create<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(values: CreateRecord[], options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>[]>;
35
35
  createOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(values: CreateRecord, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>> & {}>;
36
- private handleWillCreateForPlugins;
36
+ private handleWillCreatePlugins;
37
+ private handleDidCreateForPlugins;
37
38
  private get primaryFieldName();
38
39
  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>;
39
40
  private handleDidFindForPlugins;
@@ -104,6 +104,7 @@ export default class AbstractStore extends AbstractMutexer {
104
104
  var _a, _b;
105
105
  return __awaiter(this, void 0, void 0, function* () {
106
106
  try {
107
+ let valuesToMixinBeforeCreate = yield this.handleWillCreatePlugins(values);
107
108
  //@ts-ignore
108
109
  validateSchemaValues(this.createSchema, values);
109
110
  const cleanedValues = this.willCreate
@@ -115,10 +116,10 @@ export default class AbstractStore extends AbstractMutexer {
115
116
  const toSave = normalizeSchemaValues(this.databaseSchema,
116
117
  //@ts-ignore
117
118
  databaseRecord, { shouldCreateEntityInstances: false });
118
- const mixinValuesOnReturn = yield this.handleWillCreateForPlugins(toSave);
119
- const record = yield this.db.createOne(this.collectionName, toSave);
119
+ const record = yield this.db.createOne(this.collectionName, Object.assign(Object.assign({}, toSave), valuesToMixinBeforeCreate));
120
120
  yield ((_b = this.didCreate) === null || _b === void 0 ? void 0 : _b.call(this, record));
121
121
  const normalized = yield this.prepareAndNormalizeRecord(record, options);
122
+ const mixinValuesOnReturn = yield this.handleDidCreateForPlugins(normalized);
122
123
  return Object.assign(Object.assign({}, normalized), mixinValuesOnReturn);
123
124
  }
124
125
  catch (err) {
@@ -132,12 +133,26 @@ export default class AbstractStore extends AbstractMutexer {
132
133
  }
133
134
  });
134
135
  }
135
- handleWillCreateForPlugins(toSave) {
136
+ handleWillCreatePlugins(values) {
137
+ var _a;
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ let valuesToMixinBeforeCreate = {};
140
+ for (const plugin of this.plugins) {
141
+ const r = yield ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, values));
142
+ const { valuesToMixinBeforeCreate: v } = r !== null && r !== void 0 ? r : {};
143
+ if (v) {
144
+ valuesToMixinBeforeCreate = Object.assign(Object.assign({}, valuesToMixinBeforeCreate), v);
145
+ }
146
+ }
147
+ return valuesToMixinBeforeCreate;
148
+ });
149
+ }
150
+ handleDidCreateForPlugins(record) {
136
151
  var _a;
137
152
  return __awaiter(this, void 0, void 0, function* () {
138
153
  let mixinValuesOnReturn = {};
139
154
  for (const plugin of this.plugins) {
140
- const r = yield ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, toSave));
155
+ const r = yield ((_a = plugin.didCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, record));
141
156
  mixinValuesOnReturn = Object.assign(Object.assign({}, mixinValuesOnReturn), r === null || r === void 0 ? void 0 : r.valuesToMixinBeforeReturning);
142
157
  }
143
158
  return mixinValuesOnReturn;
@@ -244,10 +259,14 @@ export default class AbstractStore extends AbstractMutexer {
244
259
  return __awaiter(this, void 0, void 0, function* () {
245
260
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
246
261
  let q = query;
262
+ let shouldUpdate = true;
247
263
  try {
248
264
  const isScrambled = this.isScrambled(initialUpdates);
249
265
  for (const plugin of this.plugins) {
250
266
  const results = yield ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q, initialUpdates));
267
+ if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
268
+ shouldUpdate = false;
269
+ }
251
270
  if (results === null || results === void 0 ? void 0 : results.query) {
252
271
  q = results.query;
253
272
  }
@@ -266,6 +285,9 @@ export default class AbstractStore extends AbstractMutexer {
266
285
  //@ts-ignore
267
286
  validateSchemaValues(this.updateSchema, initialUpdates);
268
287
  }
288
+ if (!shouldUpdate) {
289
+ return current;
290
+ }
269
291
  const cleanedUpdates = !isScrambled && this.willUpdate
270
292
  ? yield this.willUpdate(initialUpdates, current)
271
293
  : initialUpdates;
@@ -40,17 +40,22 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
40
40
  badDatabaseName: string;
41
41
  }>;
42
42
  export interface DataStorePlugin {
43
- willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginHookResponse>;
43
+ willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
44
+ didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
44
45
  willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
45
46
  willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
46
47
  didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
47
48
  getName(): string;
48
49
  }
49
- export interface DataStorePluginHookResponse {
50
+ export interface DataStorePluginDidCreateOneResponse {
50
51
  valuesToMixinBeforeReturning?: Record<string, any>;
51
52
  }
53
+ export interface DataStorePluginWillCreateOneResponse {
54
+ valuesToMixinBeforeCreate?: Record<string, any>;
55
+ }
52
56
  export interface DataStorePluginWillUpdateOneResponse {
53
57
  query?: Record<string, any>;
58
+ shouldUpdate?: boolean;
54
59
  }
55
60
  export interface DataStorePluginWillDeleteOneResponse {
56
61
  query?: Record<string, any>;
@@ -33,7 +33,8 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
33
33
  private mapCasing;
34
34
  create<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(values: CreateRecord[], options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>[]>;
35
35
  createOne<CreateEntityInstances extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(values: CreateRecord, options?: PrepareOptions<CreateEntityInstances, FullSchema, F>): Promise<SchemaValues<FullSchema, false, CreateEntityInstances, false, F, SchemaPublicFieldNames<FullSchema>> & {}>;
36
- private handleWillCreateForPlugins;
36
+ private handleWillCreatePlugins;
37
+ private handleDidCreateForPlugins;
37
38
  private get primaryFieldName();
38
39
  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>;
39
40
  private handleDidFindForPlugins;
@@ -118,6 +118,7 @@ class AbstractStore extends AbstractMutexer_1.default {
118
118
  async createOne(values, options) {
119
119
  var _a, _b;
120
120
  try {
121
+ let valuesToMixinBeforeCreate = await this.handleWillCreatePlugins(values);
121
122
  //@ts-ignore
122
123
  (0, schema_1.validateSchemaValues)(this.createSchema, values);
123
124
  const cleanedValues = this.willCreate
@@ -129,10 +130,10 @@ class AbstractStore extends AbstractMutexer_1.default {
129
130
  const toSave = (0, schema_1.normalizeSchemaValues)(this.databaseSchema,
130
131
  //@ts-ignore
131
132
  databaseRecord, { shouldCreateEntityInstances: false });
132
- const mixinValuesOnReturn = await this.handleWillCreateForPlugins(toSave);
133
- const record = await this.db.createOne(this.collectionName, toSave);
133
+ const record = await this.db.createOne(this.collectionName, Object.assign(Object.assign({}, toSave), valuesToMixinBeforeCreate));
134
134
  await ((_b = this.didCreate) === null || _b === void 0 ? void 0 : _b.call(this, record));
135
135
  const normalized = await this.prepareAndNormalizeRecord(record, options);
136
+ const mixinValuesOnReturn = await this.handleDidCreateForPlugins(normalized);
136
137
  return Object.assign(Object.assign({}, normalized), mixinValuesOnReturn);
137
138
  }
138
139
  catch (err) {
@@ -145,11 +146,23 @@ class AbstractStore extends AbstractMutexer_1.default {
145
146
  throw coded[0];
146
147
  }
147
148
  }
148
- async handleWillCreateForPlugins(toSave) {
149
+ async handleWillCreatePlugins(values) {
150
+ var _a;
151
+ let valuesToMixinBeforeCreate = {};
152
+ for (const plugin of this.plugins) {
153
+ const r = await ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, values));
154
+ const { valuesToMixinBeforeCreate: v } = r !== null && r !== void 0 ? r : {};
155
+ if (v) {
156
+ valuesToMixinBeforeCreate = Object.assign(Object.assign({}, valuesToMixinBeforeCreate), v);
157
+ }
158
+ }
159
+ return valuesToMixinBeforeCreate;
160
+ }
161
+ async handleDidCreateForPlugins(record) {
149
162
  var _a;
150
163
  let mixinValuesOnReturn = {};
151
164
  for (const plugin of this.plugins) {
152
- const r = await ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, toSave));
165
+ const r = await ((_a = plugin.didCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, record));
153
166
  mixinValuesOnReturn = Object.assign(Object.assign({}, mixinValuesOnReturn), r === null || r === void 0 ? void 0 : r.valuesToMixinBeforeReturning);
154
167
  }
155
168
  return mixinValuesOnReturn;
@@ -238,10 +251,14 @@ class AbstractStore extends AbstractMutexer_1.default {
238
251
  var _a, _b;
239
252
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
240
253
  let q = query;
254
+ let shouldUpdate = true;
241
255
  try {
242
256
  const isScrambled = this.isScrambled(initialUpdates);
243
257
  for (const plugin of this.plugins) {
244
258
  const results = await ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q, initialUpdates));
259
+ if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
260
+ shouldUpdate = false;
261
+ }
245
262
  if (results === null || results === void 0 ? void 0 : results.query) {
246
263
  q = results.query;
247
264
  }
@@ -260,6 +277,9 @@ class AbstractStore extends AbstractMutexer_1.default {
260
277
  //@ts-ignore
261
278
  (0, schema_1.validateSchemaValues)(this.updateSchema, initialUpdates);
262
279
  }
280
+ if (!shouldUpdate) {
281
+ return current;
282
+ }
263
283
  const cleanedUpdates = !isScrambled && this.willUpdate
264
284
  ? await this.willUpdate(initialUpdates, current)
265
285
  : initialUpdates;
@@ -40,17 +40,22 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
40
40
  badDatabaseName: string;
41
41
  }>;
42
42
  export interface DataStorePlugin {
43
- willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginHookResponse>;
43
+ willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
44
+ didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
44
45
  willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
45
46
  willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
46
47
  didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
47
48
  getName(): string;
48
49
  }
49
- export interface DataStorePluginHookResponse {
50
+ export interface DataStorePluginDidCreateOneResponse {
50
51
  valuesToMixinBeforeReturning?: Record<string, any>;
51
52
  }
53
+ export interface DataStorePluginWillCreateOneResponse {
54
+ valuesToMixinBeforeCreate?: Record<string, any>;
55
+ }
52
56
  export interface DataStorePluginWillUpdateOneResponse {
53
57
  query?: Record<string, any>;
58
+ shouldUpdate?: boolean;
54
59
  }
55
60
  export interface DataStorePluginWillDeleteOneResponse {
56
61
  query?: Record<string, any>;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "25.11.2",
6
+ "version": "25.13.0",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",