@sprucelabs/data-stores 25.13.14 → 26.0.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/databases/NeDbDatabase.d.ts +1 -0
- package/build/databases/NeDbDatabase.js +3 -0
- package/build/esm/databases/NeDbDatabase.d.ts +1 -0
- package/build/esm/databases/NeDbDatabase.js +3 -0
- package/build/esm/factories/StoreFactory.d.ts +0 -5
- package/build/esm/factories/StoreFactory.js +0 -5
- package/build/esm/stores/AbstractStore.d.ts +4 -3
- package/build/esm/stores/AbstractStore.js +24 -18
- package/build/esm/types/database.types.d.ts +1 -0
- package/build/factories/StoreFactory.d.ts +0 -5
- package/build/factories/StoreFactory.js +0 -5
- package/build/stores/AbstractStore.d.ts +4 -3
- package/build/stores/AbstractStore.js +23 -17
- package/build/types/database.types.d.ts +1 -0
- package/package.json +4 -4
|
@@ -10,6 +10,7 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
10
10
|
private toMongoId;
|
|
11
11
|
private prepQuery;
|
|
12
12
|
private valuesToDocument;
|
|
13
|
+
getShouldAutoGenerateId(): boolean;
|
|
13
14
|
setShouldAutoGenerateIds(collection: string, shouldAutoGenerateIds: boolean): Promise<void>;
|
|
14
15
|
private randomDelay;
|
|
15
16
|
isConnected(): boolean;
|
|
@@ -62,6 +62,9 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
62
62
|
const undefinedToPlaceholder = this.handlePlaceholders(nullsToPlaceholder, UNDEFINED_PLACEHOLDER, (val) => val === undefined || val === UNDEFINED_PLACEHOLDER);
|
|
63
63
|
return undefinedToPlaceholder;
|
|
64
64
|
}
|
|
65
|
+
getShouldAutoGenerateId() {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
65
68
|
async setShouldAutoGenerateIds(collection, shouldAutoGenerateIds) {
|
|
66
69
|
const col = this.loadCollection(collection);
|
|
67
70
|
//@ts-ignore
|
|
@@ -10,6 +10,7 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
10
10
|
private toMongoId;
|
|
11
11
|
private prepQuery;
|
|
12
12
|
private valuesToDocument;
|
|
13
|
+
getShouldAutoGenerateId(): boolean;
|
|
13
14
|
setShouldAutoGenerateIds(collection: string, shouldAutoGenerateIds: boolean): Promise<void>;
|
|
14
15
|
private randomDelay;
|
|
15
16
|
isConnected(): boolean;
|
|
@@ -70,6 +70,9 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
70
70
|
const undefinedToPlaceholder = this.handlePlaceholders(nullsToPlaceholder, UNDEFINED_PLACEHOLDER, (val) => val === undefined || val === UNDEFINED_PLACEHOLDER);
|
|
71
71
|
return undefinedToPlaceholder;
|
|
72
72
|
}
|
|
73
|
+
getShouldAutoGenerateId() {
|
|
74
|
+
return true;
|
|
75
|
+
}
|
|
73
76
|
setShouldAutoGenerateIds(collection, shouldAutoGenerateIds) {
|
|
74
77
|
return __awaiter(this, void 0, void 0, function* () {
|
|
75
78
|
const col = this.loadCollection(collection);
|
|
@@ -10,11 +10,6 @@ export default class StoreFactory {
|
|
|
10
10
|
private stores;
|
|
11
11
|
private constructor();
|
|
12
12
|
static Factory(db: Database): StoreFactory;
|
|
13
|
-
/**
|
|
14
|
-
* @deprecated stores.Store(..) -> stores.getStore(...)
|
|
15
|
-
* This change has big speed improvements. This factory method
|
|
16
|
-
* will never be removed, but hopefully won't be needed often.
|
|
17
|
-
*/
|
|
18
13
|
Store<Name extends StoreName, Options extends StoreOptions<Name>>(name: Name, options?: Options): Promise<StoreMap[Name]>;
|
|
19
14
|
getStoreNames(): StoreName[];
|
|
20
15
|
setStoreClass(name: string, Class: StoreContructor): void;
|
|
@@ -18,11 +18,6 @@ class StoreFactory {
|
|
|
18
18
|
static Factory(db) {
|
|
19
19
|
return new this(db);
|
|
20
20
|
}
|
|
21
|
-
/**
|
|
22
|
-
* @deprecated stores.Store(..) -> stores.getStore(...)
|
|
23
|
-
* This change has big speed improvements. This factory method
|
|
24
|
-
* will never be removed, but hopefully won't be needed often.
|
|
25
|
-
*/
|
|
26
21
|
Store(name, options) {
|
|
27
22
|
var _a;
|
|
28
23
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -4,7 +4,7 @@ import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
|
4
4
|
import { Database } from '../types/database.types';
|
|
5
5
|
import { QueryBuilder, QueryOptions } from '../types/query.types';
|
|
6
6
|
import { PrepareOptions, PrepareResults, SaveOperations, DataStore, DataStorePlugin } 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>, 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, PrimaryFieldName extends SchemaFieldNames<DatabaseSchema> | 'id' = 'id', DatabaseRecord = SchemaValues<DatabaseSchema>, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
|
|
8
8
|
abstract readonly name: string;
|
|
9
9
|
protected abstract collectionName: string;
|
|
10
10
|
protected abstract createSchema: CreateSchema;
|
|
@@ -13,12 +13,12 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
13
13
|
protected abstract databaseSchema: DatabaseSchema;
|
|
14
14
|
protected scrambleFields?: string[];
|
|
15
15
|
protected db: Database;
|
|
16
|
-
protected primaryFieldNames:
|
|
16
|
+
protected primaryFieldNames: PrimaryFieldName[];
|
|
17
17
|
protected shouldMapLowerCaseToCamelCase: boolean;
|
|
18
18
|
protected plugins: DataStorePlugin[];
|
|
19
19
|
initialize?(): Promise<void>;
|
|
20
20
|
protected prepareRecord?<IncludePrivateFields extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(record: DatabaseRecord, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<PrepareResults<FullSchema, IncludePrivateFields>>;
|
|
21
|
-
protected willCreate?(values: CreateRecord): Promise<Omit<DatabaseRecord,
|
|
21
|
+
protected willCreate?(values: CreateRecord): Promise<Omit<DatabaseRecord, PrimaryFieldName>>;
|
|
22
22
|
protected didCreate?(values: CreateRecord): Promise<void>;
|
|
23
23
|
protected willUpdate?(updates: UpdateRecord, record: DatabaseRecord): Promise<Partial<DatabaseRecord>>;
|
|
24
24
|
protected didUpdate?(old: DatabaseRecord, updated: DatabaseRecord): Promise<void>;
|
|
@@ -33,6 +33,7 @@ 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 normalizeBeforeSave;
|
|
36
37
|
private handleWillCreatePlugins;
|
|
37
38
|
private handleDidCreateForPlugins;
|
|
38
39
|
private get primaryFieldName();
|
|
@@ -18,7 +18,7 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
18
18
|
}
|
|
19
19
|
return t;
|
|
20
20
|
};
|
|
21
|
-
import SchemaEntity, { normalizeSchemaValues, validateSchemaValues, } from '@sprucelabs/schema';
|
|
21
|
+
import SchemaEntity, { normalizeSchemaValues, validateSchemaValues, dropFields, } from '@sprucelabs/schema';
|
|
22
22
|
import { SCRAMBLE_VALUE } from '../constants.js';
|
|
23
23
|
import BatchCursorImpl from '../cursors/BatchCursor.js';
|
|
24
24
|
import SpruceError from '../errors/SpruceError.js';
|
|
@@ -77,15 +77,7 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
77
77
|
//@ts-ignore
|
|
78
78
|
values.forEach((v) => validateSchemaValues(this.createSchema, v));
|
|
79
79
|
const cleanedValues = yield Promise.all(values.map((v) => __awaiter(this, void 0, void 0, function* () { return (this.willCreate ? this.willCreate(v) : v); })));
|
|
80
|
-
const
|
|
81
|
-
var _a;
|
|
82
|
-
return (Object.assign(Object.assign({}, v), { [this.primaryFieldName]:
|
|
83
|
-
//@ts-ignore
|
|
84
|
-
(_a = v[this.primaryFieldName]) !== null && _a !== void 0 ? _a : this.db.generateId() }));
|
|
85
|
-
});
|
|
86
|
-
const toSave = databaseRecords.map((r) => normalizeSchemaValues(this.databaseSchema,
|
|
87
|
-
//@ts-ignore
|
|
88
|
-
r, { shouldCreateEntityInstances: false }));
|
|
80
|
+
const toSave = cleanedValues.map((v) => this.normalizeBeforeSave(v));
|
|
89
81
|
const records = yield this.db.create(this.collectionName, toSave);
|
|
90
82
|
return Promise.all(records.map((record) => __awaiter(this, void 0, void 0, function* () { return this.prepareAndNormalizeRecord(record, options); })));
|
|
91
83
|
}
|
|
@@ -101,7 +93,7 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
101
93
|
});
|
|
102
94
|
}
|
|
103
95
|
createOne(values, options) {
|
|
104
|
-
var _a
|
|
96
|
+
var _a;
|
|
105
97
|
return __awaiter(this, void 0, void 0, function* () {
|
|
106
98
|
try {
|
|
107
99
|
let valuesToMixinBeforeCreate = yield this.handleWillCreatePlugins(values);
|
|
@@ -110,14 +102,9 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
110
102
|
const cleanedValues = this.willCreate
|
|
111
103
|
? yield this.willCreate(values)
|
|
112
104
|
: values;
|
|
113
|
-
const
|
|
114
|
-
//@ts-ignore
|
|
115
|
-
(_a = cleanedValues[this.primaryFieldName]) !== null && _a !== void 0 ? _a : this.db.generateId() });
|
|
116
|
-
const toSave = normalizeSchemaValues(this.databaseSchema,
|
|
117
|
-
//@ts-ignore
|
|
118
|
-
databaseRecord, { shouldCreateEntityInstances: false });
|
|
105
|
+
const toSave = this.normalizeBeforeSave(cleanedValues);
|
|
119
106
|
const record = yield this.db.createOne(this.collectionName, Object.assign(Object.assign({}, toSave), valuesToMixinBeforeCreate));
|
|
120
|
-
yield ((
|
|
107
|
+
yield ((_a = this.didCreate) === null || _a === void 0 ? void 0 : _a.call(this, record));
|
|
121
108
|
const normalized = yield this.prepareAndNormalizeRecord(record, options);
|
|
122
109
|
const mixinValuesOnReturn = yield this.handleDidCreateForPlugins(normalized);
|
|
123
110
|
return Object.assign(Object.assign({}, normalized), mixinValuesOnReturn);
|
|
@@ -133,6 +120,25 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
133
120
|
}
|
|
134
121
|
});
|
|
135
122
|
}
|
|
123
|
+
normalizeBeforeSave(cleanedValues) {
|
|
124
|
+
var _a, _b, _c, _d;
|
|
125
|
+
const shouldAutoGenerateId = (_b = (_a = this.db).getShouldAutoGenerateId) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
126
|
+
const databaseRecord = !shouldAutoGenerateId
|
|
127
|
+
? cleanedValues
|
|
128
|
+
: Object.assign(Object.assign({}, cleanedValues), { [this.primaryFieldName]:
|
|
129
|
+
//@ts-ignore
|
|
130
|
+
(_c = cleanedValues[this.primaryFieldName]) !== null && _c !== void 0 ? _c : this.db.generateId() });
|
|
131
|
+
const fields = shouldAutoGenerateId
|
|
132
|
+
? undefined
|
|
133
|
+
: Object.keys(dropFields((_d = this.databaseSchema.fields) !== null && _d !== void 0 ? _d : {}, this.primaryFieldNames));
|
|
134
|
+
const toSave = normalizeSchemaValues(this.databaseSchema,
|
|
135
|
+
//@ts-ignore
|
|
136
|
+
databaseRecord, {
|
|
137
|
+
shouldCreateEntityInstances: false,
|
|
138
|
+
fields,
|
|
139
|
+
});
|
|
140
|
+
return toSave;
|
|
141
|
+
}
|
|
136
142
|
handleWillCreatePlugins(values) {
|
|
137
143
|
var _a;
|
|
138
144
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -13,6 +13,7 @@ export interface Database {
|
|
|
13
13
|
generateId(): string;
|
|
14
14
|
connect(): Promise<void>;
|
|
15
15
|
close(): Promise<void>;
|
|
16
|
+
getShouldAutoGenerateId?(): boolean;
|
|
16
17
|
createOne(collection: string, values: Record<string, any>): Promise<Record<string, any>>;
|
|
17
18
|
create(collection: string, values: Record<string, any>[]): Promise<Record<string, any>[]>;
|
|
18
19
|
dropCollection(name: string): Promise<void>;
|
|
@@ -10,11 +10,6 @@ export default class StoreFactory {
|
|
|
10
10
|
private stores;
|
|
11
11
|
private constructor();
|
|
12
12
|
static Factory(db: Database): StoreFactory;
|
|
13
|
-
/**
|
|
14
|
-
* @deprecated stores.Store(..) -> stores.getStore(...)
|
|
15
|
-
* This change has big speed improvements. This factory method
|
|
16
|
-
* will never be removed, but hopefully won't be needed often.
|
|
17
|
-
*/
|
|
18
13
|
Store<Name extends StoreName, Options extends StoreOptions<Name>>(name: Name, options?: Options): Promise<StoreMap[Name]>;
|
|
19
14
|
getStoreNames(): StoreName[];
|
|
20
15
|
setStoreClass(name: string, Class: StoreContructor): void;
|
|
@@ -14,11 +14,6 @@ class StoreFactory {
|
|
|
14
14
|
static Factory(db) {
|
|
15
15
|
return new this(db);
|
|
16
16
|
}
|
|
17
|
-
/**
|
|
18
|
-
* @deprecated stores.Store(..) -> stores.getStore(...)
|
|
19
|
-
* This change has big speed improvements. This factory method
|
|
20
|
-
* will never be removed, but hopefully won't be needed often.
|
|
21
|
-
*/
|
|
22
17
|
async Store(name, options) {
|
|
23
18
|
var _a;
|
|
24
19
|
const Store = this.storeMap[name];
|
|
@@ -4,7 +4,7 @@ import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
|
4
4
|
import { Database } from '../types/database.types';
|
|
5
5
|
import { QueryBuilder, QueryOptions } from '../types/query.types';
|
|
6
6
|
import { PrepareOptions, PrepareResults, SaveOperations, DataStore, DataStorePlugin } 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>, 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, PrimaryFieldName extends SchemaFieldNames<DatabaseSchema> | 'id' = 'id', DatabaseRecord = SchemaValues<DatabaseSchema>, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
|
|
8
8
|
abstract readonly name: string;
|
|
9
9
|
protected abstract collectionName: string;
|
|
10
10
|
protected abstract createSchema: CreateSchema;
|
|
@@ -13,12 +13,12 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
|
|
|
13
13
|
protected abstract databaseSchema: DatabaseSchema;
|
|
14
14
|
protected scrambleFields?: string[];
|
|
15
15
|
protected db: Database;
|
|
16
|
-
protected primaryFieldNames:
|
|
16
|
+
protected primaryFieldNames: PrimaryFieldName[];
|
|
17
17
|
protected shouldMapLowerCaseToCamelCase: boolean;
|
|
18
18
|
protected plugins: DataStorePlugin[];
|
|
19
19
|
initialize?(): Promise<void>;
|
|
20
20
|
protected prepareRecord?<IncludePrivateFields extends boolean, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>>(record: DatabaseRecord, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<PrepareResults<FullSchema, IncludePrivateFields>>;
|
|
21
|
-
protected willCreate?(values: CreateRecord): Promise<Omit<DatabaseRecord,
|
|
21
|
+
protected willCreate?(values: CreateRecord): Promise<Omit<DatabaseRecord, PrimaryFieldName>>;
|
|
22
22
|
protected didCreate?(values: CreateRecord): Promise<void>;
|
|
23
23
|
protected willUpdate?(updates: UpdateRecord, record: DatabaseRecord): Promise<Partial<DatabaseRecord>>;
|
|
24
24
|
protected didUpdate?(old: DatabaseRecord, updated: DatabaseRecord): Promise<void>;
|
|
@@ -33,6 +33,7 @@ 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 normalizeBeforeSave;
|
|
36
37
|
private handleWillCreatePlugins;
|
|
37
38
|
private handleDidCreateForPlugins;
|
|
38
39
|
private get primaryFieldName();
|
|
@@ -93,15 +93,7 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
93
93
|
//@ts-ignore
|
|
94
94
|
values.forEach((v) => (0, schema_1.validateSchemaValues)(this.createSchema, v));
|
|
95
95
|
const cleanedValues = await Promise.all(values.map(async (v) => (this.willCreate ? this.willCreate(v) : v)));
|
|
96
|
-
const
|
|
97
|
-
var _a;
|
|
98
|
-
return (Object.assign(Object.assign({}, v), { [this.primaryFieldName]:
|
|
99
|
-
//@ts-ignore
|
|
100
|
-
(_a = v[this.primaryFieldName]) !== null && _a !== void 0 ? _a : this.db.generateId() }));
|
|
101
|
-
});
|
|
102
|
-
const toSave = databaseRecords.map((r) => (0, schema_1.normalizeSchemaValues)(this.databaseSchema,
|
|
103
|
-
//@ts-ignore
|
|
104
|
-
r, { shouldCreateEntityInstances: false }));
|
|
96
|
+
const toSave = cleanedValues.map((v) => this.normalizeBeforeSave(v));
|
|
105
97
|
const records = await this.db.create(this.collectionName, toSave);
|
|
106
98
|
return Promise.all(records.map(async (record) => this.prepareAndNormalizeRecord(record, options)));
|
|
107
99
|
}
|
|
@@ -116,7 +108,7 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
116
108
|
}
|
|
117
109
|
}
|
|
118
110
|
async createOne(values, options) {
|
|
119
|
-
var _a
|
|
111
|
+
var _a;
|
|
120
112
|
try {
|
|
121
113
|
let valuesToMixinBeforeCreate = await this.handleWillCreatePlugins(values);
|
|
122
114
|
//@ts-ignore
|
|
@@ -124,14 +116,9 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
124
116
|
const cleanedValues = this.willCreate
|
|
125
117
|
? await this.willCreate(values)
|
|
126
118
|
: values;
|
|
127
|
-
const
|
|
128
|
-
//@ts-ignore
|
|
129
|
-
(_a = cleanedValues[this.primaryFieldName]) !== null && _a !== void 0 ? _a : this.db.generateId() });
|
|
130
|
-
const toSave = (0, schema_1.normalizeSchemaValues)(this.databaseSchema,
|
|
131
|
-
//@ts-ignore
|
|
132
|
-
databaseRecord, { shouldCreateEntityInstances: false });
|
|
119
|
+
const toSave = this.normalizeBeforeSave(cleanedValues);
|
|
133
120
|
const record = await this.db.createOne(this.collectionName, Object.assign(Object.assign({}, toSave), valuesToMixinBeforeCreate));
|
|
134
|
-
await ((
|
|
121
|
+
await ((_a = this.didCreate) === null || _a === void 0 ? void 0 : _a.call(this, record));
|
|
135
122
|
const normalized = await this.prepareAndNormalizeRecord(record, options);
|
|
136
123
|
const mixinValuesOnReturn = await this.handleDidCreateForPlugins(normalized);
|
|
137
124
|
return Object.assign(Object.assign({}, normalized), mixinValuesOnReturn);
|
|
@@ -146,6 +133,25 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
146
133
|
throw coded[0];
|
|
147
134
|
}
|
|
148
135
|
}
|
|
136
|
+
normalizeBeforeSave(cleanedValues) {
|
|
137
|
+
var _a, _b, _c, _d;
|
|
138
|
+
const shouldAutoGenerateId = (_b = (_a = this.db).getShouldAutoGenerateId) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
139
|
+
const databaseRecord = !shouldAutoGenerateId
|
|
140
|
+
? cleanedValues
|
|
141
|
+
: Object.assign(Object.assign({}, cleanedValues), { [this.primaryFieldName]:
|
|
142
|
+
//@ts-ignore
|
|
143
|
+
(_c = cleanedValues[this.primaryFieldName]) !== null && _c !== void 0 ? _c : this.db.generateId() });
|
|
144
|
+
const fields = shouldAutoGenerateId
|
|
145
|
+
? undefined
|
|
146
|
+
: Object.keys((0, schema_1.dropFields)((_d = this.databaseSchema.fields) !== null && _d !== void 0 ? _d : {}, this.primaryFieldNames));
|
|
147
|
+
const toSave = (0, schema_1.normalizeSchemaValues)(this.databaseSchema,
|
|
148
|
+
//@ts-ignore
|
|
149
|
+
databaseRecord, {
|
|
150
|
+
shouldCreateEntityInstances: false,
|
|
151
|
+
fields,
|
|
152
|
+
});
|
|
153
|
+
return toSave;
|
|
154
|
+
}
|
|
149
155
|
async handleWillCreatePlugins(values) {
|
|
150
156
|
var _a;
|
|
151
157
|
let valuesToMixinBeforeCreate = {};
|
|
@@ -13,6 +13,7 @@ export interface Database {
|
|
|
13
13
|
generateId(): string;
|
|
14
14
|
connect(): Promise<void>;
|
|
15
15
|
close(): Promise<void>;
|
|
16
|
+
getShouldAutoGenerateId?(): boolean;
|
|
16
17
|
createOne(collection: string, values: Record<string, any>): Promise<Record<string, any>>;
|
|
17
18
|
create(collection: string, values: Record<string, any>[]): Promise<Record<string, any>[]>;
|
|
18
19
|
dropCollection(name: string): Promise<void>;
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "
|
|
6
|
+
"version": "26.0.1",
|
|
7
7
|
"files": [
|
|
8
8
|
"build/**/*",
|
|
9
9
|
"!build/__tests__",
|
|
@@ -78,7 +78,7 @@
|
|
|
78
78
|
"nedb": "^1.8.0"
|
|
79
79
|
},
|
|
80
80
|
"devDependencies": {
|
|
81
|
-
"@sprucelabs/esm-postbuild": "^5.0.
|
|
81
|
+
"@sprucelabs/esm-postbuild": "^5.0.103",
|
|
82
82
|
"@sprucelabs/jest-json-reporter": "^7.0.128",
|
|
83
83
|
"@sprucelabs/jest-sheets-reporter": "^3.0.26",
|
|
84
84
|
"@sprucelabs/resolve-path-aliases": "^1.1.266",
|
|
@@ -87,11 +87,11 @@
|
|
|
87
87
|
"@sprucelabs/test-utils": "^4.0.81",
|
|
88
88
|
"@types/lodash": "^4.14.202",
|
|
89
89
|
"@types/nedb": "^1.8.16",
|
|
90
|
-
"@types/node": "^20.10.
|
|
90
|
+
"@types/node": "^20.10.3",
|
|
91
91
|
"chokidar-cli": "^3.0.0",
|
|
92
92
|
"concurrently": "^8.2.2",
|
|
93
93
|
"dotenv": "^16.3.1",
|
|
94
|
-
"eslint": "^8.
|
|
94
|
+
"eslint": "^8.55.0",
|
|
95
95
|
"eslint-config-spruce": "^10.13.6",
|
|
96
96
|
"jest": "^29.7.0",
|
|
97
97
|
"jest-circus": "^29.7.0",
|