@naturalcycles/db-lib 9.26.0 → 9.26.2

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.
@@ -160,7 +160,7 @@ class FileDB extends __1.BaseCommonDB {
160
160
  if (this.cfg.logStarted) {
161
161
  this.cfg.logger?.log(`>> ${op}`);
162
162
  }
163
- return Date.now();
163
+ return js_lib_1.localTime.nowUnixMillis();
164
164
  }
165
165
  logFinished(started, op) {
166
166
  if (!this.cfg.logFinished)
@@ -166,7 +166,7 @@ class InMemoryDB {
166
166
  async flushToDisk() {
167
167
  (0, js_lib_1._assert)(this.cfg.persistenceEnabled, 'flushToDisk() called but persistenceEnabled=false');
168
168
  const { persistentStoragePath, persistZip } = this.cfg;
169
- const started = Date.now();
169
+ const started = js_lib_1.localTime.nowUnixMillis();
170
170
  await nodejs_lib_1.fs2.emptyDirAsync(persistentStoragePath);
171
171
  let tables = 0;
172
172
  // infinite concurrency for now
@@ -186,7 +186,7 @@ class InMemoryDB {
186
186
  async restoreFromDisk() {
187
187
  (0, js_lib_1._assert)(this.cfg.persistenceEnabled, 'restoreFromDisk() called but persistenceEnabled=false');
188
188
  const { persistentStoragePath } = this.cfg;
189
- const started = Date.now();
189
+ const started = js_lib_1.localTime.nowUnixMillis();
190
190
  await nodejs_lib_1.fs2.ensureDirAsync(persistentStoragePath);
191
191
  this.data = {}; // empty it in the beginning!
192
192
  const files = (await nodejs_lib_1.fs2.readdirAsync(persistentStoragePath)).filter(f => f.includes('.ndjson'));
@@ -63,7 +63,7 @@ class CommonDao {
63
63
  const op = `getById(${id})`;
64
64
  const table = opt.table || this.cfg.table;
65
65
  const started = this.logStarted(op, table);
66
- let dbm = (await (opt.tx || this.cfg.db).getByIds(table, [id]))[0];
66
+ let dbm = (await (opt.tx || this.cfg.db).getByIds(table, [id], opt))[0];
67
67
  if (dbm && this.cfg.hooks.afterLoad) {
68
68
  dbm = (await this.cfg.hooks.afterLoad(dbm)) || undefined;
69
69
  }
@@ -85,7 +85,7 @@ class CommonDao {
85
85
  const op = `getByIdAsDBM(${id})`;
86
86
  const table = opt.table || this.cfg.table;
87
87
  const started = this.logStarted(op, table);
88
- let [dbm] = await (opt.tx || this.cfg.db).getByIds(table, [id]);
88
+ let [dbm] = await (opt.tx || this.cfg.db).getByIds(table, [id], opt);
89
89
  if (dbm && this.cfg.hooks.afterLoad) {
90
90
  dbm = (await this.cfg.hooks.afterLoad(dbm)) || undefined;
91
91
  }
@@ -99,7 +99,7 @@ class CommonDao {
99
99
  const op = `getByIds ${ids.length} id(s) (${(0, js_lib_1._truncate)(ids.slice(0, 10).join(', '), 50)})`;
100
100
  const table = opt.table || this.cfg.table;
101
101
  const started = this.logStarted(op, table);
102
- let dbms = await (opt.tx || this.cfg.db).getByIds(table, ids);
102
+ let dbms = await (opt.tx || this.cfg.db).getByIds(table, ids, opt);
103
103
  if (this.cfg.hooks.afterLoad && dbms.length) {
104
104
  dbms = (await (0, js_lib_1.pMap)(dbms, async (dbm) => await this.cfg.hooks.afterLoad(dbm))).filter(js_lib_1._isTruthy);
105
105
  }
@@ -113,7 +113,7 @@ class CommonDao {
113
113
  const op = `getByIdsAsDBM ${ids.length} id(s) (${(0, js_lib_1._truncate)(ids.slice(0, 10).join(', '), 50)})`;
114
114
  const table = opt.table || this.cfg.table;
115
115
  const started = this.logStarted(op, table);
116
- let dbms = await (opt.tx || this.cfg.db).getByIds(table, ids);
116
+ let dbms = await (opt.tx || this.cfg.db).getByIds(table, ids, opt);
117
117
  if (this.cfg.hooks.afterLoad && dbms.length) {
118
118
  dbms = (await (0, js_lib_1.pMap)(dbms, async (dbm) => await this.cfg.hooks.afterLoad(dbm))).filter(js_lib_1._isTruthy);
119
119
  }
@@ -978,19 +978,14 @@ class CommonDao {
978
978
  }
979
979
  else {
980
980
  // Joi
981
- const start = performance.now();
981
+ const start = js_lib_1.localTime.nowUnixMillis();
982
982
  const vr = (0, nodejs_lib_1.getValidationResult)(obj, schema, objectName);
983
- const end = performance.now();
984
- const tookMillis = end - start;
985
- if (this.cfg.debugValidationTimeThreshhold &&
986
- tookMillis >= this.cfg.debugValidationTimeThreshhold) {
987
- this.cfg.onValidationTimeThreshold?.({
988
- tookMillis,
989
- error: !!vr.error,
990
- table,
991
- obj,
992
- });
993
- }
983
+ const tookMillis = js_lib_1.localTime.nowUnixMillis() - start;
984
+ this.cfg.onValidationTime?.({
985
+ tookMillis,
986
+ table,
987
+ obj,
988
+ });
994
989
  error = vr.error;
995
990
  convertedValue = vr.value;
996
991
  }
@@ -1073,7 +1068,7 @@ class CommonDao {
1073
1068
  if (this.cfg.logStarted || force) {
1074
1069
  this.cfg.logger?.log(`>> ${table}.${op}`);
1075
1070
  }
1076
- return Date.now();
1071
+ return js_lib_1.localTime.nowUnixMillis();
1077
1072
  }
1078
1073
  logSaveStarted(op, items, table) {
1079
1074
  if (this.cfg.logStarted) {
@@ -1093,7 +1088,7 @@ class CommonDao {
1093
1088
  }
1094
1089
  this.cfg.logger?.log(...args);
1095
1090
  }
1096
- return Date.now();
1091
+ return js_lib_1.localTime.nowUnixMillis();
1097
1092
  }
1098
1093
  }
1099
1094
  exports.CommonDao = CommonDao;
@@ -1,4 +1,4 @@
1
- import { AnyObject, BaseDBEntity, CommonLogger, ErrorMode, NumberOfMilliseconds, Promisable, UnixTimestamp, ZodError, ZodSchema } from '@naturalcycles/js-lib';
1
+ import { BaseDBEntity, CommonLogger, ErrorMode, NumberOfMilliseconds, Promisable, UnixTimestamp, ZodError, ZodSchema } from '@naturalcycles/js-lib';
2
2
  import { AjvSchema, AjvValidationError, JoiValidationError, ObjectSchema, TransformLogProgressOptions, TransformMapOptions } from '@naturalcycles/nodejs-lib';
3
3
  import { CommonDB } from '../common.db';
4
4
  import { CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions } from '../db.model';
@@ -112,14 +112,11 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
112
112
  */
113
113
  validateOnSave?: boolean;
114
114
  /**
115
- * Defaults to undefined == disabled.
116
- * If set - enable the monitoring of validation time and will alert on crossing the threshold.
117
- */
118
- debugValidationTimeThreshhold?: NumberOfMilliseconds;
119
- /**
120
- * Called when debugValidationTimeThreshhold is crossed.
115
+ * The hook allows to get a callback, to instrument "validation time".
116
+ *
117
+ * @experimental
121
118
  */
122
- onValidationTimeThreshold?: (info: AnyObject) => void;
119
+ onValidationTime?: (data: OnValidationTimeData) => void;
123
120
  /**
124
121
  * Defaults to false.
125
122
  * Setting it to true will set saveMethod to `insert` for save/saveBatch, which will
@@ -288,3 +285,8 @@ export interface CommonDaoStreamOptions<IN> extends CommonDaoReadOptions, Transf
288
285
  chunkConcurrency?: number;
289
286
  }
290
287
  export type CommonDaoCreateOptions = CommonDBCreateOptions;
288
+ export interface OnValidationTimeData {
289
+ tookMillis: NumberOfMilliseconds;
290
+ table: string;
291
+ obj: any;
292
+ }
package/package.json CHANGED
@@ -45,7 +45,7 @@
45
45
  "engines": {
46
46
  "node": ">=22.10.0"
47
47
  },
48
- "version": "9.26.0",
48
+ "version": "9.26.2",
49
49
  "description": "Lowest Common Denominator API to supported Databases",
50
50
  "keywords": [
51
51
  "db",
@@ -9,6 +9,7 @@ import {
9
9
  _stringMapValues,
10
10
  generateJsonSchemaFromData,
11
11
  JsonSchemaRootObject,
12
+ localTime,
12
13
  ObjectWithId,
13
14
  UnixTimestampMillis,
14
15
  } from '@naturalcycles/js-lib'
@@ -245,7 +246,7 @@ export class FileDB extends BaseCommonDB implements CommonDB {
245
246
  if (this.cfg.logStarted) {
246
247
  this.cfg.logger?.log(`>> ${op}`)
247
248
  }
248
- return Date.now() as UnixTimestampMillis
249
+ return localTime.nowUnixMillis()
249
250
  }
250
251
 
251
252
  private logFinished(started: UnixTimestampMillis, op: string): void {
@@ -13,10 +13,10 @@ import {
13
13
  generateJsonSchemaFromData,
14
14
  JsonSchemaObject,
15
15
  JsonSchemaRootObject,
16
+ localTime,
16
17
  ObjectWithId,
17
18
  pMap,
18
19
  StringMap,
19
- UnixTimestampMillis,
20
20
  } from '@naturalcycles/js-lib'
21
21
  import {
22
22
  _pipeline,
@@ -315,7 +315,7 @@ export class InMemoryDB implements CommonDB {
315
315
  _assert(this.cfg.persistenceEnabled, 'flushToDisk() called but persistenceEnabled=false')
316
316
  const { persistentStoragePath, persistZip } = this.cfg
317
317
 
318
- const started = Date.now() as UnixTimestampMillis
318
+ const started = localTime.nowUnixMillis()
319
319
 
320
320
  await fs2.emptyDirAsync(persistentStoragePath)
321
321
 
@@ -344,7 +344,7 @@ export class InMemoryDB implements CommonDB {
344
344
  _assert(this.cfg.persistenceEnabled, 'restoreFromDisk() called but persistenceEnabled=false')
345
345
  const { persistentStoragePath } = this.cfg
346
346
 
347
- const started = Date.now() as UnixTimestampMillis
347
+ const started = localTime.nowUnixMillis()
348
348
 
349
349
  await fs2.ensureDirAsync(persistentStoragePath)
350
350
 
@@ -1,5 +1,4 @@
1
1
  import {
2
- AnyObject,
3
2
  BaseDBEntity,
4
3
  CommonLogger,
5
4
  ErrorMode,
@@ -149,15 +148,11 @@ export interface CommonDaoCfg<
149
148
  validateOnSave?: boolean
150
149
 
151
150
  /**
152
- * Defaults to undefined == disabled.
153
- * If set - enable the monitoring of validation time and will alert on crossing the threshold.
154
- */
155
- debugValidationTimeThreshhold?: NumberOfMilliseconds
156
-
157
- /**
158
- * Called when debugValidationTimeThreshhold is crossed.
151
+ * The hook allows to get a callback, to instrument "validation time".
152
+ *
153
+ * @experimental
159
154
  */
160
- onValidationTimeThreshold?: (info: AnyObject) => void
155
+ onValidationTime?: (data: OnValidationTimeData) => void
161
156
 
162
157
  /**
163
158
  * Defaults to false.
@@ -365,3 +360,9 @@ export interface CommonDaoStreamOptions<IN>
365
360
  }
366
361
 
367
362
  export type CommonDaoCreateOptions = CommonDBCreateOptions
363
+
364
+ export interface OnValidationTimeData {
365
+ tookMillis: NumberOfMilliseconds
366
+ table: string
367
+ obj: any
368
+ }
@@ -123,7 +123,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
123
123
  const table = opt.table || this.cfg.table
124
124
  const started = this.logStarted(op, table)
125
125
 
126
- let dbm = (await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id as string]))[0]
126
+ let dbm = (await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id as string], opt))[0]
127
127
  if (dbm && this.cfg.hooks!.afterLoad) {
128
128
  dbm = (await this.cfg.hooks!.afterLoad(dbm)) || undefined
129
129
  }
@@ -147,7 +147,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
147
147
  const op = `getByIdAsDBM(${id})`
148
148
  const table = opt.table || this.cfg.table
149
149
  const started = this.logStarted(op, table)
150
- let [dbm] = await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id as string])
150
+ let [dbm] = await (opt.tx || this.cfg.db).getByIds<DBM>(table, [id as string], opt)
151
151
  if (dbm && this.cfg.hooks!.afterLoad) {
152
152
  dbm = (await this.cfg.hooks!.afterLoad(dbm)) || undefined
153
153
  }
@@ -162,7 +162,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
162
162
  const op = `getByIds ${ids.length} id(s) (${_truncate(ids.slice(0, 10).join(', '), 50)})`
163
163
  const table = opt.table || this.cfg.table
164
164
  const started = this.logStarted(op, table)
165
- let dbms = await (opt.tx || this.cfg.db).getByIds<DBM>(table, ids as string[])
165
+ let dbms = await (opt.tx || this.cfg.db).getByIds<DBM>(table, ids as string[], opt)
166
166
  if (this.cfg.hooks!.afterLoad && dbms.length) {
167
167
  dbms = (await pMap(dbms, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter(
168
168
  _isTruthy,
@@ -179,7 +179,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
179
179
  const op = `getByIdsAsDBM ${ids.length} id(s) (${_truncate(ids.slice(0, 10).join(', '), 50)})`
180
180
  const table = opt.table || this.cfg.table
181
181
  const started = this.logStarted(op, table)
182
- let dbms = await (opt.tx || this.cfg.db).getByIds<DBM>(table, ids as string[])
182
+ let dbms = await (opt.tx || this.cfg.db).getByIds<DBM>(table, ids as string[], opt)
183
183
  if (this.cfg.hooks!.afterLoad && dbms.length) {
184
184
  dbms = (await pMap(dbms, async dbm => await this.cfg.hooks!.afterLoad!(dbm))).filter(
185
185
  _isTruthy,
@@ -1267,22 +1267,15 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
1267
1267
  })
1268
1268
  } else {
1269
1269
  // Joi
1270
- const start = performance.now()
1270
+ const start = localTime.nowUnixMillis()
1271
1271
  const vr = getValidationResult(obj, schema, objectName)
1272
- const end = performance.now()
1273
- const tookMillis = end - start
1274
-
1275
- if (
1276
- this.cfg.debugValidationTimeThreshhold &&
1277
- tookMillis >= this.cfg.debugValidationTimeThreshhold
1278
- ) {
1279
- this.cfg.onValidationTimeThreshold?.({
1280
- tookMillis,
1281
- error: !!vr.error,
1282
- table,
1283
- obj,
1284
- })
1285
- }
1272
+ const tookMillis = localTime.nowUnixMillis() - start
1273
+
1274
+ this.cfg.onValidationTime?.({
1275
+ tookMillis,
1276
+ table,
1277
+ obj,
1278
+ })
1286
1279
 
1287
1280
  error = vr.error
1288
1281
  convertedValue = vr.value
@@ -1384,7 +1377,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
1384
1377
  if (this.cfg.logStarted || force) {
1385
1378
  this.cfg.logger?.log(`>> ${table}.${op}`)
1386
1379
  }
1387
- return Date.now() as UnixTimestampMillis
1380
+ return localTime.nowUnixMillis()
1388
1381
  }
1389
1382
 
1390
1383
  protected logSaveStarted(op: string, items: any, table: string): UnixTimestampMillis {
@@ -1405,7 +1398,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
1405
1398
  this.cfg.logger?.log(...args)
1406
1399
  }
1407
1400
 
1408
- return Date.now() as UnixTimestampMillis
1401
+ return localTime.nowUnixMillis()
1409
1402
  }
1410
1403
  }
1411
1404