fix: DATABASE_URL error on setup

This commit is contained in:
Maze Winther
2025-06-27 16:56:59 +02:00
parent cdfb5ea7b0
commit 8fe09c83b1

View File

@ -2,18 +2,43 @@ import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres"; import postgres from "postgres";
import * as schema from "./schema"; import * as schema from "./schema";
if (!process.env.DATABASE_URL) { // Create a lazy database instance that only initializes when accessed
throw new Error("DATABASE_URL is not set"); let _db: ReturnType<typeof drizzle> | null = null;
function getDb() {
if (!process.env.DATABASE_URL) {
throw new Error("DATABASE_URL is not set");
}
if (!_db) {
const client = postgres(process.env.DATABASE_URL);
_db = drizzle(client, { schema });
}
return _db;
} }
// Create the postgres client // Export a proxy that forwards all calls to the actual db instance
const client = postgres(process.env.DATABASE_URL); export const db = new Proxy({} as ReturnType<typeof drizzle>, {
get(target, prop) {
// Create the drizzle instance return getDb()[prop as keyof typeof _db];
export const db = drizzle(client, { schema }); },
});
// Re-export schema for convenience // Re-export schema for convenience
export * from "./schema"; export * from "./schema";
// Re-export drizzle-orm functions to ensure version consistency // Re-export drizzle-orm functions to ensure version consistency
export { eq, and, or, not, isNull, isNotNull, inArray, notInArray, exists, notExists, sql } from "drizzle-orm"; export {
eq,
and,
or,
not,
isNull,
isNotNull,
inArray,
notInArray,
exists,
notExists,
sql,
} from "drizzle-orm";