@naturalcycles/db-lib 9.25.0 → 9.26.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.
@@ -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'));
@@ -494,7 +494,12 @@ class CommonDao {
494
494
  return await this.patchByIdInTransaction(id, patch, opt);
495
495
  }
496
496
  let patched;
497
- const loaded = await this.getById(id, opt);
497
+ const loaded = await this.getById(id, {
498
+ // Skipping validation here for performance reasons.
499
+ // Validation is going to happen on save anyway, just down below.
500
+ skipValidation: true,
501
+ ...opt,
502
+ });
498
503
  if (loaded) {
499
504
  patched = { ...loaded, ...patch };
500
505
  if ((0, js_lib_1._deepJsonEquals)(loaded, patched)) {
@@ -545,7 +550,12 @@ class CommonDao {
545
550
  Object.assign(bm, patch);
546
551
  }
547
552
  else {
548
- const loaded = await this.getById(bm.id, opt);
553
+ const loaded = await this.getById(bm.id, {
554
+ // Skipping validation here for performance reasons.
555
+ // Validation is going to happen on save anyway, just down below.
556
+ skipValidation: true,
557
+ ...opt,
558
+ });
549
559
  if (loaded) {
550
560
  const loadedWithPatch = {
551
561
  ...loaded,
@@ -968,19 +978,14 @@ class CommonDao {
968
978
  }
969
979
  else {
970
980
  // Joi
971
- const start = performance.now();
981
+ const start = js_lib_1.localTime.nowUnixMillis();
972
982
  const vr = (0, nodejs_lib_1.getValidationResult)(obj, schema, objectName);
973
- const end = performance.now();
974
- const tookMillis = end - start;
975
- if (this.cfg.debugValidationTimeThreshhold &&
976
- tookMillis >= this.cfg.debugValidationTimeThreshhold) {
977
- this.cfg.onValidationTimeThreshold?.({
978
- tookMillis,
979
- error: !!vr.error,
980
- table,
981
- obj,
982
- });
983
- }
983
+ const tookMillis = js_lib_1.localTime.nowUnixMillis() - start;
984
+ this.cfg.onValidationTime?.({
985
+ tookMillis,
986
+ table,
987
+ obj,
988
+ });
984
989
  error = vr.error;
985
990
  convertedValue = vr.value;
986
991
  }
@@ -1063,7 +1068,7 @@ class CommonDao {
1063
1068
  if (this.cfg.logStarted || force) {
1064
1069
  this.cfg.logger?.log(`>> ${table}.${op}`);
1065
1070
  }
1066
- return Date.now();
1071
+ return js_lib_1.localTime.nowUnixMillis();
1067
1072
  }
1068
1073
  logSaveStarted(op, items, table) {
1069
1074
  if (this.cfg.logStarted) {
@@ -1083,7 +1088,7 @@ class CommonDao {
1083
1088
  }
1084
1089
  this.cfg.logger?.log(...args);
1085
1090
  }
1086
- return Date.now();
1091
+ return js_lib_1.localTime.nowUnixMillis();
1087
1092
  }
1088
1093
  }
1089
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.25.0",
48
+ "version": "9.26.1",
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
+ }
@@ -663,7 +663,12 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
663
663
  }
664
664
 
665
665
  let patched: BM
666
- const loaded = await this.getById(id, opt)
666
+ const loaded = await this.getById(id, {
667
+ // Skipping validation here for performance reasons.
668
+ // Validation is going to happen on save anyway, just down below.
669
+ skipValidation: true,
670
+ ...opt,
671
+ })
667
672
 
668
673
  if (loaded) {
669
674
  patched = { ...loaded, ...patch }
@@ -723,7 +728,12 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
723
728
  }
724
729
  Object.assign(bm, patch)
725
730
  } else {
726
- const loaded = await this.getById(bm.id as ID, opt)
731
+ const loaded = await this.getById(bm.id as ID, {
732
+ // Skipping validation here for performance reasons.
733
+ // Validation is going to happen on save anyway, just down below.
734
+ skipValidation: true,
735
+ ...opt,
736
+ })
727
737
 
728
738
  if (loaded) {
729
739
  const loadedWithPatch: BM = {
@@ -1257,22 +1267,15 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
1257
1267
  })
1258
1268
  } else {
1259
1269
  // Joi
1260
- const start = performance.now()
1270
+ const start = localTime.nowUnixMillis()
1261
1271
  const vr = getValidationResult(obj, schema, objectName)
1262
- const end = performance.now()
1263
- const tookMillis = end - start
1264
-
1265
- if (
1266
- this.cfg.debugValidationTimeThreshhold &&
1267
- tookMillis >= this.cfg.debugValidationTimeThreshhold
1268
- ) {
1269
- this.cfg.onValidationTimeThreshold?.({
1270
- tookMillis,
1271
- error: !!vr.error,
1272
- table,
1273
- obj,
1274
- })
1275
- }
1272
+ const tookMillis = localTime.nowUnixMillis() - start
1273
+
1274
+ this.cfg.onValidationTime?.({
1275
+ tookMillis,
1276
+ table,
1277
+ obj,
1278
+ })
1276
1279
 
1277
1280
  error = vr.error
1278
1281
  convertedValue = vr.value
@@ -1374,7 +1377,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
1374
1377
  if (this.cfg.logStarted || force) {
1375
1378
  this.cfg.logger?.log(`>> ${table}.${op}`)
1376
1379
  }
1377
- return Date.now() as UnixTimestampMillis
1380
+ return localTime.nowUnixMillis()
1378
1381
  }
1379
1382
 
1380
1383
  protected logSaveStarted(op: string, items: any, table: string): UnixTimestampMillis {
@@ -1395,7 +1398,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
1395
1398
  this.cfg.logger?.log(...args)
1396
1399
  }
1397
1400
 
1398
- return Date.now() as UnixTimestampMillis
1401
+ return localTime.nowUnixMillis()
1399
1402
  }
1400
1403
  }
1401
1404