ls-deno/db-migrations.ts

40 lines
878 B
TypeScript

import { query } from "./db.ts";
const id = "id uuid primary key default uuid_generate_v4()";
const timestamps = [
"created_at timestamptz not null default now()",
"updated_at timestamptz not null default now()",
];
const tables = {
"note": {
columns: [id, "content text not null", ...timestamps],
},
"user": {
columns: [
id,
"username text not null unique",
"hashed_password text not null",
...timestamps,
],
constraints: [],
},
};
const tableStatements = Object.entries(tables).map(([name, meta]) => `
drop table if exists "${name}";
create table "${name}" (
${meta.columns.join(",\n ")}
);
`);
console.log(tableStatements);
const queryString = `
create extension if not exists "uuid-ossp";
${tableStatements.map((s) => s.trim()).join("\n\n ")}
`;
console.log(queryString);
await query(queryString);