@naturalcycles/db-lib 9.7.0 → 9.7.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.
@@ -86,7 +86,7 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
86
86
  * Similar to `save` with skipIfEquals.
87
87
  * Similar to `patch`, but doesn't load the object from the Database.
88
88
  */
89
- savePatch(bm: BM, patch: Partial<BM>, opt: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
89
+ savePatch(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM>;
90
90
  /**
91
91
  * Convenience method to replace 3 operations (loading+patching+saving) with one:
92
92
  *
@@ -154,10 +154,9 @@ export declare class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity
154
154
  anyToDBM(dbm?: any, opt?: CommonDaoOptions): DBM;
155
155
  anyToDBMs(entities: DBM[], opt?: CommonDaoOptions): DBM[];
156
156
  /**
157
- * Returns *converted value*.
158
- * Validates (unless `skipValidation=true` passed).
159
- *
157
+ * Returns *converted value* (NOT the same reference).
160
158
  * Does NOT mutate the object.
159
+ * Validates (unless `skipValidation=true` passed).
161
160
  */
162
161
  validateAndConvert<T>(obj: Partial<T>, schema: ObjectSchema<T> | AjvSchema<T> | ZodSchema<T> | undefined, opt?: CommonDaoOptions): any;
163
162
  getTableSchema(): Promise<JsonSchemaRootObject<DBM>>;
@@ -544,9 +544,15 @@ class CommonDao {
544
544
  */
545
545
  async save(bm, opt = {}) {
546
546
  this.requireWriteAccess();
547
- if (opt.skipIfEquals && (0, js_lib_1._deepJsonEquals)(bm, opt.skipIfEquals)) {
548
- // Skipping the save operation
549
- return bm;
547
+ if (opt.skipIfEquals) {
548
+ // We compare with convertedBM, to account for cases when some extra property is assigned to bm,
549
+ // which should be removed post-validation, but it breaks the "equality check"
550
+ // Post-validation the equality check should work as intended
551
+ const convertedBM = this.validateAndConvert(bm, this.cfg.bmSchema, opt);
552
+ if ((0, js_lib_1._deepJsonEquals)(convertedBM, opt.skipIfEquals)) {
553
+ // Skipping the save operation
554
+ return bm;
555
+ }
550
556
  }
551
557
  const idWasGenerated = !bm.id && this.cfg.generateId;
552
558
  this.assignIdCreatedUpdated(bm, opt); // mutates
@@ -820,13 +826,7 @@ class CommonDao {
820
826
  dbm = this.cfg.hooks.anonymize(dbm);
821
827
  }
822
828
  // DBM > BM
823
- let bm;
824
- if (this.cfg.hooks.beforeDBMToBM) {
825
- bm = await this.cfg.hooks.beforeDBMToBM(dbm);
826
- }
827
- else {
828
- bm = dbm;
829
- }
829
+ const bm = ((await this.cfg.hooks.beforeDBMToBM?.(dbm)) || dbm);
830
830
  // Validate/convert BM
831
831
  return this.validateAndConvert(bm, this.cfg.bmSchema, opt);
832
832
  }
@@ -836,21 +836,10 @@ class CommonDao {
836
836
  async bmToDBM(bm, opt) {
837
837
  if (bm === undefined)
838
838
  return;
839
- // should not do it on load, but only on save!
840
- // this.assignIdCreatedUpdated(bm, opt)
841
839
  // bm gets assigned to the new reference
842
840
  bm = this.validateAndConvert(bm, this.cfg.bmSchema, opt);
843
841
  // BM > DBM
844
- let dbm;
845
- if (this.cfg.hooks.beforeBMToDBM) {
846
- dbm = { ...(await this.cfg.hooks.beforeBMToDBM(bm)) };
847
- }
848
- else {
849
- dbm = bm;
850
- }
851
- // Validate/convert DBM
852
- // return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
853
- return dbm;
842
+ return ((await this.cfg.hooks.beforeBMToDBM?.(bm)) || bm);
854
843
  }
855
844
  async bmsToDBM(bms, opt = {}) {
856
845
  // try/catch?
@@ -875,10 +864,9 @@ class CommonDao {
875
864
  return entities.map(entity => this.anyToDBM(entity, opt));
876
865
  }
877
866
  /**
878
- * Returns *converted value*.
879
- * Validates (unless `skipValidation=true` passed).
880
- *
867
+ * Returns *converted value* (NOT the same reference).
881
868
  * Does NOT mutate the object.
869
+ * Validates (unless `skipValidation=true` passed).
882
870
  */
883
871
  validateAndConvert(obj, schema, opt = {}) {
884
872
  // Kirill 2021-10-18: I realized that there's little reason to keep removing null values
package/package.json CHANGED
@@ -40,7 +40,7 @@
40
40
  "engines": {
41
41
  "node": ">=18.12"
42
42
  },
43
- "version": "9.7.0",
43
+ "version": "9.7.2",
44
44
  "description": "Lowest Common Denominator API to supported Databases",
45
45
  "keywords": [
46
46
  "db",
@@ -612,7 +612,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
612
612
  * Similar to `save` with skipIfEquals.
613
613
  * Similar to `patch`, but doesn't load the object from the Database.
614
614
  */
615
- async savePatch(bm: BM, patch: Partial<BM>, opt: CommonDaoSaveBatchOptions<DBM>): Promise<BM> {
615
+ async savePatch(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM> {
616
616
  const patched: BM = {
617
617
  ...bm,
618
618
  ...patch,
@@ -732,9 +732,15 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
732
732
  async save(bm: Unsaved<BM>, opt: CommonDaoSaveOptions<BM, DBM> = {}): Promise<BM> {
733
733
  this.requireWriteAccess()
734
734
 
735
- if (opt.skipIfEquals && _deepJsonEquals(bm, opt.skipIfEquals)) {
736
- // Skipping the save operation
737
- return bm as BM
735
+ if (opt.skipIfEquals) {
736
+ // We compare with convertedBM, to account for cases when some extra property is assigned to bm,
737
+ // which should be removed post-validation, but it breaks the "equality check"
738
+ // Post-validation the equality check should work as intended
739
+ const convertedBM = this.validateAndConvert(bm as Partial<BM>, this.cfg.bmSchema, opt)
740
+ if (_deepJsonEquals(convertedBM, opt.skipIfEquals)) {
741
+ // Skipping the save operation
742
+ return bm as BM
743
+ }
738
744
  }
739
745
 
740
746
  const idWasGenerated = !bm.id && this.cfg.generateId
@@ -1083,15 +1089,9 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
1083
1089
  }
1084
1090
 
1085
1091
  // DBM > BM
1086
- let bm: Partial<BM>
1087
- if (this.cfg.hooks!.beforeDBMToBM) {
1088
- bm = await this.cfg.hooks!.beforeDBMToBM(dbm)
1089
- } else {
1090
- bm = dbm as any
1091
- }
1092
+ const bm = ((await this.cfg.hooks!.beforeDBMToBM?.(dbm)) || dbm) as Partial<BM>
1092
1093
 
1093
1094
  // Validate/convert BM
1094
-
1095
1095
  return this.validateAndConvert(bm, this.cfg.bmSchema, opt)
1096
1096
  }
1097
1097
 
@@ -1108,23 +1108,11 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
1108
1108
  async bmToDBM(bm?: BM, opt?: CommonDaoOptions): Promise<DBM | undefined> {
1109
1109
  if (bm === undefined) return
1110
1110
 
1111
- // should not do it on load, but only on save!
1112
- // this.assignIdCreatedUpdated(bm, opt)
1113
-
1114
1111
  // bm gets assigned to the new reference
1115
1112
  bm = this.validateAndConvert(bm, this.cfg.bmSchema, opt)
1116
1113
 
1117
1114
  // BM > DBM
1118
- let dbm: DBM
1119
- if (this.cfg.hooks!.beforeBMToDBM) {
1120
- dbm = { ...((await this.cfg.hooks!.beforeBMToDBM(bm!)) as DBM) }
1121
- } else {
1122
- dbm = bm as any
1123
- }
1124
-
1125
- // Validate/convert DBM
1126
- // return this.validateAndConvert(dbm, this.cfg.dbmSchema, DBModelType.DBM, opt)
1127
- return dbm
1115
+ return ((await this.cfg.hooks!.beforeBMToDBM?.(bm!)) || bm) as DBM
1128
1116
  }
1129
1117
 
1130
1118
  async bmsToDBM(bms: BM[], opt: CommonDaoOptions = {}): Promise<DBM[]> {
@@ -1158,10 +1146,9 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
1158
1146
  }
1159
1147
 
1160
1148
  /**
1161
- * Returns *converted value*.
1162
- * Validates (unless `skipValidation=true` passed).
1163
- *
1149
+ * Returns *converted value* (NOT the same reference).
1164
1150
  * Does NOT mutate the object.
1151
+ * Validates (unless `skipValidation=true` passed).
1165
1152
  */
1166
1153
  validateAndConvert<T>(
1167
1154
  obj: Partial<T>,