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

Function upsertMany

packages/core/fumadb/src/query/orm/index.ts:448–551  ·  view source on GitHub ↗
(name, { target, update, values })

Source from the content-addressed store, hash-verified

446 });
447 },
448 async upsertMany(name, { target, update, values }) {
449 const table = toTable(name);
450 if (values.length === 0) return;
451 if (target.length === 0) {
452 // oxlint-disable-next-line executor/no-try-catch-or-throw, executor/no-error-constructor -- boundary: public query rejects invalid upsert shape
453 throw new Error("[FumaDB] upsertMany requires at least one target column.");
454 }
455 if (update.length === 0) {
456 // oxlint-disable-next-line executor/no-try-catch-or-throw, executor/no-error-constructor -- boundary: public query rejects invalid upsert shape
457 throw new Error("[FumaDB] upsertMany requires at least one update column.");
458 }
459
460 const targetColumns = target.map((columnName) => {
461 const column = table.columns[columnName as string];
462 if (!column) throw new Error(`[FumaDB] unknown column name ${String(columnName)}.`);
463 return column;
464 });
465 const updateColumns = update.map((columnName) => {
466 const column = table.columns[columnName as string];
467 if (!column) throw new Error(`[FumaDB] unknown column name ${String(columnName)}.`);
468 return column;
469 });
470
471 const builder = createBuilder(table.columns);
472 const permittedRows: {
473 readonly value: Record<string, unknown>;
474 readonly where: Condition | undefined;
475 }[] = [];
476 for (const value of values) {
477 const updateValues = Object.fromEntries(
478 updateColumns.map((column) => [column.ormName, value[column.ormName]]),
479 );
480 const constrainedWhere = await applyUpdatePolicies(
481 table,
482 undefined,
483 updateValues,
484 context,
485 "upsert",
486 value,
487 );
488 if (constrainedWhere === false) continue;
489 await runCreatePolicies(table, value, context);
490 permittedRows.push({ value, where: constrainedWhere });
491 }
492 if (permittedRows.length === 0) return;
493
494 if (internal.upsertMany) {
495 const groups: {
496 readonly where: Condition | undefined;
497 readonly values: Record<string, unknown>[];
498 }[] = [];
499 for (const row of permittedRows) {
500 const group = groups.find((candidate) => conditionsEqual(candidate.where, row.where));
501 if (group) {
502 group.values.push(row.value);
503 } else {
504 groups.push({ where: row.where, values: [row.value] });
505 }

Callers

nothing calls this directly

Calls 9

createBuilderFunction · 0.90
toTableFunction · 0.85
applyUpdatePoliciesFunction · 0.85
runCreatePoliciesFunction · 0.85
conditionsEqualFunction · 0.85
runGroupsFunction · 0.85
builderFunction · 0.85
pushMethod · 0.80
transactionMethod · 0.65

Tested by

no test coverage detected