All files / sqlite create-data-migration.ts

100% Statements 9/9
100% Branches 0/0
100% Functions 7/7
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30                          16x 24x   46x 20x       120x 120x              
import { EOL } from "node:os";
 
import type { RelationalTable } from "../create-relational-structure";
import { getFullTableName } from "../utils";
import { escapeValue } from "./escape-value";
 
export function createDataMigration({
  tables,
  schemaName,
}: {
  tables: RelationalTable[];
  schemaName?: string | undefined;
}) {
  return tables
    .filter((table) => table.data.length > 0)
    .map((table) => {
      const keys = table.fields.map(({ key }) => key);
      return [
        `INSERT INTO ${getFullTableName({ tableName: table.name, schemaName })} (${keys.join(", ")}) VALUES`,
        `${table.data
          .map((row) => {
            const values = keys.map((key) => row[key]);
            return `  (${values.map((value) => escapeValue(value)).join(", ")})`;
          })
          .join(`,${EOL}`)};`,
      ].join(EOL);
    })
    .join(`${EOL}${EOL}`);
}