@naturalcycles/db-lib
Advanced tools
Comparing version 8.56.0 to 8.57.0
@@ -114,6 +114,12 @@ /// <reference types="node" /> | ||
* 2. Applies the patch on top of loaded data. | ||
* 3. Saves (as fast as possible since the read) with the Patch applied. | ||
* 3. Saves (as fast as possible since the read) with the Patch applied, but only if the data has changed. | ||
*/ | ||
patch(id: ID, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
patchAsDBM(id: ID, patch: Partial<DBM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<DBM>; | ||
patchById(id: ID, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
/** | ||
* Same as patchById, but takes the whole object as input. | ||
* This "whole object" is mutated with the patch and returned. | ||
* Otherwise, similar behavior as patchById. | ||
* It still loads the row from the DB. | ||
*/ | ||
patch(bm: Saved<BM>, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>>; | ||
saveAsDBM(dbm: DBM, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<DBM>; | ||
@@ -120,0 +126,0 @@ saveBatch(bms: Unsaved<BM>[], opt?: CommonDaoSaveBatchOptions<DBM>): Promise<Saved<BM>[]>; |
@@ -623,15 +623,12 @@ "use strict"; | ||
* 2. Applies the patch on top of loaded data. | ||
* 3. Saves (as fast as possible since the read) with the Patch applied. | ||
* 3. Saves (as fast as possible since the read) with the Patch applied, but only if the data has changed. | ||
*/ | ||
async patch(id, patch, opt = {}) { | ||
const bm = await this.getById(id, opt); | ||
async patchById(id, patch, opt = {}) { | ||
let patched; | ||
if (bm) { | ||
patched = { | ||
...bm, | ||
...patch, | ||
}; | ||
if ((0, js_lib_1._deepJsonEquals)(bm, patched)) { | ||
const loaded = await this.getById(id, opt); | ||
if (loaded) { | ||
patched = { ...loaded, ...patch }; | ||
if ((0, js_lib_1._deepJsonEquals)(loaded, patched)) { | ||
// Skipping the save operation, as data is the same | ||
return bm; | ||
return patched; | ||
} | ||
@@ -644,19 +641,26 @@ } | ||
} | ||
async patchAsDBM(id, patch, opt = {}) { | ||
const dbm = await this.getByIdAsDBM(id, opt); | ||
let patched; | ||
if (dbm) { | ||
patched = { | ||
...dbm, | ||
...patch, | ||
}; | ||
if ((0, js_lib_1._deepJsonEquals)(dbm, patched)) { | ||
/** | ||
* Same as patchById, but takes the whole object as input. | ||
* This "whole object" is mutated with the patch and returned. | ||
* Otherwise, similar behavior as patchById. | ||
* It still loads the row from the DB. | ||
*/ | ||
async patch(bm, patch, opt = {}) { | ||
(0, js_lib_1._assert)(bm.id, 'patch argument object should have an id', { | ||
bm, | ||
}); | ||
const loaded = await this.getById(bm.id, opt); | ||
if (loaded) { | ||
Object.assign(loaded, patch); | ||
if ((0, js_lib_1._deepJsonEquals)(loaded, bm)) { | ||
// Skipping the save operation, as data is the same | ||
return dbm; | ||
return bm; | ||
} | ||
// Make `bm` exactly the same as `loaded` | ||
(0, js_lib_1._objectAssignExact)(bm, loaded); | ||
} | ||
else { | ||
patched = this.create({ ...patch, id }, opt); | ||
Object.assign(bm, patch); | ||
} | ||
return await this.saveAsDBM(patched, opt); | ||
return await this.save(bm, opt); | ||
} | ||
@@ -663,0 +667,0 @@ async saveAsDBM(dbm, opt = {}) { |
@@ -43,3 +43,3 @@ { | ||
}, | ||
"version": "8.56.0", | ||
"version": "8.57.0", | ||
"description": "Lowest Common Denominator API to supported Databases", | ||
@@ -46,0 +46,0 @@ "keywords": [ |
@@ -8,2 +8,3 @@ import { Transform } from 'node:stream' | ||
_isTruthy, | ||
_objectAssignExact, | ||
_passthroughPredicate, | ||
@@ -848,5 +849,5 @@ _since, | ||
* 2. Applies the patch on top of loaded data. | ||
* 3. Saves (as fast as possible since the read) with the Patch applied. | ||
* 3. Saves (as fast as possible since the read) with the Patch applied, but only if the data has changed. | ||
*/ | ||
async patch( | ||
async patchById( | ||
id: ID, | ||
@@ -856,14 +857,11 @@ patch: Partial<BM>, | ||
): Promise<Saved<BM>> { | ||
const bm = await this.getById(id, opt) | ||
let patched: Saved<BM> | ||
const loaded = await this.getById(id, opt) | ||
if (bm) { | ||
patched = { | ||
...bm, | ||
...patch, | ||
} | ||
if (loaded) { | ||
patched = { ...loaded, ...patch } | ||
if (_deepJsonEquals(bm, patched)) { | ||
if (_deepJsonEquals(loaded, patched)) { | ||
// Skipping the save operation, as data is the same | ||
return bm | ||
return patched | ||
} | ||
@@ -877,25 +875,34 @@ } else { | ||
async patchAsDBM( | ||
id: ID, | ||
patch: Partial<DBM>, | ||
/** | ||
* Same as patchById, but takes the whole object as input. | ||
* This "whole object" is mutated with the patch and returned. | ||
* Otherwise, similar behavior as patchById. | ||
* It still loads the row from the DB. | ||
*/ | ||
async patch( | ||
bm: Saved<BM>, | ||
patch: Partial<BM>, | ||
opt: CommonDaoSaveBatchOptions<DBM> = {}, | ||
): Promise<DBM> { | ||
const dbm = await this.getByIdAsDBM(id, opt) | ||
let patched: DBM | ||
): Promise<Saved<BM>> { | ||
_assert(bm.id, 'patch argument object should have an id', { | ||
bm, | ||
}) | ||
if (dbm) { | ||
patched = { | ||
...dbm, | ||
...patch, | ||
} | ||
const loaded = await this.getById(bm.id, opt) | ||
if (_deepJsonEquals(dbm, patched)) { | ||
if (loaded) { | ||
Object.assign(loaded, patch) | ||
if (_deepJsonEquals(loaded, bm)) { | ||
// Skipping the save operation, as data is the same | ||
return dbm | ||
return bm | ||
} | ||
// Make `bm` exactly the same as `loaded` | ||
_objectAssignExact(bm, loaded) | ||
} else { | ||
patched = this.create({ ...patch, id } as Partial<BM>, opt) as any as DBM | ||
Object.assign(bm, patch) | ||
} | ||
return await this.saveAsDBM(patched, opt) | ||
return await this.save(bm, opt) | ||
} | ||
@@ -902,0 +909,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
416376
10889