@sprucelabs/data-stores 25.12.0 → 25.13.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.
@@ -1,9 +1,9 @@
1
1
  import { Schema, SchemaFieldNames, SchemaPartialValues, SchemaPublicFieldNames, SchemaValues } from '@sprucelabs/schema';
2
2
  import { FindBatchOptions } from '../cursors/BatchCursor';
3
3
  import AbstractMutexer from '../mutexers/AbstractMutexer';
4
- import { DataStorePlugin, Database } from '../types/database.types';
4
+ import { Database } from '../types/database.types';
5
5
  import { QueryBuilder, QueryOptions } from '../types/query.types';
6
- import { PrepareOptions, PrepareResults, SaveOperations, DataStore } from '../types/stores.types';
6
+ import { PrepareOptions, PrepareResults, SaveOperations, DataStore, DataStorePlugin } from '../types/stores.types';
7
7
  export default abstract class AbstractStore<FullSchema extends Schema, CreateSchema extends Schema = FullSchema, UpdateSchema extends Schema = CreateSchema, DatabaseSchema extends Schema = FullSchema, DatabaseRecord = SchemaValues<DatabaseSchema>, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
8
8
  abstract readonly name: string;
9
9
  protected abstract collectionName: string;
@@ -259,10 +259,14 @@ export default class AbstractStore extends AbstractMutexer {
259
259
  return __awaiter(this, void 0, void 0, function* () {
260
260
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
261
261
  let q = query;
262
+ let shouldUpdate = true;
262
263
  try {
263
264
  const isScrambled = this.isScrambled(initialUpdates);
264
265
  for (const plugin of this.plugins) {
265
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
+ }
266
270
  if (results === null || results === void 0 ? void 0 : results.query) {
267
271
  q = results.query;
268
272
  }
@@ -281,6 +285,9 @@ export default class AbstractStore extends AbstractMutexer {
281
285
  //@ts-ignore
282
286
  validateSchemaValues(this.updateSchema, initialUpdates);
283
287
  }
288
+ if (!shouldUpdate) {
289
+ return current;
290
+ }
284
291
  const cleanedUpdates = !isScrambled && this.willUpdate
285
292
  ? yield this.willUpdate(initialUpdates, current)
286
293
  : initialUpdates;
@@ -39,26 +39,3 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
39
39
  connectionStringWithRandomBadDatabaseName: string;
40
40
  badDatabaseName: string;
41
41
  }>;
42
- export interface DataStorePlugin {
43
- willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
44
- didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
45
- willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
46
- willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
47
- didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
48
- getName(): string;
49
- }
50
- export interface DataStorePluginDidCreateOneResponse {
51
- valuesToMixinBeforeReturning?: Record<string, any>;
52
- }
53
- export interface DataStorePluginWillCreateOneResponse {
54
- valuesToMixinBeforeCreate?: Record<string, any>;
55
- }
56
- export interface DataStorePluginWillUpdateOneResponse {
57
- query?: Record<string, any>;
58
- }
59
- export interface DataStorePluginWillDeleteOneResponse {
60
- query?: Record<string, any>;
61
- }
62
- export interface DataStorePluginDidFindOneResponse {
63
- valuesToMixinBeforeReturning?: Record<string, any>;
64
- }
@@ -11,6 +11,7 @@ export interface UniversalStoreOptions {
11
11
  export interface DataStore {
12
12
  initialize?(): Promise<void>;
13
13
  getCollectionName?(): string;
14
+ getDb?(): Database;
14
15
  }
15
16
  /**
16
17
  * @deprecated SimplifiedStoreFactory -> SimpleStoreFactory
@@ -29,3 +30,27 @@ export interface PrepareOptions<IncludePrivateFields extends boolean, S extends
29
30
  export type PrepareResults<S extends Schema, IncludePrivateFields extends boolean> = IncludePrivateFields extends true ? SchemaPublicValues<S> : SchemaValues<S>;
30
31
  export type StoreName = keyof StoreMap;
31
32
  export type StoreOptions<Name extends StoreName> = Name extends keyof StoreOptionsMap ? StoreOptionsMap[Name] : Record<string, never>;
33
+ export interface DataStorePlugin {
34
+ willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
35
+ didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
36
+ willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
37
+ willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
38
+ didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
39
+ getName(): string;
40
+ }
41
+ export interface DataStorePluginDidCreateOneResponse {
42
+ valuesToMixinBeforeReturning?: Record<string, any>;
43
+ }
44
+ export interface DataStorePluginWillCreateOneResponse {
45
+ valuesToMixinBeforeCreate?: Record<string, any>;
46
+ }
47
+ export interface DataStorePluginWillUpdateOneResponse {
48
+ query?: Record<string, any>;
49
+ shouldUpdate?: boolean;
50
+ }
51
+ export interface DataStorePluginWillDeleteOneResponse {
52
+ query?: Record<string, any>;
53
+ }
54
+ export interface DataStorePluginDidFindOneResponse {
55
+ valuesToMixinBeforeReturning?: Record<string, any>;
56
+ }
@@ -1,9 +1,9 @@
1
1
  import { Schema, SchemaFieldNames, SchemaPartialValues, SchemaPublicFieldNames, SchemaValues } from '@sprucelabs/schema';
2
2
  import { FindBatchOptions } from '../cursors/BatchCursor';
3
3
  import AbstractMutexer from '../mutexers/AbstractMutexer';
4
- import { DataStorePlugin, Database } from '../types/database.types';
4
+ import { Database } from '../types/database.types';
5
5
  import { QueryBuilder, QueryOptions } from '../types/query.types';
6
- import { PrepareOptions, PrepareResults, SaveOperations, DataStore } from '../types/stores.types';
6
+ import { PrepareOptions, PrepareResults, SaveOperations, DataStore, DataStorePlugin } from '../types/stores.types';
7
7
  export default abstract class AbstractStore<FullSchema extends Schema, CreateSchema extends Schema = FullSchema, UpdateSchema extends Schema = CreateSchema, DatabaseSchema extends Schema = FullSchema, DatabaseRecord = SchemaValues<DatabaseSchema>, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
8
8
  abstract readonly name: string;
9
9
  protected abstract collectionName: string;
@@ -251,10 +251,14 @@ class AbstractStore extends AbstractMutexer_1.default {
251
251
  var _a, _b;
252
252
  const { ops, updates: initialUpdates } = this.pluckOperations(updates);
253
253
  let q = query;
254
+ let shouldUpdate = true;
254
255
  try {
255
256
  const isScrambled = this.isScrambled(initialUpdates);
256
257
  for (const plugin of this.plugins) {
257
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
+ }
258
262
  if (results === null || results === void 0 ? void 0 : results.query) {
259
263
  q = results.query;
260
264
  }
@@ -273,6 +277,9 @@ class AbstractStore extends AbstractMutexer_1.default {
273
277
  //@ts-ignore
274
278
  (0, schema_1.validateSchemaValues)(this.updateSchema, initialUpdates);
275
279
  }
280
+ if (!shouldUpdate) {
281
+ return current;
282
+ }
276
283
  const cleanedUpdates = !isScrambled && this.willUpdate
277
284
  ? await this.willUpdate(initialUpdates, current)
278
285
  : initialUpdates;
@@ -39,26 +39,3 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
39
39
  connectionStringWithRandomBadDatabaseName: string;
40
40
  badDatabaseName: string;
41
41
  }>;
42
- export interface DataStorePlugin {
43
- willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
44
- didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
45
- willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
46
- willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
47
- didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
48
- getName(): string;
49
- }
50
- export interface DataStorePluginDidCreateOneResponse {
51
- valuesToMixinBeforeReturning?: Record<string, any>;
52
- }
53
- export interface DataStorePluginWillCreateOneResponse {
54
- valuesToMixinBeforeCreate?: Record<string, any>;
55
- }
56
- export interface DataStorePluginWillUpdateOneResponse {
57
- query?: Record<string, any>;
58
- }
59
- export interface DataStorePluginWillDeleteOneResponse {
60
- query?: Record<string, any>;
61
- }
62
- export interface DataStorePluginDidFindOneResponse {
63
- valuesToMixinBeforeReturning?: Record<string, any>;
64
- }
@@ -11,6 +11,7 @@ export interface UniversalStoreOptions {
11
11
  export interface DataStore {
12
12
  initialize?(): Promise<void>;
13
13
  getCollectionName?(): string;
14
+ getDb?(): Database;
14
15
  }
15
16
  /**
16
17
  * @deprecated SimplifiedStoreFactory -> SimpleStoreFactory
@@ -29,3 +30,27 @@ export interface PrepareOptions<IncludePrivateFields extends boolean, S extends
29
30
  export type PrepareResults<S extends Schema, IncludePrivateFields extends boolean> = IncludePrivateFields extends true ? SchemaPublicValues<S> : SchemaValues<S>;
30
31
  export type StoreName = keyof StoreMap;
31
32
  export type StoreOptions<Name extends StoreName> = Name extends keyof StoreOptionsMap ? StoreOptionsMap[Name] : Record<string, never>;
33
+ export interface DataStorePlugin {
34
+ willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
35
+ didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
36
+ willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
37
+ willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
38
+ didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
39
+ getName(): string;
40
+ }
41
+ export interface DataStorePluginDidCreateOneResponse {
42
+ valuesToMixinBeforeReturning?: Record<string, any>;
43
+ }
44
+ export interface DataStorePluginWillCreateOneResponse {
45
+ valuesToMixinBeforeCreate?: Record<string, any>;
46
+ }
47
+ export interface DataStorePluginWillUpdateOneResponse {
48
+ query?: Record<string, any>;
49
+ shouldUpdate?: boolean;
50
+ }
51
+ export interface DataStorePluginWillDeleteOneResponse {
52
+ query?: Record<string, any>;
53
+ }
54
+ export interface DataStorePluginDidFindOneResponse {
55
+ valuesToMixinBeforeReturning?: Record<string, any>;
56
+ }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "25.12.0",
6
+ "version": "25.13.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.43",
71
+ "@sprucelabs/error": "^5.1.44",
72
72
  "@sprucelabs/globby": "^1.0.3",
73
73
  "@sprucelabs/schema": "^29.0.83",
74
- "@sprucelabs/spruce-skill-utils": "^30.1.18",
74
+ "@sprucelabs/spruce-skill-utils": "^30.1.19",
75
75
  "just-clone": "^6.2.0",
76
76
  "lodash": "^4.17.21",
77
77
  "mongodb": "^6.2.0",
78
78
  "nedb": "^1.8.0"
79
79
  },
80
80
  "devDependencies": {
81
- "@sprucelabs/esm-postbuild": "^5.0.91",
82
- "@sprucelabs/jest-json-reporter": "^7.0.118",
81
+ "@sprucelabs/esm-postbuild": "^5.0.92",
82
+ "@sprucelabs/jest-json-reporter": "^7.0.119",
83
83
  "@sprucelabs/jest-sheets-reporter": "^3.0.26",
84
- "@sprucelabs/resolve-path-aliases": "^1.1.259",
84
+ "@sprucelabs/resolve-path-aliases": "^1.1.260",
85
85
  "@sprucelabs/semantic-release": "^4.0.8",
86
86
  "@sprucelabs/test": "^8.0.27",
87
- "@sprucelabs/test-utils": "^4.0.70",
87
+ "@sprucelabs/test-utils": "^4.0.71",
88
88
  "@types/lodash": "^4.14.201",
89
89
  "@types/nedb": "^1.8.16",
90
90
  "@types/node": "^20.9.0",
@@ -95,7 +95,7 @@
95
95
  "eslint-config-spruce": "^10.13.6",
96
96
  "jest": "^29.7.0",
97
97
  "jest-circus": "^29.7.0",
98
- "prettier": "^3.0.3",
98
+ "prettier": "^3.1.0",
99
99
  "ts-node": "^10.9.1",
100
100
  "tsc-watch": "^6.0.4",
101
101
  "tsconfig-paths": "^4.2.0",