@sprucelabs/data-stores 26.1.7 → 26.2.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.
- package/build/esm/index.d.ts +1 -0
- package/build/esm/index.js +1 -0
- package/build/esm/plugins/DatabaseFieldMapperPlugin.d.ts +10 -0
- package/build/esm/plugins/DatabaseFieldMapperPlugin.js +42 -0
- package/build/esm/stores/AbstractStore.d.ts +2 -1
- package/build/esm/stores/AbstractStore.js +19 -6
- package/build/esm/types/stores.types.d.ts +6 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +3 -1
- package/build/plugins/DatabaseFieldMapperPlugin.d.ts +10 -0
- package/build/plugins/DatabaseFieldMapperPlugin.js +30 -0
- package/build/stores/AbstractStore.d.ts +2 -1
- package/build/stores/AbstractStore.js +17 -6
- package/build/types/stores.types.d.ts +6 -0
- package/package.json +3 -3
package/build/esm/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/esm/index.js
CHANGED
|
@@ -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
|
*/
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { QueryOptions } from '../types/query.types';
|
|
2
|
+
import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse } from '../types/stores.types';
|
|
3
|
+
export default class DatabaseFieldMapperPlugin implements DataStorePlugin {
|
|
4
|
+
private mapper;
|
|
5
|
+
constructor(map: Record<string, any>);
|
|
6
|
+
getName(): string;
|
|
7
|
+
willCreateOne(values: Record<string, any>): Promise<void | DataStorePluginWillCreateOneResponse>;
|
|
8
|
+
prepareRecord(record: Record<string, any>): Promise<void | DataStorePluginPrepareResponse>;
|
|
9
|
+
willFind(query: Record<string, any>, options?: QueryOptions): Promise<void | DataStorePluginWillFindResponse>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { KeyMapper } from '@sprucelabs/schema';
|
|
11
|
+
export default class DatabaseFieldMapperPlugin {
|
|
12
|
+
constructor(map) {
|
|
13
|
+
this.mapper = new KeyMapper(map);
|
|
14
|
+
}
|
|
15
|
+
getName() {
|
|
16
|
+
return 'fieldMapper';
|
|
17
|
+
}
|
|
18
|
+
willCreateOne(values) {
|
|
19
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
+
return {
|
|
21
|
+
newValues: this.mapper.mapTo(values),
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
prepareRecord(record) {
|
|
26
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
+
return {
|
|
28
|
+
newValues: this.mapper.mapFrom(record),
|
|
29
|
+
};
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
willFind(query, options) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
let { sort } = options !== null && options !== void 0 ? options : {};
|
|
35
|
+
sort = sort === null || sort === void 0 ? void 0 : sort.map((s) => (Object.assign(Object.assign({}, s), { field: this.mapper.mapFieldNameTo(s.field) })));
|
|
36
|
+
return {
|
|
37
|
+
query: this.mapper.mapTo(query),
|
|
38
|
+
options: Object.assign(Object.assign({}, options), { sort }),
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -25,7 +25,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
25
25
|
protected willScramble?(values: Partial<DatabaseRecord> & {
|
|
26
26
|
_isScrambled: true;
|
|
27
27
|
}): Promise<Partial<DatabaseRecord>>;
|
|
28
|
-
protected
|
|
28
|
+
protected willFind?(query: QueryBuilder<QueryRecord>): Promise<QueryBuilder<Partial<DatabaseRecord>>>;
|
|
29
29
|
protected constructor(db: Database, collectionName?: string);
|
|
30
30
|
protected setCollectionName(name: string): void;
|
|
31
31
|
getDb(): Database;
|
|
@@ -44,6 +44,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
44
44
|
count(query?: QueryBuilder<QueryRecord>): Promise<number>;
|
|
45
45
|
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>>>[]>;
|
|
46
46
|
private _find;
|
|
47
|
+
private handleWillFindPlugins;
|
|
47
48
|
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>>>;
|
|
48
49
|
upsertOne<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: UpdateRecord & CreateRecord & {
|
|
49
50
|
id?: string;
|
|
@@ -225,12 +225,15 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
225
225
|
}) {
|
|
226
226
|
var _a, _b;
|
|
227
227
|
return __awaiter(this, void 0, void 0, function* () {
|
|
228
|
-
let
|
|
228
|
+
let resolvedQuery = query;
|
|
229
229
|
const { shouldTriggerWillQuery } = internalOptions;
|
|
230
230
|
if (shouldTriggerWillQuery) {
|
|
231
|
-
|
|
231
|
+
resolvedQuery = ((_b = (yield ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query);
|
|
232
232
|
}
|
|
233
|
-
const
|
|
233
|
+
const { query: q, options: o } = yield this.handleWillFindPlugins(resolvedQuery, queryOptions);
|
|
234
|
+
resolvedQuery = q;
|
|
235
|
+
const resolvedOptions = o !== null && o !== void 0 ? o : queryOptions;
|
|
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 }));
|
|
234
237
|
if (results) {
|
|
235
238
|
const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
|
|
236
239
|
const records = yield Promise.all(all);
|
|
@@ -239,6 +242,16 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
239
242
|
return [];
|
|
240
243
|
});
|
|
241
244
|
}
|
|
245
|
+
handleWillFindPlugins(query, queryOptions) {
|
|
246
|
+
var _a, _b;
|
|
247
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
248
|
+
const plugin = this.plugins[0];
|
|
249
|
+
const { query: pluginQuery, options: pluginOptions } = (_b = (yield ((_a = plugin === null || plugin === void 0 ? void 0 : plugin.willFind) === null || _a === void 0 ? void 0 : _a.call(plugin, query, queryOptions)))) !== null && _b !== void 0 ? _b : {};
|
|
250
|
+
query = pluginQuery !== null && pluginQuery !== void 0 ? pluginQuery : query;
|
|
251
|
+
const options = pluginOptions !== null && pluginOptions !== void 0 ? pluginOptions : queryOptions;
|
|
252
|
+
return { options, query };
|
|
253
|
+
});
|
|
254
|
+
}
|
|
242
255
|
findBatch(query, options) {
|
|
243
256
|
return __awaiter(this, void 0, void 0, function* () {
|
|
244
257
|
return BatchCursorImpl.Cursor(this, query, options);
|
|
@@ -296,7 +309,7 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
296
309
|
var _a, _b, _c, _d;
|
|
297
310
|
return __awaiter(this, void 0, void 0, function* () {
|
|
298
311
|
const { ops, updates: initialUpdates } = this.pluckOperations(updates);
|
|
299
|
-
let q = (_b = (yield ((_a = this.
|
|
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;
|
|
300
313
|
let shouldUpdate = true;
|
|
301
314
|
try {
|
|
302
315
|
const isScrambled = this.isScrambled(initialUpdates);
|
|
@@ -406,7 +419,7 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
406
419
|
deleteOne(query) {
|
|
407
420
|
var _a, _b, _c, _d;
|
|
408
421
|
return __awaiter(this, void 0, void 0, function* () {
|
|
409
|
-
let q = (_b = (yield ((_a = this.
|
|
422
|
+
let q = (_b = (yield ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : Object.assign({}, query);
|
|
410
423
|
for (const plugin of this.plugins) {
|
|
411
424
|
const { query } = (_d = (yield ((_c = plugin.willDeleteOne) === null || _c === void 0 ? void 0 : _c.call(plugin, q)))) !== null && _d !== void 0 ? _d : {};
|
|
412
425
|
if (query) {
|
|
@@ -419,7 +432,7 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
419
432
|
delete(query) {
|
|
420
433
|
var _a, _b;
|
|
421
434
|
return __awaiter(this, void 0, void 0, function* () {
|
|
422
|
-
const q = (_b = (yield ((_a = this.
|
|
435
|
+
const q = (_b = (yield ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : Object.assign({}, query);
|
|
423
436
|
return yield this.db.delete(this.collectionName, q);
|
|
424
437
|
});
|
|
425
438
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Schema, SchemaFieldNames, SchemaPublicValues, SchemaValues } from '@sprucelabs/schema';
|
|
2
2
|
import StoreFactory from '../factories/StoreFactory';
|
|
3
3
|
import { Database } from './database.types';
|
|
4
|
+
import { QueryOptions } from './query.types';
|
|
4
5
|
export declare const saveOperations: readonly ["$push", "$inc", "$min", "$max", "$mul", "$push", "$pull", "$pop"];
|
|
5
6
|
export type SaveOperation = (typeof saveOperations)[number];
|
|
6
7
|
export type SaveOperations = Partial<Record<SaveOperation, Record<string, any>>>;
|
|
@@ -38,6 +39,7 @@ export interface DataStorePlugin {
|
|
|
38
39
|
didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
|
|
39
40
|
getName(): string;
|
|
40
41
|
prepareRecord?: (record: Record<string, any>) => Promise<void | DataStorePluginPrepareResponse>;
|
|
42
|
+
willFind?: (query: Record<string, any>, options?: QueryOptions) => Promise<void | DataStorePluginWillFindResponse>;
|
|
41
43
|
}
|
|
42
44
|
export interface DataStorePluginPrepareResponse {
|
|
43
45
|
newValues?: Record<string, any>;
|
|
@@ -59,3 +61,7 @@ export interface DataStorePluginWillDeleteOneResponse {
|
|
|
59
61
|
export interface DataStorePluginDidFindOneResponse {
|
|
60
62
|
valuesToMixinBeforeReturning?: Record<string, any>;
|
|
61
63
|
}
|
|
64
|
+
export interface DataStorePluginWillFindResponse {
|
|
65
|
+
query?: Record<string, any>;
|
|
66
|
+
options?: QueryOptions;
|
|
67
|
+
}
|
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
|
*/
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { QueryOptions } from '../types/query.types';
|
|
2
|
+
import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse } from '../types/stores.types';
|
|
3
|
+
export default class DatabaseFieldMapperPlugin implements DataStorePlugin {
|
|
4
|
+
private mapper;
|
|
5
|
+
constructor(map: Record<string, any>);
|
|
6
|
+
getName(): string;
|
|
7
|
+
willCreateOne(values: Record<string, any>): Promise<void | DataStorePluginWillCreateOneResponse>;
|
|
8
|
+
prepareRecord(record: Record<string, any>): Promise<void | DataStorePluginPrepareResponse>;
|
|
9
|
+
willFind(query: Record<string, any>, options?: QueryOptions): Promise<void | DataStorePluginWillFindResponse>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
4
|
+
class DatabaseFieldMapperPlugin {
|
|
5
|
+
constructor(map) {
|
|
6
|
+
this.mapper = new schema_1.KeyMapper(map);
|
|
7
|
+
}
|
|
8
|
+
getName() {
|
|
9
|
+
return 'fieldMapper';
|
|
10
|
+
}
|
|
11
|
+
async willCreateOne(values) {
|
|
12
|
+
return {
|
|
13
|
+
newValues: this.mapper.mapTo(values),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
async prepareRecord(record) {
|
|
17
|
+
return {
|
|
18
|
+
newValues: this.mapper.mapFrom(record),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
async willFind(query, options) {
|
|
22
|
+
let { sort } = options !== null && options !== void 0 ? options : {};
|
|
23
|
+
sort = sort === null || sort === void 0 ? void 0 : sort.map((s) => (Object.assign(Object.assign({}, s), { field: this.mapper.mapFieldNameTo(s.field) })));
|
|
24
|
+
return {
|
|
25
|
+
query: this.mapper.mapTo(query),
|
|
26
|
+
options: Object.assign(Object.assign({}, options), { sort }),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.default = DatabaseFieldMapperPlugin;
|
|
@@ -25,7 +25,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
25
25
|
protected willScramble?(values: Partial<DatabaseRecord> & {
|
|
26
26
|
_isScrambled: true;
|
|
27
27
|
}): Promise<Partial<DatabaseRecord>>;
|
|
28
|
-
protected
|
|
28
|
+
protected willFind?(query: QueryBuilder<QueryRecord>): Promise<QueryBuilder<Partial<DatabaseRecord>>>;
|
|
29
29
|
protected constructor(db: Database, collectionName?: string);
|
|
30
30
|
protected setCollectionName(name: string): void;
|
|
31
31
|
getDb(): Database;
|
|
@@ -44,6 +44,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
44
44
|
count(query?: QueryBuilder<QueryRecord>): Promise<number>;
|
|
45
45
|
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>>>[]>;
|
|
46
46
|
private _find;
|
|
47
|
+
private handleWillFindPlugins;
|
|
47
48
|
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>>>;
|
|
48
49
|
upsertOne<IncludePrivateFields extends boolean = true, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: UpdateRecord & CreateRecord & {
|
|
49
50
|
id?: string;
|
|
@@ -223,12 +223,15 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
223
223
|
shouldTriggerWillQuery: true,
|
|
224
224
|
}) {
|
|
225
225
|
var _a, _b;
|
|
226
|
-
let
|
|
226
|
+
let resolvedQuery = query;
|
|
227
227
|
const { shouldTriggerWillQuery } = internalOptions;
|
|
228
228
|
if (shouldTriggerWillQuery) {
|
|
229
|
-
|
|
229
|
+
resolvedQuery = ((_b = (await ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query);
|
|
230
230
|
}
|
|
231
|
-
const
|
|
231
|
+
const { query: q, options: o } = await this.handleWillFindPlugins(resolvedQuery, queryOptions);
|
|
232
|
+
resolvedQuery = q;
|
|
233
|
+
const resolvedOptions = o !== null && o !== void 0 ? o : queryOptions;
|
|
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 }));
|
|
232
235
|
if (results) {
|
|
233
236
|
const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
|
|
234
237
|
const records = await Promise.all(all);
|
|
@@ -236,6 +239,14 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
236
239
|
}
|
|
237
240
|
return [];
|
|
238
241
|
}
|
|
242
|
+
async handleWillFindPlugins(query, queryOptions) {
|
|
243
|
+
var _a, _b;
|
|
244
|
+
const plugin = this.plugins[0];
|
|
245
|
+
const { query: pluginQuery, options: pluginOptions } = (_b = (await ((_a = plugin === null || plugin === void 0 ? void 0 : plugin.willFind) === null || _a === void 0 ? void 0 : _a.call(plugin, query, queryOptions)))) !== null && _b !== void 0 ? _b : {};
|
|
246
|
+
query = pluginQuery !== null && pluginQuery !== void 0 ? pluginQuery : query;
|
|
247
|
+
const options = pluginOptions !== null && pluginOptions !== void 0 ? pluginOptions : queryOptions;
|
|
248
|
+
return { options, query };
|
|
249
|
+
}
|
|
239
250
|
async findBatch(query, options) {
|
|
240
251
|
return BatchCursor_1.default.Cursor(this, query, options);
|
|
241
252
|
}
|
|
@@ -284,7 +295,7 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
284
295
|
async findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
|
|
285
296
|
var _a, _b, _c, _d;
|
|
286
297
|
const { ops, updates: initialUpdates } = this.pluckOperations(updates);
|
|
287
|
-
let q = (_b = (await ((_a = this.
|
|
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;
|
|
288
299
|
let shouldUpdate = true;
|
|
289
300
|
try {
|
|
290
301
|
const isScrambled = this.isScrambled(initialUpdates);
|
|
@@ -388,7 +399,7 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
388
399
|
}
|
|
389
400
|
async deleteOne(query) {
|
|
390
401
|
var _a, _b, _c, _d;
|
|
391
|
-
let q = (_b = (await ((_a = this.
|
|
402
|
+
let q = (_b = (await ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : Object.assign({}, query);
|
|
392
403
|
for (const plugin of this.plugins) {
|
|
393
404
|
const { query } = (_d = (await ((_c = plugin.willDeleteOne) === null || _c === void 0 ? void 0 : _c.call(plugin, q)))) !== null && _d !== void 0 ? _d : {};
|
|
394
405
|
if (query) {
|
|
@@ -399,7 +410,7 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
399
410
|
}
|
|
400
411
|
async delete(query) {
|
|
401
412
|
var _a, _b;
|
|
402
|
-
const q = (_b = (await ((_a = this.
|
|
413
|
+
const q = (_b = (await ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, Object.assign({}, query))))) !== null && _b !== void 0 ? _b : Object.assign({}, query);
|
|
403
414
|
return await this.db.delete(this.collectionName, q);
|
|
404
415
|
}
|
|
405
416
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Schema, SchemaFieldNames, SchemaPublicValues, SchemaValues } from '@sprucelabs/schema';
|
|
2
2
|
import StoreFactory from '../factories/StoreFactory';
|
|
3
3
|
import { Database } from './database.types';
|
|
4
|
+
import { QueryOptions } from './query.types';
|
|
4
5
|
export declare const saveOperations: readonly ["$push", "$inc", "$min", "$max", "$mul", "$push", "$pull", "$pop"];
|
|
5
6
|
export type SaveOperation = (typeof saveOperations)[number];
|
|
6
7
|
export type SaveOperations = Partial<Record<SaveOperation, Record<string, any>>>;
|
|
@@ -38,6 +39,7 @@ export interface DataStorePlugin {
|
|
|
38
39
|
didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
|
|
39
40
|
getName(): string;
|
|
40
41
|
prepareRecord?: (record: Record<string, any>) => Promise<void | DataStorePluginPrepareResponse>;
|
|
42
|
+
willFind?: (query: Record<string, any>, options?: QueryOptions) => Promise<void | DataStorePluginWillFindResponse>;
|
|
41
43
|
}
|
|
42
44
|
export interface DataStorePluginPrepareResponse {
|
|
43
45
|
newValues?: Record<string, any>;
|
|
@@ -59,3 +61,7 @@ export interface DataStorePluginWillDeleteOneResponse {
|
|
|
59
61
|
export interface DataStorePluginDidFindOneResponse {
|
|
60
62
|
valuesToMixinBeforeReturning?: Record<string, any>;
|
|
61
63
|
}
|
|
64
|
+
export interface DataStorePluginWillFindResponse {
|
|
65
|
+
query?: Record<string, any>;
|
|
66
|
+
options?: QueryOptions;
|
|
67
|
+
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "26.1
|
|
6
|
+
"version": "26.2.1",
|
|
7
7
|
"files": [
|
|
8
8
|
"build/**/*",
|
|
9
9
|
"!build/__tests__",
|
|
@@ -70,8 +70,8 @@
|
|
|
70
70
|
"dependencies": {
|
|
71
71
|
"@sprucelabs/error": "^5.1.52",
|
|
72
72
|
"@sprucelabs/globby": "^1.0.3",
|
|
73
|
-
"@sprucelabs/schema": "^29.
|
|
74
|
-
"@sprucelabs/spruce-skill-utils": "^30.1.
|
|
73
|
+
"@sprucelabs/schema": "^29.2.0",
|
|
74
|
+
"@sprucelabs/spruce-skill-utils": "^30.1.40",
|
|
75
75
|
"just-clone": "^6.2.0",
|
|
76
76
|
"lodash": "^4.17.21",
|
|
77
77
|
"mongodb": "^6.3.0",
|