MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / upsert

Function upsert

packages/core/fumadb/src/adapters/kysely/query.ts:463–494  ·  view source on GitHub ↗
(table, { where, update, create })

Source from the content-addressed store, hash-verified

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));

Callers

nothing calls this directly

Calls 5

encodeValuesFunction · 0.85
setMethod · 0.80
limitMethod · 0.80
buildWhereFunction · 0.70
executeMethod · 0.65

Tested by

no test coverage detected