import { query } from "@/db.ts"; const id = "id uuid primary key default uuid_generate_v4()"; interface TableSpec { columns: string[]; additionalStatements?: string[]; prepStatements?: string[]; } const timestamps = [ "created_at timestamptz not null default now()", "updated_at timestamptz not null default now()", ]; const tables: Record = { "note": { columns: [id, "content text not null", ...timestamps], }, "user": { columns: [ id, "username text not null unique", "hashed_password text not null", "name text", ...timestamps, ], }, "team": { columns: [ id, "name text not null", ...timestamps, ], additionalStatements: [ 'create index name_idx on team ("name")', ], }, "team_user": { prepStatements: [ "drop type if exists team_user_status", "create type team_user_status as enum ('invited', 'accepted', 'owner')", ], columns: [ "team_id uuid", "user_id uuid", "status team_user_status", ...timestamps, ], additionalStatements: [ "create index team_user_idx on team_user (team_id) include (user_id)", "create index team_idx on team_user (team_id)", "create index user_idx on team_user (user_id)", "create index status_idx on team_user (status)", ], }, }; const tableStatements = Object.entries(tables).map(([name, meta]) => ` ${(meta.prepStatements || []).map((s) => `${s};`).join("\n")} -- TABLE ${name} drop table if exists "${name}"; create table "${name}" ( ${meta.columns.join(",\n ")} ); ${(meta.additionalStatements || []).map((s) => `${s};`).join("\n")} `); const queryString = ` create extension if not exists "uuid-ossp"; ${tableStatements.map((s) => s.trim()).join("\n\n")} `; console.log(queryString); await query(queryString);