@naturalcycles/db-lib 9.24.2 → 9.26.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.
@@ -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,7 +978,19 @@ class CommonDao {
968
978
  }
969
979
  else {
970
980
  // Joi
981
+ const start = performance.now();
971
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
+ }
972
994
  error = vr.error;
973
995
  convertedValue = vr.value;
974
996
  }
@@ -1,4 +1,4 @@
1
- import { BaseDBEntity, CommonLogger, ErrorMode, Promisable, UnixTimestamp, ZodError, ZodSchema } from '@naturalcycles/js-lib';
1
+ import { AnyObject, 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';
@@ -111,6 +111,15 @@ export interface CommonDaoCfg<BM extends BaseDBEntity, DBM extends BaseDBEntity
111
111
  * If set to false - save (write) operations will skip validation (and conversion).
112
112
  */
113
113
  validateOnSave?: boolean;
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.
121
+ */
122
+ onValidationTimeThreshold?: (info: AnyObject) => void;
114
123
  /**
115
124
  * Defaults to false.
116
125
  * Setting it to true will set saveMethod to `insert` for save/saveBatch, which will
package/package.json CHANGED
@@ -43,9 +43,9 @@
43
43
  "url": "https://github.com/NaturalCycles/db-lib"
44
44
  },
45
45
  "engines": {
46
- "node": ">=20.13"
46
+ "node": ">=22.10.0"
47
47
  },
48
- "version": "9.24.2",
48
+ "version": "9.26.0",
49
49
  "description": "Lowest Common Denominator API to supported Databases",
50
50
  "keywords": [
51
51
  "db",
@@ -1,7 +1,9 @@
1
1
  import {
2
+ AnyObject,
2
3
  BaseDBEntity,
3
4
  CommonLogger,
4
5
  ErrorMode,
6
+ NumberOfMilliseconds,
5
7
  Promisable,
6
8
  UnixTimestamp,
7
9
  ZodError,
@@ -146,6 +148,17 @@ export interface CommonDaoCfg<
146
148
  */
147
149
  validateOnSave?: boolean
148
150
 
151
+ /**
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.
159
+ */
160
+ onValidationTimeThreshold?: (info: AnyObject) => void
161
+
149
162
  /**
150
163
  * Defaults to false.
151
164
  * Setting it to true will set saveMethod to `insert` for save/saveBatch, which will
@@ -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,7 +1267,23 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
1257
1267
  })
1258
1268
  } else {
1259
1269
  // Joi
1270
+ const start = performance.now()
1260
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
+ }
1286
+
1261
1287
  error = vr.error
1262
1288
  convertedValue = vr.value
1263
1289
  }