@naturalcycles/db-lib 8.43.5 → 8.44.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.
@@ -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
- (_a = this.data)[table] || (_a[table] = {});
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
- (_a = this.data)[table] || (_a[table] = {});
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
- (_a = this.data)[table] || (_a[table] = {});
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
- var _a;
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
- var _a;
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
- var _a;
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
- var _a;
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
- (_a = this.cfg.hooks).createId || (_a.createId = () => (0, nodejs_lib_1.stringId)());
93
+ this.cfg.hooks.createRandomId ||= () => (0, nodejs_lib_1.stringId)();
95
94
  }
96
95
  else {
97
- delete this.cfg.hooks.createId;
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 || (opt.errorMode = js_lib_1.ErrorMode.SUPPRESS);
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 || (opt.errorMode = js_lib_1.ErrorMode.SUPPRESS);
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 || (opt.errorMode = js_lib_1.ErrorMode.SUPPRESS);
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 || (opt.errorMode = js_lib_1.ErrorMode.SUPPRESS);
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 || (opt.errorMode = js_lib_1.ErrorMode.SUPPRESS);
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 || (opt.errorMode = js_lib_1.ErrorMode.SUPPRESS);
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,12 +485,13 @@ 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));
489
+ if (this.cfg.createId) {
490
+ obj.id ||= this.cfg.hooks.createNaturalId?.(obj) || this.cfg.hooks.createRandomId();
491
+ }
492
492
  if (this.cfg.created) {
493
493
  ;
494
- (_a = obj)['created'] || (_a['created'] = obj['updated'] || now);
494
+ obj['created'] ||= obj['updated'] || now;
495
495
  }
496
496
  if (this.cfg.updated) {
497
497
  ;
@@ -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
- createId: (obj: DBM | BM) => ID;
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": "^10.0.0"
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.43.5",
44
+ "version": "8.44.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
- createId: (obj: DBM | BM) => ID
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!.createId ||= () => stringId() as ID
109
+ this.cfg.hooks!.createRandomId ||= () => stringId() as ID
110
110
  } else {
111
- delete this.cfg.hooks!.createId
111
+ delete this.cfg.hooks!.createRandomId
112
112
  }
113
113
  }
114
114
 
@@ -608,7 +608,9 @@ 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)
611
+ if (this.cfg.createId) {
612
+ obj.id ||= this.cfg.hooks!.createNaturalId?.(obj as BM) || this.cfg.hooks!.createRandomId!()
613
+ }
612
614
 
613
615
  if (this.cfg.created) {
614
616
  ;(obj as any)['created'] ||= (obj as any)['updated'] || now