import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { validateEnv } from "../utils/envValidator.js";

const env = validateEnv(process.env);
const dbURL = `postgres://${env.DATABASE_USER}:${env.DATABASE_PASSWORD}@${env.DATABASE_HOST}:${env.DATABASE_PORT}/${env.DATABASE_DB}`;

const queryClient = postgres(dbURL, {
    max: 10,
    idle_timeout: 60,
    connect_timeout: 30,
    max_lifetime: 1000 * 60 * 5, // 5 min time out
    onnotice: (notice) => {
        console.log("PG notice: ", notice.message);
    },
});

export const db = drizzle({ client: queryClient });
