Skip to content

Commit

Permalink
fix: upsert v3 (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangibbsnc authored May 24, 2023
1 parent fc87555 commit bb63447
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/mysql.db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,18 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
return
}

const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
if (opt.saveMethod === 'update') {
// TODO: This fails if a combination of entities with id and without id are parsed
for await (const row of rows) {
// Update already existing
_assert(row.id, 'id is required for updating')
const query = new DBQuery(table).filterEq('id', row.id)
await this.updateByQuery(query, _omit(row, ['id']))
}
return
}

const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
// inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
const sqls = insertSQL(table, rows, verb, this.cfg.logger)

Expand Down

0 comments on commit bb63447

Please sign in to comment.