Skip to content

Commit

Permalink
feat: replace CommonDao.savePatch with opt.skipDBRead
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Mar 7, 2024
1 parent 79253a8 commit e2b962b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
8 changes: 8 additions & 0 deletions src/commondao/common.dao.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ export interface CommonDaoSaveOptions<BM extends BaseDBEntity, DBM extends BaseD
skipIfEquals?: BM
}

export interface CommonDaoPatchOptions<DBM extends BaseDBEntity>
extends CommonDaoSaveBatchOptions<DBM> {
/**
* If true - patch will skip loading from DB, and will just optimistically patch passed object.
*/
skipDBRead?: boolean
}

/**
* All properties default to undefined.
*/
Expand Down
57 changes: 21 additions & 36 deletions src/commondao/common.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
CommonDaoHooks,
CommonDaoLogLevel,
CommonDaoOptions,
CommonDaoPatchOptions,
CommonDaoSaveBatchOptions,
CommonDaoSaveOptions,
CommonDaoStreamDeleteOptions,
Expand Down Expand Up @@ -604,31 +605,6 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
}

// SAVE
/**
* 1. Applies the patch
* 2. If object is the same after patching - skips saving it
* 3. Otherwise - saves the patched object and returns it
*
* Similar to `save` with skipIfEquals.
* Similar to `patch`, but doesn't load the object from the Database.
*/
async savePatch(bm: BM, patch: Partial<BM>, opt?: CommonDaoSaveBatchOptions<DBM>): Promise<BM> {
const patched: BM = {
...bm,
...patch,
}

if (_deepJsonEquals(bm, patched)) {
// Skipping the save operation, as data is the same
return bm
}

// Actually apply the patch by mutating the original object (by design)
Object.assign(bm, patch)

return await this.save(bm, opt)
}

/**
* Convenience method to replace 3 operations (loading+patching+saving) with one:
*
Expand Down Expand Up @@ -686,28 +662,37 @@ export class CommonDao<BM extends BaseDBEntity, DBM extends BaseDBEntity = BM> {
* Otherwise, similar behavior as patchById.
* It still loads the row from the DB.
*/
async patch(bm: BM, patch: Partial<BM>, opt: CommonDaoSaveBatchOptions<DBM> = {}): Promise<BM> {
async patch(bm: BM, patch: Partial<BM>, opt: CommonDaoPatchOptions<DBM> = {}): Promise<BM> {
if (this.cfg.patchInTransaction && !opt.tx) {
// patchInTransaction means that we should run this op in Transaction
// But if opt.tx is passed - means that we are already in a Transaction,
// and should just continue as-is
return await this.patchInTransaction(bm, patch, opt)
}

const loaded = await this.getById(bm.id, opt)

if (loaded) {
Object.assign(loaded, patch)

if (_deepJsonEquals(loaded, bm)) {
if (opt.skipDBRead) {
const bmBefore = _deepCopy(bm)
Object.assign(bm, patch)
if (_deepJsonEquals(bm, bmBefore)) {
// Skipping the save operation, as data is the same
return bm
}

// Make `bm` exactly the same as `loaded`
_objectAssignExact(bm, loaded)
} else {
Object.assign(bm, patch)
const loaded = await this.getById(bm.id, opt)

if (loaded) {
Object.assign(loaded, patch)

if (_deepJsonEquals(loaded, bm)) {
// Skipping the save operation, as data is the same
return bm
}

// Make `bm` exactly the same as `loaded`
_objectAssignExact(bm, loaded)
} else {
Object.assign(bm, patch)
}
}

return await this.save(bm, opt)
Expand Down

0 comments on commit e2b962b

Please sign in to comment.