import { CreateTableBuilder, Kysely, PostgresDialect, sql } from 'kysely';
import { type DB } from "./out/db";
import { Pool } from "pg";
import { env } from '../util';

export const db = new Kysely<DB>({
    dialect: new PostgresDialect({
        pool: new Pool({
            connectionString: env.DATABASE_URL,
        }),
    })
});

export function addDefaultColumns(ctb: CreateTableBuilder<any, any>) {
    return ctb
        .addColumn("id", "bigint", (cb) => cb
            .generatedAlwaysAsIdentity()
            .primaryKey()
        )
        .addColumn("created_at", "timestamptz", (cb) => cb
            .notNull()
            .defaultTo(sql`now()`)
        )
};
