@naturalcycles/db-lib 9.7.0 → 9.7.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.
|
@@ -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
|
|
548
|
-
//
|
|
549
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
@@ -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
|
|
736
|
-
//
|
|
737
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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>,
|