@naturalcycles/db-lib 8.43.5 → 8.45.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/dist/adapter/inmemory/inMemory.db.js +3 -6
- package/dist/adapter/inmemory/inMemoryKeyValueDB.js +4 -8
- package/dist/commondao/common.dao.js +12 -12
- package/dist/commondao/common.dao.model.d.ts +6 -1
- package/package.json +2 -2
- package/src/commondao/common.dao.model.ts +6 -1
- package/src/commondao/common.dao.ts +6 -4
|
@@ -58,25 +58,22 @@ class InMemoryDB {
|
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
async createTable(_table, _schema, opt = {}) {
|
|
61
|
-
var _a;
|
|
62
61
|
const table = this.cfg.tablesPrefix + _table;
|
|
63
62
|
if (opt.dropIfExists) {
|
|
64
63
|
this.data[table] = {};
|
|
65
64
|
}
|
|
66
65
|
else {
|
|
67
|
-
|
|
66
|
+
this.data[table] ||= {};
|
|
68
67
|
}
|
|
69
68
|
}
|
|
70
69
|
async getByIds(_table, ids, _opt) {
|
|
71
|
-
var _a;
|
|
72
70
|
const table = this.cfg.tablesPrefix + _table;
|
|
73
|
-
|
|
71
|
+
this.data[table] ||= {};
|
|
74
72
|
return ids.map(id => this.data[table][id]).filter(Boolean);
|
|
75
73
|
}
|
|
76
74
|
async saveBatch(_table, rows, opt = {}) {
|
|
77
|
-
var _a;
|
|
78
75
|
const table = this.cfg.tablesPrefix + _table;
|
|
79
|
-
|
|
76
|
+
this.data[table] ||= {};
|
|
80
77
|
rows.forEach(r => {
|
|
81
78
|
if (!r.id) {
|
|
82
79
|
this.cfg.logger.warn({ rows });
|
|
@@ -11,18 +11,15 @@ class InMemoryKeyValueDB {
|
|
|
11
11
|
async ping() { }
|
|
12
12
|
async createTable(_table, _opt) { }
|
|
13
13
|
async deleteByIds(table, ids) {
|
|
14
|
-
|
|
15
|
-
(_a = this.data)[table] || (_a[table] = {});
|
|
14
|
+
this.data[table] ||= {};
|
|
16
15
|
ids.forEach(id => delete this.data[table][id]);
|
|
17
16
|
}
|
|
18
17
|
async getByIds(table, ids) {
|
|
19
|
-
|
|
20
|
-
(_a = this.data)[table] || (_a[table] = {});
|
|
18
|
+
this.data[table] ||= {};
|
|
21
19
|
return ids.map(id => [id, this.data[table][id]]).filter(e => e[1]);
|
|
22
20
|
}
|
|
23
21
|
async saveBatch(table, entries) {
|
|
24
|
-
|
|
25
|
-
(_a = this.data)[table] || (_a[table] = {});
|
|
22
|
+
this.data[table] ||= {};
|
|
26
23
|
entries.forEach(([id, buf]) => (this.data[table][id] = buf));
|
|
27
24
|
}
|
|
28
25
|
streamIds(table, limit) {
|
|
@@ -35,8 +32,7 @@ class InMemoryKeyValueDB {
|
|
|
35
32
|
return node_stream_1.Readable.from(Object.entries(this.data[table] || {}).slice(0, limit));
|
|
36
33
|
}
|
|
37
34
|
async count(table) {
|
|
38
|
-
|
|
39
|
-
(_a = this.data)[table] || (_a[table] = {});
|
|
35
|
+
this.data[table] ||= {};
|
|
40
36
|
return Object.keys(this.data[table]).length;
|
|
41
37
|
}
|
|
42
38
|
}
|
|
@@ -20,7 +20,6 @@ const isCI = !!process.env['CI'];
|
|
|
20
20
|
*/
|
|
21
21
|
class CommonDao {
|
|
22
22
|
constructor(cfg) {
|
|
23
|
-
var _a;
|
|
24
23
|
this.cfg = cfg;
|
|
25
24
|
this.tx = {
|
|
26
25
|
save: async (bm, opt = {}) => {
|
|
@@ -91,10 +90,10 @@ class CommonDao {
|
|
|
91
90
|
};
|
|
92
91
|
if (this.cfg.createId) {
|
|
93
92
|
(0, js_lib_1._assert)(this.cfg.idType === 'string', 'db-lib: automatic generation of non-string ids is not supported');
|
|
94
|
-
|
|
93
|
+
this.cfg.hooks.createRandomId ||= () => (0, nodejs_lib_1.stringId)();
|
|
95
94
|
}
|
|
96
95
|
else {
|
|
97
|
-
delete this.cfg.hooks.
|
|
96
|
+
delete this.cfg.hooks.createRandomId;
|
|
98
97
|
}
|
|
99
98
|
}
|
|
100
99
|
// CREATE
|
|
@@ -333,7 +332,7 @@ class CommonDao {
|
|
|
333
332
|
q.table = opt.table || q.table;
|
|
334
333
|
opt.skipValidation = opt.skipValidation !== false; // default true
|
|
335
334
|
opt.skipConversion = opt.skipConversion !== false; // default true
|
|
336
|
-
opt.errorMode
|
|
335
|
+
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS;
|
|
337
336
|
const partialQuery = !!q._selectedFieldNames;
|
|
338
337
|
const op = `streamQueryForEach(${q.pretty()})`;
|
|
339
338
|
const started = this.logStarted(op, q.table, true);
|
|
@@ -367,7 +366,7 @@ class CommonDao {
|
|
|
367
366
|
q.table = opt.table || q.table;
|
|
368
367
|
opt.skipValidation = opt.skipValidation !== false; // default true
|
|
369
368
|
opt.skipConversion = opt.skipConversion !== false; // default true
|
|
370
|
-
opt.errorMode
|
|
369
|
+
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS;
|
|
371
370
|
const partialQuery = !!q._selectedFieldNames;
|
|
372
371
|
const op = `streamQueryAsDBMForEach(${q.pretty()})`;
|
|
373
372
|
const started = this.logStarted(op, q.table, true);
|
|
@@ -402,7 +401,7 @@ class CommonDao {
|
|
|
402
401
|
q.table = opt.table || q.table;
|
|
403
402
|
opt.skipValidation = opt.skipValidation !== false; // default true
|
|
404
403
|
opt.skipConversion = opt.skipConversion !== false; // default true
|
|
405
|
-
opt.errorMode
|
|
404
|
+
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS;
|
|
406
405
|
const partialQuery = !!q._selectedFieldNames;
|
|
407
406
|
const stream = this.cfg.db.streamQuery(q, opt);
|
|
408
407
|
if (partialQuery || opt.raw)
|
|
@@ -426,7 +425,7 @@ class CommonDao {
|
|
|
426
425
|
q.table = opt.table || q.table;
|
|
427
426
|
opt.skipValidation = opt.skipValidation !== false; // default true
|
|
428
427
|
opt.skipConversion = opt.skipConversion !== false; // default true
|
|
429
|
-
opt.errorMode
|
|
428
|
+
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS;
|
|
430
429
|
const stream = this.cfg.db.streamQuery(q, opt);
|
|
431
430
|
const partialQuery = !!q._selectedFieldNames;
|
|
432
431
|
if (partialQuery || opt.raw)
|
|
@@ -451,7 +450,7 @@ class CommonDao {
|
|
|
451
450
|
}
|
|
452
451
|
streamQueryIds(q, opt = {}) {
|
|
453
452
|
q.table = opt.table || q.table;
|
|
454
|
-
opt.errorMode
|
|
453
|
+
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS;
|
|
455
454
|
const stream = this.cfg.db
|
|
456
455
|
.streamQuery(q.select(['id']), opt)
|
|
457
456
|
.on('error', err => stream.emit('error', err))
|
|
@@ -462,7 +461,7 @@ class CommonDao {
|
|
|
462
461
|
}
|
|
463
462
|
async streamQueryIdsForEach(q, mapper, opt = {}) {
|
|
464
463
|
q.table = opt.table || q.table;
|
|
465
|
-
opt.errorMode
|
|
464
|
+
opt.errorMode ||= js_lib_1.ErrorMode.SUPPRESS;
|
|
466
465
|
const op = `streamQueryIdsForEach(${q.pretty()})`;
|
|
467
466
|
const started = this.logStarted(op, q.table, true);
|
|
468
467
|
let count = 0;
|
|
@@ -486,18 +485,19 @@ class CommonDao {
|
|
|
486
485
|
}
|
|
487
486
|
}
|
|
488
487
|
assignIdCreatedUpdated(obj, opt = {}) {
|
|
489
|
-
var _a;
|
|
490
488
|
const now = Math.floor(Date.now() / 1000);
|
|
491
|
-
obj.id || (obj.id = this.cfg.hooks.createId?.(obj));
|
|
492
489
|
if (this.cfg.created) {
|
|
493
490
|
;
|
|
494
|
-
|
|
491
|
+
obj['created'] ||= obj['updated'] || now;
|
|
495
492
|
}
|
|
496
493
|
if (this.cfg.updated) {
|
|
497
494
|
;
|
|
498
495
|
obj['updated'] =
|
|
499
496
|
opt.preserveUpdatedCreated && obj['updated'] ? obj['updated'] : now;
|
|
500
497
|
}
|
|
498
|
+
if (this.cfg.createId) {
|
|
499
|
+
obj.id ||= this.cfg.hooks.createNaturalId?.(obj) || this.cfg.hooks.createRandomId();
|
|
500
|
+
}
|
|
501
501
|
return obj;
|
|
502
502
|
}
|
|
503
503
|
// SAVE
|
|
@@ -3,7 +3,12 @@ import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchemaTyped, T
|
|
|
3
3
|
import { CommonDB } from '../common.db';
|
|
4
4
|
import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
|
|
5
5
|
export interface CommonDaoHooks<BM extends Partial<ObjectWithId<ID>>, DBM extends ObjectWithId<ID>, TM, ID extends string | number> {
|
|
6
|
-
|
|
6
|
+
createRandomId: () => ID;
|
|
7
|
+
/**
|
|
8
|
+
* createNaturalId hook is called (tried) first.
|
|
9
|
+
* If it doesn't exist - createRandomId is called.
|
|
10
|
+
*/
|
|
11
|
+
createNaturalId: (obj: DBM | BM) => ID;
|
|
7
12
|
parseNaturalId: (id: ID) => Partial<DBM>;
|
|
8
13
|
beforeCreate: (bm: Partial<BM>) => Partial<BM>;
|
|
9
14
|
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>;
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
"dependencies": {
|
|
7
7
|
"@naturalcycles/js-lib": "^14.0.0",
|
|
8
8
|
"@naturalcycles/nodejs-lib": "^12.0.0",
|
|
9
|
-
"fs-extra": "^
|
|
9
|
+
"fs-extra": "^11.1.0"
|
|
10
10
|
},
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"@naturalcycles/bench-lib": "^1.0.0",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"engines": {
|
|
42
42
|
"node": ">=14.15"
|
|
43
43
|
},
|
|
44
|
-
"version": "8.
|
|
44
|
+
"version": "8.45.0",
|
|
45
45
|
"description": "Lowest Common Denominator API to supported Databases",
|
|
46
46
|
"keywords": [
|
|
47
47
|
"db",
|
|
@@ -16,7 +16,12 @@ export interface CommonDaoHooks<
|
|
|
16
16
|
TM,
|
|
17
17
|
ID extends string | number,
|
|
18
18
|
> {
|
|
19
|
-
|
|
19
|
+
createRandomId: () => ID
|
|
20
|
+
/**
|
|
21
|
+
* createNaturalId hook is called (tried) first.
|
|
22
|
+
* If it doesn't exist - createRandomId is called.
|
|
23
|
+
*/
|
|
24
|
+
createNaturalId: (obj: DBM | BM) => ID
|
|
20
25
|
parseNaturalId: (id: ID) => Partial<DBM>
|
|
21
26
|
beforeCreate: (bm: Partial<BM>) => Partial<BM>
|
|
22
27
|
beforeDBMValidate: (dbm: Partial<DBM>) => Partial<DBM>
|
|
@@ -106,9 +106,9 @@ export class CommonDao<
|
|
|
106
106
|
'db-lib: automatic generation of non-string ids is not supported',
|
|
107
107
|
)
|
|
108
108
|
|
|
109
|
-
this.cfg.hooks!.
|
|
109
|
+
this.cfg.hooks!.createRandomId ||= () => stringId() as ID
|
|
110
110
|
} else {
|
|
111
|
-
delete this.cfg.hooks!.
|
|
111
|
+
delete this.cfg.hooks!.createRandomId
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
|
|
@@ -608,8 +608,6 @@ export class CommonDao<
|
|
|
608
608
|
assignIdCreatedUpdated(obj: DBM | BM | Unsaved<BM>, opt: CommonDaoOptions = {}): DBM | Saved<BM> {
|
|
609
609
|
const now = Math.floor(Date.now() / 1000)
|
|
610
610
|
|
|
611
|
-
obj.id ||= this.cfg.hooks!.createId?.(obj as BM)
|
|
612
|
-
|
|
613
611
|
if (this.cfg.created) {
|
|
614
612
|
;(obj as any)['created'] ||= (obj as any)['updated'] || now
|
|
615
613
|
}
|
|
@@ -619,6 +617,10 @@ export class CommonDao<
|
|
|
619
617
|
opt.preserveUpdatedCreated && (obj as any)['updated'] ? (obj as any)['updated'] : now
|
|
620
618
|
}
|
|
621
619
|
|
|
620
|
+
if (this.cfg.createId) {
|
|
621
|
+
obj.id ||= this.cfg.hooks!.createNaturalId?.(obj as BM) || this.cfg.hooks!.createRandomId!()
|
|
622
|
+
}
|
|
623
|
+
|
|
622
624
|
return obj as any
|
|
623
625
|
}
|
|
624
626
|
|