import { drizzle } from "drizzle-orm/neon-http"; import { neon } from "@neondatabase/serverless"; import * as schema from "./schema"; type DrizzleDb = ReturnType; function createDb() { const url = process.env.DATABASE_URL; if (!url) { throw new Error("DATABASE_URL environment variable is not set"); } const sql = neon(url); return drizzle({ client: sql, schema }); } // Lazy singleton — created on first use, not at module load time let _db: DrizzleDb | undefined; export const db = new Proxy({} as DrizzleDb, { get(_target, prop: string | symbol) { if (!_db) _db = createDb(); return (_db as unknown as Record)[prop]; }, });