Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit bb63447

Browse files
authored
fix: upsert v3 (#16)
1 parent fc87555 commit bb63447

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/mysql.db.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,18 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
329329
return
330330
}
331331

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

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

0 commit comments

Comments
 (0)