@sprucelabs/data-stores 29.0.44 → 30.0.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/plugins/DatabaseFieldMapperPlugin.d.ts +2 -2
- package/build/esm/plugins/DatabaseFieldMapperPlugin.js +1 -1
- package/build/esm/stores/AbstractStore.d.ts +1 -1
- package/build/esm/stores/AbstractStore.js +7 -6
- package/build/esm/tests/AbstractDatabaseTest.d.ts +8 -8
- package/build/esm/tests/AbstractDatabaseTest.js +10 -8
- package/build/esm/types/stores.types.d.ts +2 -2
- package/build/plugins/DatabaseFieldMapperPlugin.d.ts +2 -2
- package/build/plugins/DatabaseFieldMapperPlugin.js +1 -1
- package/build/stores/AbstractStore.d.ts +1 -1
- package/build/stores/AbstractStore.js +7 -6
- package/build/tests/AbstractDatabaseTest.d.ts +8 -8
- package/build/tests/AbstractDatabaseTest.js +9 -6
- package/build/types/stores.types.d.ts +2 -2
- package/package.json +3 -3
@@ -1,11 +1,11 @@
|
|
1
1
|
import { FullQueryOptions } from '../stores/AbstractStore';
|
2
|
-
import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse,
|
2
|
+
import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse, DataStorePluginWillUpdateResponse } 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
|
-
|
9
|
+
willUpdate(query: Record<string, any>, updates: Record<string, any>): Promise<void | DataStorePluginWillUpdateResponse>;
|
10
10
|
willFind(query: Record<string, any>, options?: FullQueryOptions): Promise<void | DataStorePluginWillFindResponse>;
|
11
11
|
}
|
@@ -52,7 +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: ValuesWithPaths<UpdateRecord>, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
|
53
53
|
update(query: QueryBuilder<QueryRecord>, updates: ValuesWithPaths<UpdateRecord>): Promise<number>;
|
54
54
|
private findOneAndUpdate;
|
55
|
-
private
|
55
|
+
private handleWillUpdatePlugins;
|
56
56
|
private pluckOperations;
|
57
57
|
scramble(id: string): Promise<SchemaValues<FullSchema, false, false, false, Extract<keyof FullSchema["fields"], string>, SchemaPublicFieldNames<FullSchema>>>;
|
58
58
|
private isScrambled;
|
@@ -315,7 +315,8 @@ export default class AbstractStore extends AbstractMutexer {
|
|
315
315
|
}
|
316
316
|
update(query, updates) {
|
317
317
|
return __awaiter(this, void 0, void 0, function* () {
|
318
|
-
|
318
|
+
const { updates: pUpdates, query: pQuery } = yield this.handleWillUpdatePlugins(query, updates);
|
319
|
+
return this.db.update(this.collectionName, pQuery, pUpdates);
|
319
320
|
});
|
320
321
|
}
|
321
322
|
findOneAndUpdate(query_1, updates_1, notFoundHandler_1) {
|
@@ -329,9 +330,9 @@ export default class AbstractStore extends AbstractMutexer {
|
|
329
330
|
//@ts-ignore
|
330
331
|
validateSchemaValues(this.updateSchema, initialUpdates);
|
331
332
|
}
|
332
|
-
const { query:
|
333
|
-
q =
|
334
|
-
initialUpdates =
|
333
|
+
const { query: pQuery, shouldUpdate, updates: pUpdates, } = yield this.handleWillUpdatePlugins(q, initialUpdates);
|
334
|
+
q = pQuery;
|
335
|
+
initialUpdates = pUpdates;
|
335
336
|
let current = yield this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
|
336
337
|
if (!current) {
|
337
338
|
current = yield notFoundHandler();
|
@@ -376,14 +377,14 @@ export default class AbstractStore extends AbstractMutexer {
|
|
376
377
|
}
|
377
378
|
});
|
378
379
|
}
|
379
|
-
|
380
|
+
handleWillUpdatePlugins(query, updates) {
|
380
381
|
return __awaiter(this, void 0, void 0, function* () {
|
381
382
|
var _a;
|
382
383
|
let shouldUpdate = true;
|
383
384
|
let resolvedQuery = query;
|
384
385
|
let resolvedUpdates = updates;
|
385
386
|
for (const plugin of this.plugins) {
|
386
|
-
const results = yield ((_a = plugin.
|
387
|
+
const results = yield ((_a = plugin.willUpdate) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
|
387
388
|
if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
|
388
389
|
shouldUpdate = false;
|
389
390
|
}
|
@@ -2,14 +2,14 @@ import AbstractSpruceTest from '@sprucelabs/test-utils';
|
|
2
2
|
import DatabaseFixture, { DatabaseFixtureOptions } from '../fixtures/DatabaseFixture';
|
3
3
|
import { Database } from '../types/database.types';
|
4
4
|
export default class AbstractDatabaseTest extends AbstractSpruceTest {
|
5
|
-
protected
|
6
|
-
protected
|
7
|
-
protected
|
8
|
-
protected
|
9
|
-
protected
|
10
|
-
protected
|
11
|
-
protected
|
12
|
-
protected
|
5
|
+
protected db: Database;
|
6
|
+
protected shouldUseInMemoryDatabase: boolean;
|
7
|
+
protected DB_NAME: string;
|
8
|
+
protected beforeEach(): Promise<void>;
|
9
|
+
protected afterEach(): Promise<void>;
|
10
|
+
protected DatabaseFixture(options?: DatabaseFixtureOptions): Promise<DatabaseFixture>;
|
11
|
+
protected connectToDatabase(): Promise<Database>;
|
12
|
+
protected DatabaseConnection(): Promise<{
|
13
13
|
dbFixture: DatabaseFixture;
|
14
14
|
db: Database;
|
15
15
|
}>;
|
@@ -10,8 +10,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
10
10
|
import { SchemaRegistry } from '@sprucelabs/schema';
|
11
11
|
import AbstractSpruceTest from '@sprucelabs/test-utils';
|
12
12
|
import DatabaseFixture from '../fixtures/DatabaseFixture.js';
|
13
|
-
class AbstractDatabaseTest extends AbstractSpruceTest {
|
14
|
-
|
13
|
+
export default class AbstractDatabaseTest extends AbstractSpruceTest {
|
14
|
+
constructor() {
|
15
|
+
super(...arguments);
|
16
|
+
this.shouldUseInMemoryDatabase = true;
|
17
|
+
}
|
18
|
+
beforeEach() {
|
15
19
|
const _super = Object.create(null, {
|
16
20
|
beforeEach: { get: () => super.beforeEach }
|
17
21
|
});
|
@@ -20,7 +24,7 @@ class AbstractDatabaseTest extends AbstractSpruceTest {
|
|
20
24
|
SchemaRegistry.getInstance().forgetAllSchemas();
|
21
25
|
});
|
22
26
|
}
|
23
|
-
|
27
|
+
afterEach() {
|
24
28
|
const _super = Object.create(null, {
|
25
29
|
afterEach: { get: () => super.afterEach }
|
26
30
|
});
|
@@ -33,13 +37,13 @@ class AbstractDatabaseTest extends AbstractSpruceTest {
|
|
33
37
|
this.DB_NAME = undefined;
|
34
38
|
});
|
35
39
|
}
|
36
|
-
|
40
|
+
DatabaseFixture(options) {
|
37
41
|
return __awaiter(this, void 0, void 0, function* () {
|
38
42
|
const d = new DatabaseFixture(Object.assign({}, options));
|
39
43
|
return d;
|
40
44
|
});
|
41
45
|
}
|
42
|
-
|
46
|
+
connectToDatabase() {
|
43
47
|
return __awaiter(this, void 0, void 0, function* () {
|
44
48
|
if (!this.db) {
|
45
49
|
const { dbFixture, db } = yield this.DatabaseConnection();
|
@@ -51,7 +55,7 @@ class AbstractDatabaseTest extends AbstractSpruceTest {
|
|
51
55
|
return this.db;
|
52
56
|
});
|
53
57
|
}
|
54
|
-
|
58
|
+
DatabaseConnection() {
|
55
59
|
return __awaiter(this, void 0, void 0, function* () {
|
56
60
|
const dbFixture = yield this.DatabaseFixture();
|
57
61
|
const db = yield dbFixture.connectToDatabase();
|
@@ -59,5 +63,3 @@ class AbstractDatabaseTest extends AbstractSpruceTest {
|
|
59
63
|
});
|
60
64
|
}
|
61
65
|
}
|
62
|
-
AbstractDatabaseTest.shouldUseInMemoryDatabase = true;
|
63
|
-
export default AbstractDatabaseTest;
|
@@ -35,7 +35,7 @@ export type StoreOptions<Name extends StoreName> = Name extends keyof StoreOptio
|
|
35
35
|
export interface DataStorePlugin {
|
36
36
|
willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
|
37
37
|
didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
|
38
|
-
|
38
|
+
willUpdate?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateResponse>;
|
39
39
|
willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
|
40
40
|
didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
|
41
41
|
getName(): string;
|
@@ -52,7 +52,7 @@ export interface DataStorePluginWillCreateOneResponse {
|
|
52
52
|
valuesToMixinBeforeCreate?: Record<string, any>;
|
53
53
|
newValues?: Record<string, any>;
|
54
54
|
}
|
55
|
-
export interface
|
55
|
+
export interface DataStorePluginWillUpdateResponse {
|
56
56
|
query?: Record<string, any>;
|
57
57
|
shouldUpdate?: boolean;
|
58
58
|
newUpdates?: Record<string, any>;
|
@@ -1,11 +1,11 @@
|
|
1
1
|
import { FullQueryOptions } from '../stores/AbstractStore';
|
2
|
-
import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse,
|
2
|
+
import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse, DataStorePluginWillUpdateResponse } 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
|
-
|
9
|
+
willUpdate(query: Record<string, any>, updates: Record<string, any>): Promise<void | DataStorePluginWillUpdateResponse>;
|
10
10
|
willFind(query: Record<string, any>, options?: FullQueryOptions): Promise<void | DataStorePluginWillFindResponse>;
|
11
11
|
}
|
@@ -52,7 +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: ValuesWithPaths<UpdateRecord>, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
|
53
53
|
update(query: QueryBuilder<QueryRecord>, updates: ValuesWithPaths<UpdateRecord>): Promise<number>;
|
54
54
|
private findOneAndUpdate;
|
55
|
-
private
|
55
|
+
private handleWillUpdatePlugins;
|
56
56
|
private pluckOperations;
|
57
57
|
scramble(id: string): Promise<SchemaValues<FullSchema, false, false, false, Extract<keyof FullSchema["fields"], string>, SchemaPublicFieldNames<FullSchema>>>;
|
58
58
|
private isScrambled;
|
@@ -313,7 +313,8 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
313
313
|
return this.findOneAndUpdate(query, updates, notFoundHandler, options);
|
314
314
|
}
|
315
315
|
async update(query, updates) {
|
316
|
-
|
316
|
+
const { updates: pUpdates, query: pQuery } = await this.handleWillUpdatePlugins(query, updates);
|
317
|
+
return this.db.update(this.collectionName, pQuery, pUpdates);
|
317
318
|
}
|
318
319
|
async findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
|
319
320
|
var _a, _b, _c;
|
@@ -325,9 +326,9 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
325
326
|
//@ts-ignore
|
326
327
|
(0, schema_1.validateSchemaValues)(this.updateSchema, initialUpdates);
|
327
328
|
}
|
328
|
-
const { query:
|
329
|
-
q =
|
330
|
-
initialUpdates =
|
329
|
+
const { query: pQuery, shouldUpdate, updates: pUpdates, } = await this.handleWillUpdatePlugins(q, initialUpdates);
|
330
|
+
q = pQuery;
|
331
|
+
initialUpdates = pUpdates;
|
331
332
|
let current = await this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
|
332
333
|
if (!current) {
|
333
334
|
current = await notFoundHandler();
|
@@ -371,13 +372,13 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
371
372
|
throw coded[0];
|
372
373
|
}
|
373
374
|
}
|
374
|
-
async
|
375
|
+
async handleWillUpdatePlugins(query, updates) {
|
375
376
|
var _a;
|
376
377
|
let shouldUpdate = true;
|
377
378
|
let resolvedQuery = query;
|
378
379
|
let resolvedUpdates = updates;
|
379
380
|
for (const plugin of this.plugins) {
|
380
|
-
const results = await ((_a = plugin.
|
381
|
+
const results = await ((_a = plugin.willUpdate) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
|
381
382
|
if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
|
382
383
|
shouldUpdate = false;
|
383
384
|
}
|
@@ -2,14 +2,14 @@ import AbstractSpruceTest from '@sprucelabs/test-utils';
|
|
2
2
|
import DatabaseFixture, { DatabaseFixtureOptions } from '../fixtures/DatabaseFixture';
|
3
3
|
import { Database } from '../types/database.types';
|
4
4
|
export default class AbstractDatabaseTest extends AbstractSpruceTest {
|
5
|
-
protected
|
6
|
-
protected
|
7
|
-
protected
|
8
|
-
protected
|
9
|
-
protected
|
10
|
-
protected
|
11
|
-
protected
|
12
|
-
protected
|
5
|
+
protected db: Database;
|
6
|
+
protected shouldUseInMemoryDatabase: boolean;
|
7
|
+
protected DB_NAME: string;
|
8
|
+
protected beforeEach(): Promise<void>;
|
9
|
+
protected afterEach(): Promise<void>;
|
10
|
+
protected DatabaseFixture(options?: DatabaseFixtureOptions): Promise<DatabaseFixture>;
|
11
|
+
protected connectToDatabase(): Promise<Database>;
|
12
|
+
protected DatabaseConnection(): Promise<{
|
13
13
|
dbFixture: DatabaseFixture;
|
14
14
|
db: Database;
|
15
15
|
}>;
|
@@ -7,11 +7,15 @@ const schema_1 = require("@sprucelabs/schema");
|
|
7
7
|
const test_utils_1 = __importDefault(require("@sprucelabs/test-utils"));
|
8
8
|
const DatabaseFixture_1 = __importDefault(require("../fixtures/DatabaseFixture"));
|
9
9
|
class AbstractDatabaseTest extends test_utils_1.default {
|
10
|
-
|
10
|
+
constructor() {
|
11
|
+
super(...arguments);
|
12
|
+
this.shouldUseInMemoryDatabase = true;
|
13
|
+
}
|
14
|
+
async beforeEach() {
|
11
15
|
await super.beforeEach();
|
12
16
|
schema_1.SchemaRegistry.getInstance().forgetAllSchemas();
|
13
17
|
}
|
14
|
-
|
18
|
+
async afterEach() {
|
15
19
|
await super.afterEach();
|
16
20
|
await DatabaseFixture_1.default.destroy();
|
17
21
|
//@ts-ignore
|
@@ -19,11 +23,11 @@ class AbstractDatabaseTest extends test_utils_1.default {
|
|
19
23
|
//@ts-ignore
|
20
24
|
this.DB_NAME = undefined;
|
21
25
|
}
|
22
|
-
|
26
|
+
async DatabaseFixture(options) {
|
23
27
|
const d = new DatabaseFixture_1.default(Object.assign({}, options));
|
24
28
|
return d;
|
25
29
|
}
|
26
|
-
|
30
|
+
async connectToDatabase() {
|
27
31
|
if (!this.db) {
|
28
32
|
const { dbFixture, db } = await this.DatabaseConnection();
|
29
33
|
this.DB_NAME = this.shouldUseInMemoryDatabase
|
@@ -33,11 +37,10 @@ class AbstractDatabaseTest extends test_utils_1.default {
|
|
33
37
|
}
|
34
38
|
return this.db;
|
35
39
|
}
|
36
|
-
|
40
|
+
async DatabaseConnection() {
|
37
41
|
const dbFixture = await this.DatabaseFixture();
|
38
42
|
const db = await dbFixture.connectToDatabase();
|
39
43
|
return { dbFixture, db };
|
40
44
|
}
|
41
45
|
}
|
42
|
-
AbstractDatabaseTest.shouldUseInMemoryDatabase = true;
|
43
46
|
exports.default = AbstractDatabaseTest;
|
@@ -35,7 +35,7 @@ export type StoreOptions<Name extends StoreName> = Name extends keyof StoreOptio
|
|
35
35
|
export interface DataStorePlugin {
|
36
36
|
willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
|
37
37
|
didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
|
38
|
-
|
38
|
+
willUpdate?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateResponse>;
|
39
39
|
willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
|
40
40
|
didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
|
41
41
|
getName(): string;
|
@@ -52,7 +52,7 @@ export interface DataStorePluginWillCreateOneResponse {
|
|
52
52
|
valuesToMixinBeforeCreate?: Record<string, any>;
|
53
53
|
newValues?: Record<string, any>;
|
54
54
|
}
|
55
|
-
export interface
|
55
|
+
export interface DataStorePluginWillUpdateResponse {
|
56
56
|
query?: Record<string, any>;
|
57
57
|
shouldUpdate?: boolean;
|
58
58
|
newUpdates?: Record<string, any>;
|
package/package.json
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
"publishConfig": {
|
4
4
|
"access": "public"
|
5
5
|
},
|
6
|
-
"version": "
|
6
|
+
"version": "30.0.0",
|
7
7
|
"files": [
|
8
8
|
"build/**/*",
|
9
9
|
"!build/__tests__",
|
@@ -66,8 +66,8 @@
|
|
66
66
|
"@seald-io/nedb": "^4.1.2",
|
67
67
|
"@sprucelabs/error": "^7.0.25",
|
68
68
|
"@sprucelabs/globby": "^2.0.506",
|
69
|
-
"@sprucelabs/schema": "^32.0.
|
70
|
-
"@sprucelabs/spruce-skill-utils": "^32.0.
|
69
|
+
"@sprucelabs/schema": "^32.0.41",
|
70
|
+
"@sprucelabs/spruce-skill-utils": "^32.0.42",
|
71
71
|
"just-clone": "^6.2.0",
|
72
72
|
"lodash": "^4.17.21",
|
73
73
|
"mongodb": "^6.17.0"
|