try db lazy loading for coolify build
All checks were successful
Lint / Lint and Check (push) Successful in 45s
All checks were successful
Lint / Lint and Check (push) Successful in 45s
This commit is contained in:
@@ -1,13 +1,28 @@
|
|||||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||||
|
import type { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||||
import * as schema from './schema';
|
import * as schema from './schema';
|
||||||
import '@/lib/env-config';
|
import '@/lib/env-config';
|
||||||
|
|
||||||
const DATABASE_URL = process.env.POSTGRES_URL;
|
let _db: NodePgDatabase<typeof schema> | null = null;
|
||||||
if (!DATABASE_URL) {
|
|
||||||
throw new Error('POSTGRES_URL environment variable is required');
|
function getDb() {
|
||||||
|
if (!_db) {
|
||||||
|
const DATABASE_URL = process.env.POSTGRES_URL;
|
||||||
|
if (!DATABASE_URL) {
|
||||||
|
throw new Error('POSTGRES_URL environment variable is required');
|
||||||
|
}
|
||||||
|
_db = drizzle(DATABASE_URL, { schema });
|
||||||
|
}
|
||||||
|
return _db;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const db = drizzle(DATABASE_URL, { schema });
|
export const db = new Proxy({} as NodePgDatabase<typeof schema>, {
|
||||||
|
get(target, prop) {
|
||||||
|
const database = getDb();
|
||||||
|
const value = database[prop as keyof typeof database];
|
||||||
|
return typeof value === 'function' ? value.bind(database) : value;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
// Re-export schema types for convenience
|
// Re-export schema types for convenience
|
||||||
export * from './schema';
|
export * from './schema';
|
||||||
|
|||||||
Reference in New Issue
Block a user