Files
Sahaj Jain d67e460673 fix: make database connection lazy to allow dev without DATABASE_URL
Pages that don't use the database will now work without the env var set.
DB-dependent features will show a clear error only when accessed.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 20:41:18 +05:30

20 lines
583 B
TypeScript

import { drizzle, type NeonHttpDatabase } from "drizzle-orm/neon-http";
import { neon } from "@neondatabase/serverless";
let _db: NeonHttpDatabase | null = null;
export const db = new Proxy({} as NeonHttpDatabase, {
get(_target, prop) {
if (!_db) {
if (!process.env.DATABASE_URL) {
throw new Error(
"DATABASE_URL is not set. Database features are disabled in local development without a database."
);
}
const sql = neon(process.env.DATABASE_URL);
_db = drizzle({ client: sql });
}
return (_db as any)[prop];
},
});