ls-deno/db-migrations.ts

40 lines
878 B
TypeScript
Raw Normal View History

2022-09-27 15:49:41 -05:00
import { query } from "./db.ts";
2022-09-30 15:14:57 -05:00
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: [],
},
};
2022-09-27 15:49:41 -05:00
2022-09-30 15:14:57 -05:00
const tableStatements = Object.entries(tables).map(([name, meta]) => `
drop table if exists "${name}";
create table "${name}" (
${meta.columns.join(",\n ")}
2022-09-27 15:49:41 -05:00
);
`);
2022-09-30 15:14:57 -05:00
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);