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

Function createMany

packages/core/fumadb/src/adapters/drizzle/query.ts:583–626  ·  view source on GitHub ↗
(table, values)

Source from the content-addressed store, hash-verified

581 },
582
583 async createMany(table, values) {
584 if (values.length === 0) return [];
585 const idField = table.getIdColumn().names.drizzle;
586 const drizzleTable = toDrizzle(table);
587 values = values.map((v) => mapValues(v, table));
588 const columnsPerRow = Math.max(1, Object.keys(values[0]!).length);
589 const batchSize = parameterBoundedBatchSize(table, columnsPerRow, 0, maxBoundParameters);
590 const batches: (typeof values)[] = [];
591 for (let i = 0; i < values.length; i += batchSize) {
592 batches.push(values.slice(i, i + batchSize));
593 }
594
595 const insertBatches = async (handle: typeof db): Promise<{ _id: unknown }[]> => {
596 if (provider === "sqlite" || provider === "postgresql") {
597 const out: { _id: unknown }[] = [];
598 for (const batch of batches) {
599 out.push(
600 ...(await (handle as unknown as P_DBType)
601 .insert(drizzleTable as unknown as P_TableType)
602 .values(batch)
603 .returning({
604 _id: (drizzleTable as unknown as P_TableType)[idField],
605 })),
606 );
607 }
608 return out;
609 }
610
611 const results: Record<string, unknown>[] = [];
612 for (const batch of batches) {
613 results.push(...(await handle.insert(drizzleTable).values(batch).$returningId()));
614 }
615 return results.map((result) => ({ _id: result[idField] }));
616 };
617
618 if (batches.length === 1) return insertBatches(db);
619 // One logical insert split into parameter-bounded statements must stay
620 // atomic: a later batch's constraint failure rolls back the earlier
621 // batches. (Engines without interactive transactions auto-commit each
622 // statement; `createMany` needs the inserted ids back, which the native
623 // batch API's result shape does not guarantee across drivers, so those
624 // engines keep sequential statements — their documented constraint.)
625 return runAtomically(insertBatches);
626 },
627
628 async deleteMany(table, v) {
629 const drizzleTable = toDrizzle(table);

Callers

nothing calls this directly

Calls 6

toDrizzleFunction · 0.85
insertBatchesFunction · 0.85
runAtomicallyFunction · 0.85
pushMethod · 0.80
mapValuesFunction · 0.70

Tested by

no test coverage detected