@sprucelabs/data-stores 25.11.2 → 25.12.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/stores/AbstractStore.d.ts +2 -1
- package/build/esm/stores/AbstractStore.js +19 -4
- package/build/esm/types/database.types.d.ts +6 -2
- package/build/stores/AbstractStore.d.ts +2 -1
- package/build/stores/AbstractStore.js +17 -4
- package/build/types/database.types.d.ts +6 -2
- package/package.json +1 -1
|
@@ -33,7 +33,8 @@ 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
|
|
36
|
+
private handleWillCreatePlugins;
|
|
37
|
+
private handleDidCreateForPlugins;
|
|
37
38
|
private get primaryFieldName();
|
|
38
39
|
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
40
|
private handleDidFindForPlugins;
|
|
@@ -104,6 +104,7 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
104
104
|
var _a, _b;
|
|
105
105
|
return __awaiter(this, void 0, void 0, function* () {
|
|
106
106
|
try {
|
|
107
|
+
let valuesToMixinBeforeCreate = yield this.handleWillCreatePlugins(values);
|
|
107
108
|
//@ts-ignore
|
|
108
109
|
validateSchemaValues(this.createSchema, values);
|
|
109
110
|
const cleanedValues = this.willCreate
|
|
@@ -115,10 +116,10 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
115
116
|
const toSave = normalizeSchemaValues(this.databaseSchema,
|
|
116
117
|
//@ts-ignore
|
|
117
118
|
databaseRecord, { shouldCreateEntityInstances: false });
|
|
118
|
-
const
|
|
119
|
-
const record = yield this.db.createOne(this.collectionName, toSave);
|
|
119
|
+
const record = yield this.db.createOne(this.collectionName, Object.assign(Object.assign({}, toSave), valuesToMixinBeforeCreate));
|
|
120
120
|
yield ((_b = this.didCreate) === null || _b === void 0 ? void 0 : _b.call(this, record));
|
|
121
121
|
const normalized = yield this.prepareAndNormalizeRecord(record, options);
|
|
122
|
+
const mixinValuesOnReturn = yield this.handleDidCreateForPlugins(normalized);
|
|
122
123
|
return Object.assign(Object.assign({}, normalized), mixinValuesOnReturn);
|
|
123
124
|
}
|
|
124
125
|
catch (err) {
|
|
@@ -132,12 +133,26 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
132
133
|
}
|
|
133
134
|
});
|
|
134
135
|
}
|
|
135
|
-
|
|
136
|
+
handleWillCreatePlugins(values) {
|
|
137
|
+
var _a;
|
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
let valuesToMixinBeforeCreate = {};
|
|
140
|
+
for (const plugin of this.plugins) {
|
|
141
|
+
const r = yield ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, values));
|
|
142
|
+
const { valuesToMixinBeforeCreate: v } = r !== null && r !== void 0 ? r : {};
|
|
143
|
+
if (v) {
|
|
144
|
+
valuesToMixinBeforeCreate = Object.assign(Object.assign({}, valuesToMixinBeforeCreate), v);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return valuesToMixinBeforeCreate;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
handleDidCreateForPlugins(record) {
|
|
136
151
|
var _a;
|
|
137
152
|
return __awaiter(this, void 0, void 0, function* () {
|
|
138
153
|
let mixinValuesOnReturn = {};
|
|
139
154
|
for (const plugin of this.plugins) {
|
|
140
|
-
const r = yield ((_a = plugin.
|
|
155
|
+
const r = yield ((_a = plugin.didCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, record));
|
|
141
156
|
mixinValuesOnReturn = Object.assign(Object.assign({}, mixinValuesOnReturn), r === null || r === void 0 ? void 0 : r.valuesToMixinBeforeReturning);
|
|
142
157
|
}
|
|
143
158
|
return mixinValuesOnReturn;
|
|
@@ -40,15 +40,19 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
|
|
|
40
40
|
badDatabaseName: string;
|
|
41
41
|
}>;
|
|
42
42
|
export interface DataStorePlugin {
|
|
43
|
-
willCreateOne?: (values: Record<string, any>) => Promise<void |
|
|
43
|
+
willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
|
|
44
|
+
didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
|
|
44
45
|
willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
|
|
45
46
|
willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
|
|
46
47
|
didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
|
|
47
48
|
getName(): string;
|
|
48
49
|
}
|
|
49
|
-
export interface
|
|
50
|
+
export interface DataStorePluginDidCreateOneResponse {
|
|
50
51
|
valuesToMixinBeforeReturning?: Record<string, any>;
|
|
51
52
|
}
|
|
53
|
+
export interface DataStorePluginWillCreateOneResponse {
|
|
54
|
+
valuesToMixinBeforeCreate?: Record<string, any>;
|
|
55
|
+
}
|
|
52
56
|
export interface DataStorePluginWillUpdateOneResponse {
|
|
53
57
|
query?: Record<string, any>;
|
|
54
58
|
}
|
|
@@ -33,7 +33,8 @@ 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
|
|
36
|
+
private handleWillCreatePlugins;
|
|
37
|
+
private handleDidCreateForPlugins;
|
|
37
38
|
private get primaryFieldName();
|
|
38
39
|
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
40
|
private handleDidFindForPlugins;
|
|
@@ -118,6 +118,7 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
118
118
|
async createOne(values, options) {
|
|
119
119
|
var _a, _b;
|
|
120
120
|
try {
|
|
121
|
+
let valuesToMixinBeforeCreate = await this.handleWillCreatePlugins(values);
|
|
121
122
|
//@ts-ignore
|
|
122
123
|
(0, schema_1.validateSchemaValues)(this.createSchema, values);
|
|
123
124
|
const cleanedValues = this.willCreate
|
|
@@ -129,10 +130,10 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
129
130
|
const toSave = (0, schema_1.normalizeSchemaValues)(this.databaseSchema,
|
|
130
131
|
//@ts-ignore
|
|
131
132
|
databaseRecord, { shouldCreateEntityInstances: false });
|
|
132
|
-
const
|
|
133
|
-
const record = await this.db.createOne(this.collectionName, toSave);
|
|
133
|
+
const record = await this.db.createOne(this.collectionName, Object.assign(Object.assign({}, toSave), valuesToMixinBeforeCreate));
|
|
134
134
|
await ((_b = this.didCreate) === null || _b === void 0 ? void 0 : _b.call(this, record));
|
|
135
135
|
const normalized = await this.prepareAndNormalizeRecord(record, options);
|
|
136
|
+
const mixinValuesOnReturn = await this.handleDidCreateForPlugins(normalized);
|
|
136
137
|
return Object.assign(Object.assign({}, normalized), mixinValuesOnReturn);
|
|
137
138
|
}
|
|
138
139
|
catch (err) {
|
|
@@ -145,11 +146,23 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
145
146
|
throw coded[0];
|
|
146
147
|
}
|
|
147
148
|
}
|
|
148
|
-
async
|
|
149
|
+
async handleWillCreatePlugins(values) {
|
|
150
|
+
var _a;
|
|
151
|
+
let valuesToMixinBeforeCreate = {};
|
|
152
|
+
for (const plugin of this.plugins) {
|
|
153
|
+
const r = await ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, values));
|
|
154
|
+
const { valuesToMixinBeforeCreate: v } = r !== null && r !== void 0 ? r : {};
|
|
155
|
+
if (v) {
|
|
156
|
+
valuesToMixinBeforeCreate = Object.assign(Object.assign({}, valuesToMixinBeforeCreate), v);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return valuesToMixinBeforeCreate;
|
|
160
|
+
}
|
|
161
|
+
async handleDidCreateForPlugins(record) {
|
|
149
162
|
var _a;
|
|
150
163
|
let mixinValuesOnReturn = {};
|
|
151
164
|
for (const plugin of this.plugins) {
|
|
152
|
-
const r = await ((_a = plugin.
|
|
165
|
+
const r = await ((_a = plugin.didCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, record));
|
|
153
166
|
mixinValuesOnReturn = Object.assign(Object.assign({}, mixinValuesOnReturn), r === null || r === void 0 ? void 0 : r.valuesToMixinBeforeReturning);
|
|
154
167
|
}
|
|
155
168
|
return mixinValuesOnReturn;
|
|
@@ -40,15 +40,19 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
|
|
|
40
40
|
badDatabaseName: string;
|
|
41
41
|
}>;
|
|
42
42
|
export interface DataStorePlugin {
|
|
43
|
-
willCreateOne?: (values: Record<string, any>) => Promise<void |
|
|
43
|
+
willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
|
|
44
|
+
didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
|
|
44
45
|
willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
|
|
45
46
|
willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
|
|
46
47
|
didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
|
|
47
48
|
getName(): string;
|
|
48
49
|
}
|
|
49
|
-
export interface
|
|
50
|
+
export interface DataStorePluginDidCreateOneResponse {
|
|
50
51
|
valuesToMixinBeforeReturning?: Record<string, any>;
|
|
51
52
|
}
|
|
53
|
+
export interface DataStorePluginWillCreateOneResponse {
|
|
54
|
+
valuesToMixinBeforeCreate?: Record<string, any>;
|
|
55
|
+
}
|
|
52
56
|
export interface DataStorePluginWillUpdateOneResponse {
|
|
53
57
|
query?: Record<string, any>;
|
|
54
58
|
}
|