@naturalcycles/db-lib 9.26.3 → 9.27.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.
|
@@ -509,7 +509,7 @@ class CommonDao {
|
|
|
509
509
|
}
|
|
510
510
|
else {
|
|
511
511
|
const table = opt.table || this.cfg.table;
|
|
512
|
-
(0, js_lib_1._assert)(
|
|
512
|
+
(0, js_lib_1._assert)(opt.createIfMissing, `DB row required, but not found in ${table}`, {
|
|
513
513
|
id,
|
|
514
514
|
table,
|
|
515
515
|
});
|
|
@@ -550,26 +550,21 @@ class CommonDao {
|
|
|
550
550
|
Object.assign(bm, patch);
|
|
551
551
|
}
|
|
552
552
|
else {
|
|
553
|
-
const loaded = await this.
|
|
553
|
+
const loaded = await this.requireById(bm.id, {
|
|
554
554
|
// Skipping validation here for performance reasons.
|
|
555
555
|
// Validation is going to happen on save anyway, just down below.
|
|
556
556
|
skipValidation: true,
|
|
557
557
|
...opt,
|
|
558
558
|
});
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
return bm;
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
else {
|
|
572
|
-
Object.assign(bm, patch);
|
|
559
|
+
const loadedWithPatch = {
|
|
560
|
+
...loaded,
|
|
561
|
+
...patch,
|
|
562
|
+
};
|
|
563
|
+
// Make `bm` exactly the same as `loadedWithPatch`
|
|
564
|
+
(0, js_lib_1._objectAssignExact)(bm, loadedWithPatch);
|
|
565
|
+
if ((0, js_lib_1._deepJsonEquals)(loaded, loadedWithPatch)) {
|
|
566
|
+
// Skipping the save operation, as data is the same
|
|
567
|
+
return bm;
|
|
573
568
|
}
|
|
574
569
|
}
|
|
575
570
|
return await this.save(bm, opt);
|
|
@@ -227,16 +227,18 @@ export interface CommonDaoSaveOptions<BM extends BaseDBEntity, DBM extends BaseD
|
|
|
227
227
|
export interface CommonDaoPatchByIdOptions<DBM extends BaseDBEntity> extends CommonDaoSaveBatchOptions<DBM> {
|
|
228
228
|
/**
|
|
229
229
|
* Defaults to false.
|
|
230
|
-
* With false, if the row doesn't exist - it will
|
|
231
|
-
* With true, if the row doesn't exist - it will
|
|
230
|
+
* With false, if the row doesn't exist - it will throw an error.
|
|
231
|
+
* With true, if the row doesn't exist - it will be auto-created with `dao.create`.
|
|
232
232
|
*
|
|
233
233
|
* Use true when you expect the row to exist and it would be an error if it doesn't.
|
|
234
234
|
*/
|
|
235
|
-
|
|
235
|
+
createIfMissing?: boolean;
|
|
236
236
|
}
|
|
237
237
|
export interface CommonDaoPatchOptions<DBM extends BaseDBEntity> extends CommonDaoSaveBatchOptions<DBM> {
|
|
238
238
|
/**
|
|
239
239
|
* If true - patch will skip loading from DB, and will just optimistically patch passed object.
|
|
240
|
+
*
|
|
241
|
+
* Consequently, when the row doesn't exist - it will be auto-created with `dao.create`.
|
|
240
242
|
*/
|
|
241
243
|
skipDBRead?: boolean;
|
|
242
244
|
}
|
package/package.json
CHANGED
|
@@ -285,18 +285,20 @@ export interface CommonDaoPatchByIdOptions<DBM extends BaseDBEntity>
|
|
|
285
285
|
extends CommonDaoSaveBatchOptions<DBM> {
|
|
286
286
|
/**
|
|
287
287
|
* Defaults to false.
|
|
288
|
-
* With false, if the row doesn't exist - it will
|
|
289
|
-
* With true, if the row doesn't exist - it will
|
|
288
|
+
* With false, if the row doesn't exist - it will throw an error.
|
|
289
|
+
* With true, if the row doesn't exist - it will be auto-created with `dao.create`.
|
|
290
290
|
*
|
|
291
291
|
* Use true when you expect the row to exist and it would be an error if it doesn't.
|
|
292
292
|
*/
|
|
293
|
-
|
|
293
|
+
createIfMissing?: boolean
|
|
294
294
|
}
|
|
295
295
|
|
|
296
296
|
export interface CommonDaoPatchOptions<DBM extends BaseDBEntity>
|
|
297
297
|
extends CommonDaoSaveBatchOptions<DBM> {
|
|
298
298
|
/**
|
|
299
299
|
* If true - patch will skip loading from DB, and will just optimistically patch passed object.
|
|
300
|
+
*
|
|
301
|
+
* Consequently, when the row doesn't exist - it will be auto-created with `dao.create`.
|
|
300
302
|
*/
|
|
301
303
|
skipDBRead?: boolean
|
|
302
304
|
}
|
|
@@ -679,7 +679,7 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
679
679
|
}
|
|
680
680
|
} else {
|
|
681
681
|
const table = opt.table || this.cfg.table
|
|
682
|
-
_assert(
|
|
682
|
+
_assert(opt.createIfMissing, `DB row required, but not found in ${table}`, {
|
|
683
683
|
id,
|
|
684
684
|
table,
|
|
685
685
|
})
|
|
@@ -728,28 +728,24 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM, I
|
|
|
728
728
|
}
|
|
729
729
|
Object.assign(bm, patch)
|
|
730
730
|
} else {
|
|
731
|
-
const loaded = await this.
|
|
731
|
+
const loaded = await this.requireById(bm.id as ID, {
|
|
732
732
|
// Skipping validation here for performance reasons.
|
|
733
733
|
// Validation is going to happen on save anyway, just down below.
|
|
734
734
|
skipValidation: true,
|
|
735
735
|
...opt,
|
|
736
736
|
})
|
|
737
737
|
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
}
|
|
738
|
+
const loadedWithPatch: BM = {
|
|
739
|
+
...loaded,
|
|
740
|
+
...patch,
|
|
741
|
+
}
|
|
743
742
|
|
|
744
|
-
|
|
745
|
-
|
|
743
|
+
// Make `bm` exactly the same as `loadedWithPatch`
|
|
744
|
+
_objectAssignExact(bm, loadedWithPatch)
|
|
746
745
|
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
}
|
|
751
|
-
} else {
|
|
752
|
-
Object.assign(bm, patch)
|
|
746
|
+
if (_deepJsonEquals(loaded, loadedWithPatch)) {
|
|
747
|
+
// Skipping the save operation, as data is the same
|
|
748
|
+
return bm
|
|
753
749
|
}
|
|
754
750
|
}
|
|
755
751
|
|