Skip to content

Commit

Permalink
feat: CommonDBSaveOptions.assignGeneratedIds
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed May 17, 2022
1 parent 789b5ed commit f781aab
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 132 deletions.
6 changes: 6 additions & 0 deletions src/commondao/common.dao.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export interface CommonDaoCfg<
*/
createId?: boolean

/**
* See the same option in CommonDB.
* Defaults to false normally.
*/
assignGeneratedIds?: boolean

/**
* Defaults to true
* Set to false to disable `created` field management.
Expand Down
9 changes: 7 additions & 2 deletions src/commondao/common.dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class CommonDao<
logLevel: isGAE || isCI ? CommonDaoLogLevel.NONE : CommonDaoLogLevel.OPERATIONS,
idType: 'string',
createId: true,
assignGeneratedIds: false,
created: true,
updated: true,
logger: console,
Expand Down Expand Up @@ -609,7 +610,7 @@ export class CommonDao<
*/
async save(bm: Unsaved<BM>, opt: CommonDaoSaveOptions<DBM> = {}): Promise<Saved<BM>> {
this.requireWriteAccess()
const idWasGenerated = !bm.id
const idWasGenerated = !bm.id && this.cfg.createId
this.assignIdCreatedUpdated(bm, opt) // mutates
const dbm = await this.bmToDBM(bm as BM, opt)
const table = opt.table || this.cfg.table
Expand All @@ -621,6 +622,7 @@ export class CommonDao<
const started = this.logSaveStarted(op, bm, table)
await this.cfg.db.saveBatch(table, [dbm], {
excludeFromIndexes: this.cfg.excludeFromIndexes,
assignGeneratedIds: this.cfg.assignGeneratedIds,
...opt,
})

Expand Down Expand Up @@ -667,7 +669,7 @@ export class CommonDao<
// assigning id in case it misses the id
// will override/set `updated` field, unless opts.preserveUpdated is set
if (!opt.raw) {
const idWasGenerated = !dbm.id
const idWasGenerated = !dbm.id && this.cfg.createId
this.assignIdCreatedUpdated(dbm, opt) // mutates
dbm = this.anyToDBM(dbm, opt)
if (opt.ensureUniqueId && idWasGenerated) await this.ensureUniqueId(table, dbm)
Expand All @@ -679,6 +681,7 @@ export class CommonDao<
const started = this.logSaveStarted(op, dbm, table)
await this.cfg.db.saveBatch(table, [dbm], {
excludeFromIndexes: this.cfg.excludeFromIndexes,
assignGeneratedIds: this.cfg.assignGeneratedIds,
...opt,
})
this.logSaveResult(started, op, table)
Expand Down Expand Up @@ -706,6 +709,7 @@ export class CommonDao<

await this.cfg.db.saveBatch(table, dbms, {
excludeFromIndexes: this.cfg.excludeFromIndexes,
assignGeneratedIds: this.cfg.assignGeneratedIds,
...opt,
})

Expand Down Expand Up @@ -736,6 +740,7 @@ export class CommonDao<

await this.cfg.db.saveBatch(table, dbms, {
excludeFromIndexes: this.cfg.excludeFromIndexes,
assignGeneratedIds: this.cfg.assignGeneratedIds,
...opt,
})

Expand Down
8 changes: 8 additions & 0 deletions src/db.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export interface CommonDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObje
* Default is `upsert`
*/
saveMethod?: CommonDBSaveMethod

/**
* Only applicable to tables where id is "auto-generated by DB", e.g `auto_increment` in MySQL.
* By default it's false, so, auto-generated id will NOT be assigned/returned.
* Setting it to true will assign and return auto-generated id (on all rows, one by one).
* It's not true by default, because getting auto-generated id incurs an overhead of doing extra call (e.g LAST_INSERT_ID() in MySQL).
*/
assignGeneratedIds?: boolean
}

export type CommonDBStreamOptions = CommonDBOptions
Expand Down
Loading

0 comments on commit f781aab

Please sign in to comment.