@sprucelabs/data-stores 26.2.2 → 26.2.4
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 +3 -3
- package/build/databases/NeDbDatabase.js +7 -6
- package/build/esm/databases/NeDbDatabase.d.ts +3 -3
- package/build/esm/databases/NeDbDatabase.js +7 -6
- package/build/esm/stores/AbstractStore.js +13 -8
- package/build/esm/types/database.types.d.ts +6 -4
- package/build/stores/AbstractStore.js +13 -8
- package/build/types/database.types.d.ts +6 -4
- package/package.json +3 -3
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
2
|
-
import { CreateOptions, Database } from '../types/database.types';
|
|
2
|
+
import { CreateOptions, Database, DatabaseInternalOptions } from '../types/database.types';
|
|
3
3
|
import { QueryOptions } from '../types/query.types';
|
|
4
4
|
export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
5
5
|
private collections;
|
|
@@ -22,8 +22,8 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
22
22
|
private loadCollection;
|
|
23
23
|
dropCollection(name: string): Promise<void>;
|
|
24
24
|
dropDatabase(): Promise<void>;
|
|
25
|
-
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any> | null>;
|
|
26
|
-
find(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any>[]>;
|
|
25
|
+
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any> | null>;
|
|
26
|
+
find(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>[]>;
|
|
27
27
|
update(collection: string, query: Record<string, any>, updates: Record<string, any>, neDbOptions?: Record<string, any>): Promise<number>;
|
|
28
28
|
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any>, neDbOptions?: Record<string, any>, action?: string): Promise<Record<string, any>>;
|
|
29
29
|
upsertOne(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
@@ -177,11 +177,12 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
177
177
|
this.collections = {};
|
|
178
178
|
return Promise.resolve();
|
|
179
179
|
}
|
|
180
|
-
async findOne(collection, query, options) {
|
|
181
|
-
const results = await this.find(collection,
|
|
182
|
-
|
|
180
|
+
async findOne(collection, query, options, dbOptions) {
|
|
181
|
+
const results = await this.find(collection, query !== null && query !== void 0 ? query : {}, Object.assign({ limit: 1 }, (options || {})), dbOptions);
|
|
182
|
+
const match = results[0];
|
|
183
|
+
return match;
|
|
183
184
|
}
|
|
184
|
-
async find(collection, query, options) {
|
|
185
|
+
async find(collection, query, options, dbOptions) {
|
|
185
186
|
await this.randomDelay();
|
|
186
187
|
if ((options === null || options === void 0 ? void 0 : options.limit) === 0) {
|
|
187
188
|
return [];
|
|
@@ -205,7 +206,7 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
205
206
|
reject(err);
|
|
206
207
|
}
|
|
207
208
|
else {
|
|
208
|
-
resolve(results.map((r) => this.normalizeRecord(r)));
|
|
209
|
+
resolve(results.map((r) => this.normalizeRecord(r, dbOptions)));
|
|
209
210
|
}
|
|
210
211
|
});
|
|
211
212
|
});
|
|
@@ -242,7 +243,7 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
242
243
|
}));
|
|
243
244
|
}
|
|
244
245
|
else {
|
|
245
|
-
resolve(docs ? this.normalizeRecord(docs) : numUpdated);
|
|
246
|
+
resolve(docs ? this.normalizeRecord(docs, neDbOptions) : numUpdated);
|
|
246
247
|
}
|
|
247
248
|
});
|
|
248
249
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import AbstractMutexer from '../mutexers/AbstractMutexer';
|
|
2
|
-
import { CreateOptions, Database } from '../types/database.types';
|
|
2
|
+
import { CreateOptions, Database, DatabaseInternalOptions } from '../types/database.types';
|
|
3
3
|
import { QueryOptions } from '../types/query.types';
|
|
4
4
|
export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
5
5
|
private collections;
|
|
@@ -22,8 +22,8 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
22
22
|
private loadCollection;
|
|
23
23
|
dropCollection(name: string): Promise<void>;
|
|
24
24
|
dropDatabase(): Promise<void>;
|
|
25
|
-
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any> | null>;
|
|
26
|
-
find(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any>[]>;
|
|
25
|
+
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any> | null>;
|
|
26
|
+
find(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>[]>;
|
|
27
27
|
update(collection: string, query: Record<string, any>, updates: Record<string, any>, neDbOptions?: Record<string, any>): Promise<number>;
|
|
28
28
|
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any>, neDbOptions?: Record<string, any>, action?: string): Promise<Record<string, any>>;
|
|
29
29
|
upsertOne(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
@@ -199,13 +199,14 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
199
199
|
return Promise.resolve();
|
|
200
200
|
});
|
|
201
201
|
}
|
|
202
|
-
findOne(collection, query, options) {
|
|
202
|
+
findOne(collection, query, options, dbOptions) {
|
|
203
203
|
return __awaiter(this, void 0, void 0, function* () {
|
|
204
|
-
const results = yield this.find(collection,
|
|
205
|
-
|
|
204
|
+
const results = yield this.find(collection, query !== null && query !== void 0 ? query : {}, Object.assign({ limit: 1 }, (options || {})), dbOptions);
|
|
205
|
+
const match = results[0];
|
|
206
|
+
return match;
|
|
206
207
|
});
|
|
207
208
|
}
|
|
208
|
-
find(collection, query, options) {
|
|
209
|
+
find(collection, query, options, dbOptions) {
|
|
209
210
|
return __awaiter(this, void 0, void 0, function* () {
|
|
210
211
|
yield this.randomDelay();
|
|
211
212
|
if ((options === null || options === void 0 ? void 0 : options.limit) === 0) {
|
|
@@ -230,7 +231,7 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
230
231
|
reject(err);
|
|
231
232
|
}
|
|
232
233
|
else {
|
|
233
|
-
resolve(results.map((r) => this.normalizeRecord(r)));
|
|
234
|
+
resolve(results.map((r) => this.normalizeRecord(r, dbOptions)));
|
|
234
235
|
}
|
|
235
236
|
});
|
|
236
237
|
});
|
|
@@ -271,7 +272,7 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
271
272
|
}));
|
|
272
273
|
}
|
|
273
274
|
else {
|
|
274
|
-
resolve(docs ? this.normalizeRecord(docs) : numUpdated);
|
|
275
|
+
resolve(docs ? this.normalizeRecord(docs, neDbOptions) : numUpdated);
|
|
275
276
|
}
|
|
276
277
|
}));
|
|
277
278
|
});
|
|
@@ -107,7 +107,7 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
107
107
|
? yield this.willCreate(values)
|
|
108
108
|
: values;
|
|
109
109
|
let { valuesToMixinBeforeCreate, values: nv } = yield this.handleWillCreatePlugins(cleanedValues);
|
|
110
|
-
const newValues =
|
|
110
|
+
const newValues = nv;
|
|
111
111
|
const toSave = this.normalizeBeforeSave(newValues);
|
|
112
112
|
const record = yield this.db.createOne(this.collectionName, Object.assign(Object.assign({}, toSave), valuesToMixinBeforeCreate), {
|
|
113
113
|
primaryFieldNames: this.primaryFieldNames,
|
|
@@ -151,9 +151,9 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
151
151
|
var _a;
|
|
152
152
|
return __awaiter(this, void 0, void 0, function* () {
|
|
153
153
|
let valuesToMixinBeforeCreate = {};
|
|
154
|
-
let newValues;
|
|
154
|
+
let newValues = values;
|
|
155
155
|
for (const plugin of this.plugins) {
|
|
156
|
-
const r = yield ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin,
|
|
156
|
+
const r = yield ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, newValues));
|
|
157
157
|
const { valuesToMixinBeforeCreate: v, newValues: nv } = r !== null && r !== void 0 ? r : {};
|
|
158
158
|
if (nv) {
|
|
159
159
|
newValues = nv;
|
|
@@ -226,14 +226,17 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
226
226
|
var _a, _b;
|
|
227
227
|
return __awaiter(this, void 0, void 0, function* () {
|
|
228
228
|
let resolvedQuery = query;
|
|
229
|
+
let resolvedOptions = queryOptions;
|
|
229
230
|
const { shouldTriggerWillQuery } = internalOptions;
|
|
230
231
|
if (shouldTriggerWillQuery) {
|
|
231
232
|
resolvedQuery = ((_b = (yield ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query);
|
|
233
|
+
const { query: q, options: o } = yield this.handleWillFindPlugins(resolvedQuery, queryOptions);
|
|
234
|
+
resolvedQuery = q;
|
|
235
|
+
resolvedOptions = o;
|
|
232
236
|
}
|
|
233
|
-
const {
|
|
234
|
-
|
|
235
|
-
|
|
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 }));
|
|
237
|
+
const results = yield this.db.find(this.collectionName, resolvedQuery, Object.assign(Object.assign({}, resolvedOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }), {
|
|
238
|
+
primaryFieldNames: this.primaryFieldNames,
|
|
239
|
+
});
|
|
237
240
|
if (results) {
|
|
238
241
|
const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
|
|
239
242
|
const records = yield Promise.all(all);
|
|
@@ -345,7 +348,9 @@ export default class AbstractStore extends AbstractMutexer {
|
|
|
345
348
|
for (const { name, value } of ops) {
|
|
346
349
|
normalizedValues[name] = value;
|
|
347
350
|
}
|
|
348
|
-
const results = yield this.db.updateOne(this.collectionName, q, normalizedValues
|
|
351
|
+
const results = yield this.db.updateOne(this.collectionName, q, normalizedValues, {
|
|
352
|
+
primaryFieldNames: this.primaryFieldNames,
|
|
353
|
+
});
|
|
349
354
|
yield ((_c = this.didUpdate) === null || _c === void 0 ? void 0 : _c.call(this, current, results));
|
|
350
355
|
return this.prepareAndNormalizeRecord(results, options);
|
|
351
356
|
}
|
|
@@ -16,9 +16,9 @@ export interface Database {
|
|
|
16
16
|
create(collection: string, values: Record<string, any>[]): Promise<Record<string, any>[]>;
|
|
17
17
|
dropCollection(name: string): Promise<void>;
|
|
18
18
|
dropDatabase(): Promise<void>;
|
|
19
|
-
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any> | null>;
|
|
20
|
-
find(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any>[]>;
|
|
21
|
-
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any
|
|
19
|
+
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any> | null>;
|
|
20
|
+
find(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>[]>;
|
|
21
|
+
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any>, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>>;
|
|
22
22
|
update(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<number>;
|
|
23
23
|
upsertOne(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
24
24
|
delete(collection: string, query: Record<string, any>): Promise<number>;
|
|
@@ -40,6 +40,8 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
|
|
|
40
40
|
}>;
|
|
41
41
|
export type UniqueIndex = string[];
|
|
42
42
|
export type Index = string[];
|
|
43
|
-
export interface CreateOptions {
|
|
43
|
+
export interface CreateOptions extends DatabaseInternalOptions {
|
|
44
|
+
}
|
|
45
|
+
export interface DatabaseInternalOptions {
|
|
44
46
|
primaryFieldNames?: string[];
|
|
45
47
|
}
|
|
@@ -121,7 +121,7 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
121
121
|
? await this.willCreate(values)
|
|
122
122
|
: values;
|
|
123
123
|
let { valuesToMixinBeforeCreate, values: nv } = await this.handleWillCreatePlugins(cleanedValues);
|
|
124
|
-
const newValues =
|
|
124
|
+
const newValues = nv;
|
|
125
125
|
const toSave = this.normalizeBeforeSave(newValues);
|
|
126
126
|
const record = await this.db.createOne(this.collectionName, Object.assign(Object.assign({}, toSave), valuesToMixinBeforeCreate), {
|
|
127
127
|
primaryFieldNames: this.primaryFieldNames,
|
|
@@ -163,9 +163,9 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
163
163
|
async handleWillCreatePlugins(values) {
|
|
164
164
|
var _a;
|
|
165
165
|
let valuesToMixinBeforeCreate = {};
|
|
166
|
-
let newValues;
|
|
166
|
+
let newValues = values;
|
|
167
167
|
for (const plugin of this.plugins) {
|
|
168
|
-
const r = await ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin,
|
|
168
|
+
const r = await ((_a = plugin.willCreateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, newValues));
|
|
169
169
|
const { valuesToMixinBeforeCreate: v, newValues: nv } = r !== null && r !== void 0 ? r : {};
|
|
170
170
|
if (nv) {
|
|
171
171
|
newValues = nv;
|
|
@@ -224,14 +224,17 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
224
224
|
}) {
|
|
225
225
|
var _a, _b;
|
|
226
226
|
let resolvedQuery = query;
|
|
227
|
+
let resolvedOptions = queryOptions;
|
|
227
228
|
const { shouldTriggerWillQuery } = internalOptions;
|
|
228
229
|
if (shouldTriggerWillQuery) {
|
|
229
230
|
resolvedQuery = ((_b = (await ((_a = this.willFind) === null || _a === void 0 ? void 0 : _a.call(this, query)))) !== null && _b !== void 0 ? _b : query);
|
|
231
|
+
const { query: q, options: o } = await this.handleWillFindPlugins(resolvedQuery, queryOptions);
|
|
232
|
+
resolvedQuery = q;
|
|
233
|
+
resolvedOptions = o;
|
|
230
234
|
}
|
|
231
|
-
const {
|
|
232
|
-
|
|
233
|
-
|
|
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 }));
|
|
235
|
+
const results = await this.db.find(this.collectionName, resolvedQuery, Object.assign(Object.assign({}, resolvedOptions), { includeFields: options === null || options === void 0 ? void 0 : options.includeFields }), {
|
|
236
|
+
primaryFieldNames: this.primaryFieldNames,
|
|
237
|
+
});
|
|
235
238
|
if (results) {
|
|
236
239
|
const all = results.map((result) => this.prepareAndNormalizeRecord(result, Object.assign(Object.assign({}, options), { shouldStripUndefinedAndNullValues: true })));
|
|
237
240
|
const records = await Promise.all(all);
|
|
@@ -331,7 +334,9 @@ class AbstractStore extends AbstractMutexer_1.default {
|
|
|
331
334
|
for (const { name, value } of ops) {
|
|
332
335
|
normalizedValues[name] = value;
|
|
333
336
|
}
|
|
334
|
-
const results = await this.db.updateOne(this.collectionName, q, normalizedValues
|
|
337
|
+
const results = await this.db.updateOne(this.collectionName, q, normalizedValues, {
|
|
338
|
+
primaryFieldNames: this.primaryFieldNames,
|
|
339
|
+
});
|
|
335
340
|
await ((_c = this.didUpdate) === null || _c === void 0 ? void 0 : _c.call(this, current, results));
|
|
336
341
|
return this.prepareAndNormalizeRecord(results, options);
|
|
337
342
|
}
|
|
@@ -16,9 +16,9 @@ export interface Database {
|
|
|
16
16
|
create(collection: string, values: Record<string, any>[]): Promise<Record<string, any>[]>;
|
|
17
17
|
dropCollection(name: string): Promise<void>;
|
|
18
18
|
dropDatabase(): Promise<void>;
|
|
19
|
-
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any> | null>;
|
|
20
|
-
find(collection: string, query?: Record<string, any>, options?: QueryOptions): Promise<Record<string, any>[]>;
|
|
21
|
-
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any
|
|
19
|
+
findOne(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any> | null>;
|
|
20
|
+
find(collection: string, query?: Record<string, any>, options?: QueryOptions, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>[]>;
|
|
21
|
+
updateOne(collection: string, query: Record<string, any>, updates: Record<string, any>, dbOptions?: DatabaseInternalOptions): Promise<Record<string, any>>;
|
|
22
22
|
update(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<number>;
|
|
23
23
|
upsertOne(collection: string, query: Record<string, any>, updates: Record<string, any>): Promise<Record<string, any>>;
|
|
24
24
|
delete(collection: string, query: Record<string, any>): Promise<number>;
|
|
@@ -40,6 +40,8 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
|
|
|
40
40
|
}>;
|
|
41
41
|
export type UniqueIndex = string[];
|
|
42
42
|
export type Index = string[];
|
|
43
|
-
export interface CreateOptions {
|
|
43
|
+
export interface CreateOptions extends DatabaseInternalOptions {
|
|
44
|
+
}
|
|
45
|
+
export interface DatabaseInternalOptions {
|
|
44
46
|
primaryFieldNames?: string[];
|
|
45
47
|
}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "26.2.
|
|
6
|
+
"version": "26.2.4",
|
|
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.2.
|
|
74
|
-
"@sprucelabs/spruce-skill-utils": "^30.1.
|
|
73
|
+
"@sprucelabs/schema": "^29.2.1",
|
|
74
|
+
"@sprucelabs/spruce-skill-utils": "^30.1.41",
|
|
75
75
|
"just-clone": "^6.2.0",
|
|
76
76
|
"lodash": "^4.17.21",
|
|
77
77
|
"mongodb": "^6.3.0",
|