(table, { where, update, create })
| 461 | await query.execute(); |
| 462 | }, |
| 463 | async upsert(table, { where, update, create }) { |
| 464 | if (provider === "mssql") { |
| 465 | let query = kysely |
| 466 | .updateTable(table.names.sql) |
| 467 | .top(1) |
| 468 | .set(encodeValues(update, table, false)); |
| 469 | |
| 470 | if (where) query = query.where((b) => buildWhere(where, b, provider)); |
| 471 | const result = await query.executeTakeFirstOrThrow(); |
| 472 | |
| 473 | if (result.numUpdatedRows === 0n) |
| 474 | await this.createMany(table, [create]); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | const idColumn = table.getIdColumn(); |
| 479 | let query = kysely |
| 480 | .selectFrom(table.names.sql) |
| 481 | .select([`${idColumn.names.sql} as id`]); |
| 482 | if (where) query = query.where((b) => buildWhere(where, b, provider)); |
| 483 | const result = await query.limit(1).executeTakeFirst(); |
| 484 | |
| 485 | if (result) { |
| 486 | await kysely |
| 487 | .updateTable(table.names.sql) |
| 488 | .set(encodeValues(update, table, false)) |
| 489 | .where(idColumn.names.sql, "=", result.id) |
| 490 | .execute(); |
| 491 | } else { |
| 492 | await this.createMany(table, [create]); |
| 493 | } |
| 494 | }, |
| 495 | |
| 496 | async createMany(table, values) { |
| 497 | const encodedValues = values.map((v) => encodeValues(v, table, true)); |
nothing calls this directly
no test coverage detected