@sprucelabs/data-stores 25.9.0 → 25.11.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.
- package/build/esm/index.d.ts +1 -0
- package/build/esm/index.js +1 -0
- package/build/esm/stores/AbstractStore.d.ts +3 -3
- package/build/esm/stores/AbstractStore.js +30 -9
- package/build/esm/tests/storePluginAssert.d.ts +6 -0
- package/build/esm/tests/storePluginAssert.js +21 -0
- package/build/esm/types/database.types.d.ts +4 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +3 -1
- package/build/stores/AbstractStore.d.ts +3 -3
- package/build/stores/AbstractStore.js +26 -9
- package/build/tests/storePluginAssert.d.ts +6 -0
- package/build/tests/storePluginAssert.js +23 -0
- package/build/types/database.types.d.ts +4 -0
- package/package.json +1 -1
package/build/esm/index.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export * from './cursors/CursorPager';
|
|
|
25
25
|
*/
|
|
26
26
|
export { default as databaseAssertUtil } from './tests/databaseAssertUtil';
|
|
27
27
|
export { default as databaseAssert } from './tests/databaseAssertUtil';
|
|
28
|
+
export { default as storePluginAssert } from './tests/storePluginAssert';
|
|
28
29
|
export { default as BatchCursorImpl } from './cursors/BatchCursor';
|
|
29
30
|
export * from './cursors/BatchCursor';
|
|
30
31
|
export { default as BatchArrayCursor } from './cursors/BatchArrayCursor';
|
package/build/esm/index.js
CHANGED
|
@@ -25,6 +25,7 @@ export * from './cursors/CursorPager.js';
|
|
|
25
25
|
*/
|
|
26
26
|
export { default as databaseAssertUtil } from './tests/databaseAssertUtil.js';
|
|
27
27
|
export { default as databaseAssert } from './tests/databaseAssertUtil.js';
|
|
28
|
+
export { default as storePluginAssert } from './tests/storePluginAssert.js';
|
|
28
29
|
export { default as BatchCursorImpl } from './cursors/BatchCursor.js';
|
|
29
30
|
export * from './cursors/BatchCursor.js';
|
|
30
31
|
export { default as BatchArrayCursor } from './cursors/BatchArrayCursor.js';
|
|
@@ -4,9 +4,7 @@ import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
|
4
4
|
import { DataStorePlugin, Database } from '../types/database.types';
|
|
5
5
|
import { QueryBuilder, QueryOptions } from '../types/query.types';
|
|
6
6
|
import { PrepareOptions, PrepareResults, SaveOperations, DataStore } from '../types/stores.types';
|
|
7
|
-
export default abstract class AbstractStore<FullSchema extends Schema, CreateSchema extends Schema = FullSchema, UpdateSchema extends Schema = CreateSchema, DatabaseSchema extends Schema = FullSchema, DatabaseRecord = SchemaValues<DatabaseSchema> & {
|
|
8
|
-
id: string;
|
|
9
|
-
}, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
|
|
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 {
|
|
10
8
|
abstract readonly name: string;
|
|
11
9
|
protected abstract collectionName: string;
|
|
12
10
|
protected abstract createSchema: CreateSchema;
|
|
@@ -35,8 +33,10 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
35
33
|
private mapCasing;
|
|
36
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>[]>;
|
|
37
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;
|
|
38
37
|
private get primaryFieldName();
|
|
39
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;
|
|
40
40
|
count(query?: QueryBuilder<QueryRecord>): Promise<number>;
|
|
41
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>>>[]>;
|
|
42
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
|
|
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
|
-
|
|
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 ((
|
|
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
|
-
|
|
146
|
-
|
|
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);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Schema } from '@sprucelabs/schema';
|
|
2
|
+
import AbstractStore from '../stores/AbstractStore';
|
|
3
|
+
declare const storePluginAssert: {
|
|
4
|
+
storeHasPlugin: (store: AbstractStore<Schema>, pluginName: string) => import("..").DataStorePlugin;
|
|
5
|
+
};
|
|
6
|
+
export default storePluginAssert;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { assertOptions } from '@sprucelabs/schema';
|
|
2
|
+
import { assert } from '@sprucelabs/test-utils';
|
|
3
|
+
const storePluginAssert = {
|
|
4
|
+
storeHasPlugin: (store, pluginName) => {
|
|
5
|
+
assertOptions({
|
|
6
|
+
store,
|
|
7
|
+
pluginName,
|
|
8
|
+
}, ['store', 'pluginName']);
|
|
9
|
+
//@ts-ignore
|
|
10
|
+
const { plugins } = store;
|
|
11
|
+
if (!(plugins === null || plugins === void 0 ? void 0 : plugins.length)) {
|
|
12
|
+
assert.fail(`The store you passed has no plugins. Add one to 'protected plugins: DataStorePlugin[] = [...]' or add it in the constructor of your store with 'this.plugins[...]'`);
|
|
13
|
+
}
|
|
14
|
+
const plugin = plugins === null || plugins === void 0 ? void 0 : plugins.find((p) => p.getName() === pluginName);
|
|
15
|
+
if ((plugin === null || plugin === void 0 ? void 0 : plugin.getName()) !== pluginName) {
|
|
16
|
+
assert.fail(`I could not find the plugin '${pluginName}' in the store you passed. Make sure you added it to 'protected plugins: DataStorePlugin[] = [...]' or added it in the constructor of your store with 'this.plugins[...]'`);
|
|
17
|
+
}
|
|
18
|
+
return plugin;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
export default storePluginAssert;
|
|
@@ -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/build/index.d.ts
CHANGED
|
@@ -25,6 +25,7 @@ export * from './cursors/CursorPager';
|
|
|
25
25
|
*/
|
|
26
26
|
export { default as databaseAssertUtil } from './tests/databaseAssertUtil';
|
|
27
27
|
export { default as databaseAssert } from './tests/databaseAssertUtil';
|
|
28
|
+
export { default as storePluginAssert } from './tests/storePluginAssert';
|
|
28
29
|
export { default as BatchCursorImpl } from './cursors/BatchCursor';
|
|
29
30
|
export * from './cursors/BatchCursor';
|
|
30
31
|
export { default as BatchArrayCursor } from './cursors/BatchArrayCursor';
|
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.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.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");
|
|
@@ -60,6 +60,8 @@ var databaseAssertUtil_1 = require("./tests/databaseAssertUtil");
|
|
|
60
60
|
Object.defineProperty(exports, "databaseAssertUtil", { enumerable: true, get: function () { return __importDefault(databaseAssertUtil_1).default; } });
|
|
61
61
|
var databaseAssertUtil_2 = require("./tests/databaseAssertUtil");
|
|
62
62
|
Object.defineProperty(exports, "databaseAssert", { enumerable: true, get: function () { return __importDefault(databaseAssertUtil_2).default; } });
|
|
63
|
+
var storePluginAssert_1 = require("./tests/storePluginAssert");
|
|
64
|
+
Object.defineProperty(exports, "storePluginAssert", { enumerable: true, get: function () { return __importDefault(storePluginAssert_1).default; } });
|
|
63
65
|
var BatchCursor_1 = require("./cursors/BatchCursor");
|
|
64
66
|
Object.defineProperty(exports, "BatchCursorImpl", { enumerable: true, get: function () { return __importDefault(BatchCursor_1).default; } });
|
|
65
67
|
__exportStar(require("./cursors/BatchCursor"), exports);
|
|
@@ -4,9 +4,7 @@ import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
|
4
4
|
import { DataStorePlugin, Database } from '../types/database.types';
|
|
5
5
|
import { QueryBuilder, QueryOptions } from '../types/query.types';
|
|
6
6
|
import { PrepareOptions, PrepareResults, SaveOperations, DataStore } from '../types/stores.types';
|
|
7
|
-
export default abstract class AbstractStore<FullSchema extends Schema, CreateSchema extends Schema = FullSchema, UpdateSchema extends Schema = CreateSchema, DatabaseSchema extends Schema = FullSchema, DatabaseRecord = SchemaValues<DatabaseSchema> & {
|
|
8
|
-
id: string;
|
|
9
|
-
}, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
|
|
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 {
|
|
10
8
|
abstract readonly name: string;
|
|
11
9
|
protected abstract collectionName: string;
|
|
12
10
|
protected abstract createSchema: CreateSchema;
|
|
@@ -35,8 +33,10 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
35
33
|
private mapCasing;
|
|
36
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>[]>;
|
|
37
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;
|
|
38
37
|
private get primaryFieldName();
|
|
39
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;
|
|
40
40
|
count(query?: QueryBuilder<QueryRecord>): Promise<number>;
|
|
41
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>>>[]>;
|
|
42
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
|
|
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
|
-
|
|
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 ((
|
|
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
|
-
|
|
158
|
-
|
|
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;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Schema } from '@sprucelabs/schema';
|
|
2
|
+
import AbstractStore from '../stores/AbstractStore';
|
|
3
|
+
declare const storePluginAssert: {
|
|
4
|
+
storeHasPlugin: (store: AbstractStore<Schema>, pluginName: string) => import("..").DataStorePlugin;
|
|
5
|
+
};
|
|
6
|
+
export default storePluginAssert;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
4
|
+
const test_utils_1 = require("@sprucelabs/test-utils");
|
|
5
|
+
const storePluginAssert = {
|
|
6
|
+
storeHasPlugin: (store, pluginName) => {
|
|
7
|
+
(0, schema_1.assertOptions)({
|
|
8
|
+
store,
|
|
9
|
+
pluginName,
|
|
10
|
+
}, ['store', 'pluginName']);
|
|
11
|
+
//@ts-ignore
|
|
12
|
+
const { plugins } = store;
|
|
13
|
+
if (!(plugins === null || plugins === void 0 ? void 0 : plugins.length)) {
|
|
14
|
+
test_utils_1.assert.fail(`The store you passed has no plugins. Add one to 'protected plugins: DataStorePlugin[] = [...]' or add it in the constructor of your store with 'this.plugins[...]'`);
|
|
15
|
+
}
|
|
16
|
+
const plugin = plugins === null || plugins === void 0 ? void 0 : plugins.find((p) => p.getName() === pluginName);
|
|
17
|
+
if ((plugin === null || plugin === void 0 ? void 0 : plugin.getName()) !== pluginName) {
|
|
18
|
+
test_utils_1.assert.fail(`I could not find the plugin '${pluginName}' in the store you passed. Make sure you added it to 'protected plugins: DataStorePlugin[] = [...]' or added it in the constructor of your store with 'this.plugins[...]'`);
|
|
19
|
+
}
|
|
20
|
+
return plugin;
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
exports.default = storePluginAssert;
|
|
@@ -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
|
+
}
|