@sprucelabs/data-stores 25.10.0 → 25.11.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.
@@ -33,8 +33,10 @@ 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
37
  private get primaryFieldName();
37
38
  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
+ private handleDidFindForPlugins;
38
40
  count(query?: QueryBuilder<QueryRecord>): Promise<number>;
39
41
  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>>>[]>;
40
42
  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>>>;
@@ -101,7 +101,7 @@ export default class AbstractStore extends AbstractMutexer {
101
101
  });
102
102
  }
103
103
  createOne(values, options) {
104
- var _a, _b, _c;
104
+ var _a, _b;
105
105
  return __awaiter(this, void 0, void 0, function* () {
106
106
  try {
107
107
  //@ts-ignore
@@ -115,13 +115,9 @@ export default class AbstractStore extends AbstractMutexer {
115
115
  const toSave = normalizeSchemaValues(this.databaseSchema,
116
116
  //@ts-ignore
117
117
  databaseRecord, { shouldCreateEntityInstances: false });
118
- let mixinValuesOnReturn = {};
119
- for (const plugin of this.plugins) {
120
- const r = yield ((_b = plugin.willCreateOne) === null || _b === void 0 ? void 0 : _b.call(plugin, toSave));
121
- mixinValuesOnReturn = Object.assign(Object.assign({}, mixinValuesOnReturn), r === null || r === void 0 ? void 0 : r.valuesToMixinBeforeReturning);
122
- }
118
+ const mixinValuesOnReturn = yield this.handleWillCreateForPlugins(toSave);
123
119
  const record = yield this.db.createOne(this.collectionName, toSave);
124
- yield ((_c = this.didCreate) === null || _c === void 0 ? void 0 : _c.call(this, record));
120
+ yield ((_b = this.didCreate) === null || _b === void 0 ? void 0 : _b.call(this, record));
125
121
  const normalized = yield this.prepareAndNormalizeRecord(record, options);
126
122
  return Object.assign(Object.assign({}, normalized), mixinValuesOnReturn);
127
123
  }
@@ -136,18 +132,43 @@ export default class AbstractStore extends AbstractMutexer {
136
132
  }
137
133
  });
138
134
  }
135
+ handleWillCreateForPlugins(toSave) {
136
+ var _a;
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ let mixinValuesOnReturn = {};
139
+ for (const plugin of this.plugins) {
140
+ const r = yield ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, toSave));
141
+ mixinValuesOnReturn = Object.assign(Object.assign({}, mixinValuesOnReturn), r === null || r === void 0 ? void 0 : r.valuesToMixinBeforeReturning);
142
+ }
143
+ return mixinValuesOnReturn;
144
+ });
145
+ }
139
146
  get primaryFieldName() {
140
147
  return this.primaryFieldNames[0];
141
148
  }
142
149
  findOne(query, options = {}) {
143
150
  return __awaiter(this, void 0, void 0, function* () {
144
151
  const results = yield this.find(query, { limit: 1 }, options);
145
- if (results.length > 0) {
146
- return results[0];
152
+ let match = results[0];
153
+ if (match) {
154
+ match = (yield this.handleDidFindForPlugins(query, match));
155
+ return match;
147
156
  }
148
157
  return null;
149
158
  });
150
159
  }
160
+ handleDidFindForPlugins(query, match) {
161
+ var _a, _b;
162
+ return __awaiter(this, void 0, void 0, function* () {
163
+ for (const plugin of this.plugins) {
164
+ const { valuesToMixinBeforeReturning: values } = (_b = (yield ((_a = plugin.didFindOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, match)))) !== null && _b !== void 0 ? _b : {};
165
+ if (values) {
166
+ match = Object.assign(Object.assign({}, match), values);
167
+ }
168
+ }
169
+ return match;
170
+ });
171
+ }
151
172
  count(query) {
152
173
  return __awaiter(this, void 0, void 0, function* () {
153
174
  const count = yield this.db.count(this.collectionName, query);
@@ -43,6 +43,7 @@ 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
45
  willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
46
+ didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
46
47
  getName(): string;
47
48
  }
48
49
  export interface DataStorePluginHookResponse {
@@ -54,3 +55,6 @@ export interface DataStorePluginWillUpdateOneResponse {
54
55
  export interface DataStorePluginWillDeleteOneResponse {
55
56
  query?: Record<string, any>;
56
57
  }
58
+ export interface DataStorePluginDidFindOneResponse {
59
+ valuesToMixinBeforeReturning?: Record<string, any>;
60
+ }
@@ -33,8 +33,10 @@ 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
37
  private get primaryFieldName();
37
38
  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
+ private handleDidFindForPlugins;
38
40
  count(query?: QueryBuilder<QueryRecord>): Promise<number>;
39
41
  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>>>[]>;
40
42
  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>>>;
@@ -116,7 +116,7 @@ class AbstractStore extends AbstractMutexer_1.default {
116
116
  }
117
117
  }
118
118
  async createOne(values, options) {
119
- var _a, _b, _c;
119
+ var _a, _b;
120
120
  try {
121
121
  //@ts-ignore
122
122
  (0, schema_1.validateSchemaValues)(this.createSchema, values);
@@ -129,13 +129,9 @@ class AbstractStore extends AbstractMutexer_1.default {
129
129
  const toSave = (0, schema_1.normalizeSchemaValues)(this.databaseSchema,
130
130
  //@ts-ignore
131
131
  databaseRecord, { shouldCreateEntityInstances: false });
132
- let mixinValuesOnReturn = {};
133
- for (const plugin of this.plugins) {
134
- const r = await ((_b = plugin.willCreateOne) === null || _b === void 0 ? void 0 : _b.call(plugin, toSave));
135
- mixinValuesOnReturn = Object.assign(Object.assign({}, mixinValuesOnReturn), r === null || r === void 0 ? void 0 : r.valuesToMixinBeforeReturning);
136
- }
132
+ const mixinValuesOnReturn = await this.handleWillCreateForPlugins(toSave);
137
133
  const record = await this.db.createOne(this.collectionName, toSave);
138
- await ((_c = this.didCreate) === null || _c === void 0 ? void 0 : _c.call(this, record));
134
+ await ((_b = this.didCreate) === null || _b === void 0 ? void 0 : _b.call(this, record));
139
135
  const normalized = await this.prepareAndNormalizeRecord(record, options);
140
136
  return Object.assign(Object.assign({}, normalized), mixinValuesOnReturn);
141
137
  }
@@ -149,16 +145,37 @@ class AbstractStore extends AbstractMutexer_1.default {
149
145
  throw coded[0];
150
146
  }
151
147
  }
148
+ async handleWillCreateForPlugins(toSave) {
149
+ var _a;
150
+ let mixinValuesOnReturn = {};
151
+ for (const plugin of this.plugins) {
152
+ const r = await ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, toSave));
153
+ mixinValuesOnReturn = Object.assign(Object.assign({}, mixinValuesOnReturn), r === null || r === void 0 ? void 0 : r.valuesToMixinBeforeReturning);
154
+ }
155
+ return mixinValuesOnReturn;
156
+ }
152
157
  get primaryFieldName() {
153
158
  return this.primaryFieldNames[0];
154
159
  }
155
160
  async findOne(query, options = {}) {
156
161
  const results = await this.find(query, { limit: 1 }, options);
157
- if (results.length > 0) {
158
- return results[0];
162
+ let match = results[0];
163
+ if (match) {
164
+ match = (await this.handleDidFindForPlugins(query, match));
165
+ return match;
159
166
  }
160
167
  return null;
161
168
  }
169
+ async handleDidFindForPlugins(query, match) {
170
+ var _a, _b;
171
+ for (const plugin of this.plugins) {
172
+ const { valuesToMixinBeforeReturning: values } = (_b = (await ((_a = plugin.didFindOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, match)))) !== null && _b !== void 0 ? _b : {};
173
+ if (values) {
174
+ match = Object.assign(Object.assign({}, match), values);
175
+ }
176
+ }
177
+ return match;
178
+ }
162
179
  async count(query) {
163
180
  const count = await this.db.count(this.collectionName, query);
164
181
  return count;
@@ -43,6 +43,7 @@ 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
45
  willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
46
+ didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
46
47
  getName(): string;
47
48
  }
48
49
  export interface DataStorePluginHookResponse {
@@ -54,3 +55,6 @@ export interface DataStorePluginWillUpdateOneResponse {
54
55
  export interface DataStorePluginWillDeleteOneResponse {
55
56
  query?: Record<string, any>;
56
57
  }
58
+ export interface DataStorePluginDidFindOneResponse {
59
+ valuesToMixinBeforeReturning?: Record<string, any>;
60
+ }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "25.10.0",
6
+ "version": "25.11.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.42",
71
+ "@sprucelabs/error": "^5.1.43",
72
72
  "@sprucelabs/globby": "^1.0.3",
73
- "@sprucelabs/schema": "^29.0.81",
74
- "@sprucelabs/spruce-skill-utils": "^30.1.16",
73
+ "@sprucelabs/schema": "^29.0.82",
74
+ "@sprucelabs/spruce-skill-utils": "^30.1.17",
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.90",
82
- "@sprucelabs/jest-json-reporter": "^7.0.117",
81
+ "@sprucelabs/esm-postbuild": "^5.0.91",
82
+ "@sprucelabs/jest-json-reporter": "^7.0.118",
83
83
  "@sprucelabs/jest-sheets-reporter": "^3.0.26",
84
- "@sprucelabs/resolve-path-aliases": "^1.1.258",
84
+ "@sprucelabs/resolve-path-aliases": "^1.1.259",
85
85
  "@sprucelabs/semantic-release": "^4.0.8",
86
- "@sprucelabs/test": "^8.0.26",
87
- "@sprucelabs/test-utils": "^4.0.69",
86
+ "@sprucelabs/test": "^8.0.27",
87
+ "@sprucelabs/test-utils": "^4.0.70",
88
88
  "@types/lodash": "^4.14.201",
89
89
  "@types/nedb": "^1.8.16",
90
90
  "@types/node": "^20.9.0",