@sprucelabs/data-stores 26.2.1 → 26.2.3
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.
- package/build/databases/NeDbDatabase.d.ts +3 -3
- package/build/databases/NeDbDatabase.js +7 -6
- package/build/esm/databases/NeDbDatabase.d.ts +3 -3
- package/build/esm/databases/NeDbDatabase.js +7 -6
- package/build/esm/plugins/DatabaseFieldMapperPlugin.d.ts +2 -1
- package/build/esm/plugins/DatabaseFieldMapperPlugin.js +8 -0
- package/build/esm/stores/AbstractStore.d.ts +1 -0
- package/build/esm/stores/AbstractStore.js +40 -21
- package/build/esm/types/database.types.d.ts +6 -4
- package/build/esm/types/stores.types.d.ts +1 -0
- package/build/plugins/DatabaseFieldMapperPlugin.d.ts +2 -1
- package/build/plugins/DatabaseFieldMapperPlugin.js +6 -0
- package/build/stores/AbstractStore.d.ts +1 -0
- package/build/stores/AbstractStore.js +38 -21
- package/build/types/database.types.d.ts +6 -4
- package/build/types/stores.types.d.ts +1 -0
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
2
|
-
import { CreateOptions, Database } from '../types/database.types';
|
|
2
|
+
import { CreateOptions, Database, DatabaseInternalOptions } from '../types/database.types';
|
|
3
3
|
import { QueryOptions } from '../types/query.types';
|
|
4
4
|
export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
5
5
|
private collections;
|
|
@@ -22,8 +22,8 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
22
22
|
private loadCollection;
|
|
23
23
|
dropCollection(name: string): Promise<void>;
|
|
24
24
|
dropDatabase(): Promise<void>;
|
|
25
|
-
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any> | null>;
|
|
26
|
-
find(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any>[]>;
|
|
25
|
+
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any> | null>;
|
|
26
|
+
find(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>[]>;
|
|
27
27
|
update(collection: string, query: Record<string, any>, updates: Record<string, any>, neDbOptions?: Record<string, any>): Promise<number>;
|
|
28
28
|
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any>, neDbOptions?: Record<string, any>, action?: string): Promise<Record<string, any>>;
|
|
29
29
|
upsertOne(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
@@ -177,11 +177,12 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
177
177
|
this.collections = {};
|
|
178
178
|
return Promise.resolve();
|
|
179
179
|
}
|
|
180
|
-
async findOne(collection, query, options) {
|
|
181
|
-
const results = await this.find(collection,
|
|
182
|
-
|
|
180
|
+
async findOne(collection, query, options, dbOptions) {
|
|
181
|
+
const results = await this.find(collection, query !== null && query !== void 0 ? query : {}, Object.assign({ limit: 1 }, (options || {})), dbOptions);
|
|
182
|
+
const match = results[0];
|
|
183
|
+
return match;
|
|
183
184
|
}
|
|
184
|
-
async find(collection, query, options) {
|
|
185
|
+
async find(collection, query, options, dbOptions) {
|
|
185
186
|
await this.randomDelay();
|
|
186
187
|
if ((options === null || options === void 0 ? void 0 : options.limit) === 0) {
|
|
187
188
|
return [];
|
|
@@ -205,7 +206,7 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
205
206
|
reject(err);
|
|
206
207
|
}
|
|
207
208
|
else {
|
|
208
|
-
resolve(results.map((r) => this.normalizeRecord(r)));
|
|
209
|
+
resolve(results.map((r) => this.normalizeRecord(r, dbOptions)));
|
|
209
210
|
}
|
|
210
211
|
});
|
|
211
212
|
});
|
|
@@ -242,7 +243,7 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
242
243
|
}));
|
|
243
244
|
}
|
|
244
245
|
else {
|
|
245
|
-
resolve(docs ? this.normalizeRecord(docs) : numUpdated);
|
|
246
|
+
resolve(docs ? this.normalizeRecord(docs, neDbOptions) : numUpdated);
|
|
246
247
|
}
|
|
247
248
|
});
|
|
248
249
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
2
|
-
import { CreateOptions, Database } from '../types/database.types';
|
|
2
|
+
import { CreateOptions, Database, DatabaseInternalOptions } from '../types/database.types';
|
|
3
3
|
import { QueryOptions } from '../types/query.types';
|
|
4
4
|
export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
5
5
|
private collections;
|
|
@@ -22,8 +22,8 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
22
22
|
private loadCollection;
|
|
23
23
|
dropCollection(name: string): Promise<void>;
|
|
24
24
|
dropDatabase(): Promise<void>;
|
|
25
|
-
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any> | null>;
|
|
26
|
-
find(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any>[]>;
|
|
25
|
+
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any> | null>;
|
|
26
|
+
find(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>[]>;
|
|
27
27
|
update(collection: string, query: Record<string, any>, updates: Record<string, any>, neDbOptions?: Record<string, any>): Promise<number>;
|
|
28
28
|
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any>, neDbOptions?: Record<string, any>, action?: string): Promise<Record<string, any>>;
|
|
29
29
|
upsertOne(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
@@ -199,13 +199,14 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
199
199
|
return Promise.resolve();
|
|
200
200
|
});
|
|
201
201
|
}
|
|
202
|
-
findOne(collection, query, options) {
|
|
202
|
+
findOne(collection, query, options, dbOptions) {
|
|
203
203
|
return __awaiter(this, void 0, void 0, function* () {
|
|
204
|
-
const results = yield this.find(collection,
|
|
205
|
-
|
|
204
|
+
const results = yield this.find(collection, query !== null && query !== void 0 ? query : {}, Object.assign({ limit: 1 }, (options || {})), dbOptions);
|
|
205
|
+
const match = results[0];
|
|
206
|
+
return match;
|
|
206
207
|
});
|
|
207
208
|
}
|
|
208
|
-
find(collection, query, options) {
|
|
209
|
+
find(collection, query, options, dbOptions) {
|
|
209
210
|
return __awaiter(this, void 0, void 0, function* () {
|
|
210
211
|
yield this.randomDelay();
|
|
211
212
|
if ((options === null || options === void 0 ? void 0 : options.limit) === 0) {
|
|
@@ -230,7 +231,7 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
230
231
|
reject(err);
|
|
231
232
|
}
|
|
232
233
|
else {
|
|
233
|
-
resolve(results.map((r) => this.normalizeRecord(r)));
|
|
234
|
+
resolve(results.map((r) => this.normalizeRecord(r, dbOptions)));
|
|
234
235
|
}
|
|
235
236
|
});
|
|
236
237
|
});
|
|
@@ -271,7 +272,7 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
271
272
|
}));
|
|
272
273
|
}
|
|
273
274
|
else {
|
|
274
|
-
resolve(docs ? this.normalizeRecord(docs) : numUpdated);
|
|
275
|
+
resolve(docs ? this.normalizeRecord(docs, neDbOptions) : numUpdated);
|
|
275
276
|
}
|
|
276
277
|
}));
|
|
277
278
|
});
|
|
@@ -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;
|
|
@@ -226,14 +226,17 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
226
226
|
var _a, _b;
|
|
227
227
|
return __awaiter(this, void 0, void 0, function* () {
|
|
228
228
|
let resolvedQuery = query;
|
|
229
|
+
let resolvedOptions = queryOptions;
|
|
229
230
|
const { shouldTriggerWillQuery } = internalOptions;
|
|
230
231
|
if (shouldTriggerWillQuery) {
|
|
231
232
|
resolvedQuery = ((_b = (yield ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query);
|
|
233
|
+
const { query: q, options: o } = yield this.handleWillFindPlugins(resolvedQuery, queryOptions);
|
|
234
|
+
resolvedQuery = q;
|
|
235
|
+
resolvedOptions = o;
|
|
232
236
|
}
|
|
233
|
-
const {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
const results = yield this.db.find(this.collectionName, resolvedQuery, Object.assign(Object.assign({}, resolvedOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }));
|
|
237
|
+
const results = yield this.db.find(this.collectionName, resolvedQuery, Object.assign(Object.assign({}, resolvedOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }), {
|
|
238
|
+
primaryFieldNames: this.primaryFieldNames,
|
|
239
|
+
});
|
|
237
240
|
if (results) {
|
|
238
241
|
const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
|
|
239
242
|
const records = yield Promise.all(all);
|
|
@@ -306,22 +309,19 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
306
309
|
});
|
|
307
310
|
}
|
|
308
311
|
findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
|
|
309
|
-
var _a, _b, _c
|
|
312
|
+
var _a, _b, _c;
|
|
310
313
|
return __awaiter(this, void 0, void 0, function* () {
|
|
311
|
-
|
|
314
|
+
let { ops, updates: initialUpdates } = this.pluckOperations(updates);
|
|
312
315
|
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
316
|
try {
|
|
315
317
|
const isScrambled = this.isScrambled(initialUpdates);
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
shouldUpdate = false;
|
|
320
|
-
}
|
|
321
|
-
if (results === null || results === void 0 ? void 0 : results.query) {
|
|
322
|
-
q = results.query;
|
|
323
|
-
}
|
|
318
|
+
if (!isScrambled) {
|
|
319
|
+
//@ts-ignore
|
|
320
|
+
validateSchemaValues(this.updateSchema, initialUpdates);
|
|
324
321
|
}
|
|
322
|
+
const { query: qPlugins, shouldUpdate, updates: qUpdates, } = yield this.handleWillUpdateOnePlugins(q, initialUpdates);
|
|
323
|
+
q = qPlugins;
|
|
324
|
+
initialUpdates = qUpdates;
|
|
325
325
|
let current = yield this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
|
|
326
326
|
if (!current) {
|
|
327
327
|
current = yield notFoundHandler();
|
|
@@ -332,10 +332,6 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
332
332
|
throw new Error('Make sure your notFoundHandler returns a record or throws');
|
|
333
333
|
}
|
|
334
334
|
}
|
|
335
|
-
if (!isScrambled) {
|
|
336
|
-
//@ts-ignore
|
|
337
|
-
validateSchemaValues(this.updateSchema, initialUpdates);
|
|
338
|
-
}
|
|
339
335
|
if (!shouldUpdate) {
|
|
340
336
|
return current;
|
|
341
337
|
}
|
|
@@ -352,8 +348,10 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
352
348
|
for (const { name, value } of ops) {
|
|
353
349
|
normalizedValues[name] = value;
|
|
354
350
|
}
|
|
355
|
-
const results = yield this.db.updateOne(this.collectionName, q, normalizedValues
|
|
356
|
-
|
|
351
|
+
const results = yield this.db.updateOne(this.collectionName, q, normalizedValues, {
|
|
352
|
+
primaryFieldNames: this.primaryFieldNames,
|
|
353
|
+
});
|
|
354
|
+
yield ((_c = this.didUpdate) === null || _c === void 0 ? void 0 : _c.call(this, current, results));
|
|
357
355
|
return this.prepareAndNormalizeRecord(results, options);
|
|
358
356
|
}
|
|
359
357
|
catch (err) {
|
|
@@ -367,6 +365,27 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
367
365
|
}
|
|
368
366
|
});
|
|
369
367
|
}
|
|
368
|
+
handleWillUpdateOnePlugins(query, updates) {
|
|
369
|
+
var _a;
|
|
370
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
371
|
+
let shouldUpdate = true;
|
|
372
|
+
let resolvedQuery = query;
|
|
373
|
+
let resolvedUpdates = updates;
|
|
374
|
+
for (const plugin of this.plugins) {
|
|
375
|
+
const results = yield ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
|
|
376
|
+
if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
|
|
377
|
+
shouldUpdate = false;
|
|
378
|
+
}
|
|
379
|
+
if (results === null || results === void 0 ? void 0 : results.query) {
|
|
380
|
+
resolvedQuery = results.query;
|
|
381
|
+
}
|
|
382
|
+
if (results === null || results === void 0 ? void 0 : results.newValues) {
|
|
383
|
+
resolvedUpdates = results.newValues;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return { query: resolvedQuery, shouldUpdate, updates: resolvedUpdates };
|
|
387
|
+
});
|
|
388
|
+
}
|
|
370
389
|
pluckOperations(updates) {
|
|
371
390
|
const initialUpdates = __rest(updates, []);
|
|
372
391
|
const ops = saveOperations
|
|
@@ -16,9 +16,9 @@ export interface Database {
|
|
|
16
16
|
create(collection: string, values: Record<string, any>[]): Promise<Record<string, any>[]>;
|
|
17
17
|
dropCollection(name: string): Promise<void>;
|
|
18
18
|
dropDatabase(): Promise<void>;
|
|
19
|
-
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any> | null>;
|
|
20
|
-
find(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any>[]>;
|
|
21
|
-
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any
|
|
19
|
+
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any> | null>;
|
|
20
|
+
find(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>[]>;
|
|
21
|
+
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any>, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>>;
|
|
22
22
|
update(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<number>;
|
|
23
23
|
upsertOne(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
24
24
|
delete(collection: string, query: Record<string, any>): Promise<number>;
|
|
@@ -40,6 +40,8 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
|
|
|
40
40
|
}>;
|
|
41
41
|
export type UniqueIndex = string[];
|
|
42
42
|
export type Index = string[];
|
|
43
|
-
export interface CreateOptions {
|
|
43
|
+
export interface CreateOptions extends DatabaseInternalOptions {
|
|
44
|
+
}
|
|
45
|
+
export interface DatabaseInternalOptions {
|
|
44
46
|
primaryFieldNames?: string[];
|
|
45
47
|
}
|
|
@@ -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>;
|
|
@@ -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;
|
|
@@ -224,14 +224,17 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
224
224
|
}) {
|
|
225
225
|
var _a, _b;
|
|
226
226
|
let resolvedQuery = query;
|
|
227
|
+
let resolvedOptions = queryOptions;
|
|
227
228
|
const { shouldTriggerWillQuery } = internalOptions;
|
|
228
229
|
if (shouldTriggerWillQuery) {
|
|
229
230
|
resolvedQuery = ((_b = (await ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query);
|
|
231
|
+
const { query: q, options: o } = await this.handleWillFindPlugins(resolvedQuery, queryOptions);
|
|
232
|
+
resolvedQuery = q;
|
|
233
|
+
resolvedOptions = o;
|
|
230
234
|
}
|
|
231
|
-
const {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
const results = await this.db.find(this.collectionName, resolvedQuery, Object.assign(Object.assign({}, resolvedOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }));
|
|
235
|
+
const results = await this.db.find(this.collectionName, resolvedQuery, Object.assign(Object.assign({}, resolvedOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }), {
|
|
236
|
+
primaryFieldNames: this.primaryFieldNames,
|
|
237
|
+
});
|
|
235
238
|
if (results) {
|
|
236
239
|
const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
|
|
237
240
|
const records = await Promise.all(all);
|
|
@@ -293,21 +296,18 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
293
296
|
return this.db.update(this.collectionName, query, updates);
|
|
294
297
|
}
|
|
295
298
|
async findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
|
|
296
|
-
var _a, _b, _c
|
|
297
|
-
|
|
299
|
+
var _a, _b, _c;
|
|
300
|
+
let { ops, updates: initialUpdates } = this.pluckOperations(updates);
|
|
298
301
|
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
302
|
try {
|
|
301
303
|
const isScrambled = this.isScrambled(initialUpdates);
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
shouldUpdate = false;
|
|
306
|
-
}
|
|
307
|
-
if (results === null || results === void 0 ? void 0 : results.query) {
|
|
308
|
-
q = results.query;
|
|
309
|
-
}
|
|
304
|
+
if (!isScrambled) {
|
|
305
|
+
//@ts-ignore
|
|
306
|
+
(0, schema_1.validateSchemaValues)(this.updateSchema, initialUpdates);
|
|
310
307
|
}
|
|
308
|
+
const { query: qPlugins, shouldUpdate, updates: qUpdates, } = await this.handleWillUpdateOnePlugins(q, initialUpdates);
|
|
309
|
+
q = qPlugins;
|
|
310
|
+
initialUpdates = qUpdates;
|
|
311
311
|
let current = await this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
|
|
312
312
|
if (!current) {
|
|
313
313
|
current = await notFoundHandler();
|
|
@@ -318,10 +318,6 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
318
318
|
throw new Error('Make sure your notFoundHandler returns a record or throws');
|
|
319
319
|
}
|
|
320
320
|
}
|
|
321
|
-
if (!isScrambled) {
|
|
322
|
-
//@ts-ignore
|
|
323
|
-
(0, schema_1.validateSchemaValues)(this.updateSchema, initialUpdates);
|
|
324
|
-
}
|
|
325
321
|
if (!shouldUpdate) {
|
|
326
322
|
return current;
|
|
327
323
|
}
|
|
@@ -338,8 +334,10 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
338
334
|
for (const { name, value } of ops) {
|
|
339
335
|
normalizedValues[name] = value;
|
|
340
336
|
}
|
|
341
|
-
const results = await this.db.updateOne(this.collectionName, q, normalizedValues
|
|
342
|
-
|
|
337
|
+
const results = await this.db.updateOne(this.collectionName, q, normalizedValues, {
|
|
338
|
+
primaryFieldNames: this.primaryFieldNames,
|
|
339
|
+
});
|
|
340
|
+
await ((_c = this.didUpdate) === null || _c === void 0 ? void 0 : _c.call(this, current, results));
|
|
343
341
|
return this.prepareAndNormalizeRecord(results, options);
|
|
344
342
|
}
|
|
345
343
|
catch (err) {
|
|
@@ -352,6 +350,25 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
352
350
|
throw coded[0];
|
|
353
351
|
}
|
|
354
352
|
}
|
|
353
|
+
async handleWillUpdateOnePlugins(query, updates) {
|
|
354
|
+
var _a;
|
|
355
|
+
let shouldUpdate = true;
|
|
356
|
+
let resolvedQuery = query;
|
|
357
|
+
let resolvedUpdates = updates;
|
|
358
|
+
for (const plugin of this.plugins) {
|
|
359
|
+
const results = await ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
|
|
360
|
+
if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
|
|
361
|
+
shouldUpdate = false;
|
|
362
|
+
}
|
|
363
|
+
if (results === null || results === void 0 ? void 0 : results.query) {
|
|
364
|
+
resolvedQuery = results.query;
|
|
365
|
+
}
|
|
366
|
+
if (results === null || results === void 0 ? void 0 : results.newValues) {
|
|
367
|
+
resolvedUpdates = results.newValues;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
return { query: resolvedQuery, shouldUpdate, updates: resolvedUpdates };
|
|
371
|
+
}
|
|
355
372
|
pluckOperations(updates) {
|
|
356
373
|
const initialUpdates = __rest(updates, []);
|
|
357
374
|
const ops = stores_types_1.saveOperations
|
|
@@ -16,9 +16,9 @@ export interface Database {
|
|
|
16
16
|
create(collection: string, values: Record<string, any>[]): Promise<Record<string, any>[]>;
|
|
17
17
|
dropCollection(name: string): Promise<void>;
|
|
18
18
|
dropDatabase(): Promise<void>;
|
|
19
|
-
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any> | null>;
|
|
20
|
-
find(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any>[]>;
|
|
21
|
-
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any
|
|
19
|
+
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any> | null>;
|
|
20
|
+
find(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>[]>;
|
|
21
|
+
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any>, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>>;
|
|
22
22
|
update(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<number>;
|
|
23
23
|
upsertOne(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
24
24
|
delete(collection: string, query: Record<string, any>): Promise<number>;
|
|
@@ -40,6 +40,8 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
|
|
|
40
40
|
}>;
|
|
41
41
|
export type UniqueIndex = string[];
|
|
42
42
|
export type Index = string[];
|
|
43
|
-
export interface CreateOptions {
|
|
43
|
+
export interface CreateOptions extends DatabaseInternalOptions {
|
|
44
|
+
}
|
|
45
|
+
export interface DatabaseInternalOptions {
|
|
44
46
|
primaryFieldNames?: string[];
|
|
45
47
|
}
|
|
@@ -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>;
|