Allow build without DATABASE_URL; only require it at runtime
This commit is contained in:
+21
-3
@@ -2,11 +2,29 @@ import { Pool } from 'pg';
|
||||
|
||||
const connectionString = process.env.DATABASE_URL;
|
||||
|
||||
if (!connectionString) {
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
function createPool() {
|
||||
if (!connectionString) {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
}
|
||||
// During build we don't need a real connection; return a stubbed pool
|
||||
// that throws clearly if someone actually tries to query.
|
||||
return {
|
||||
query: async () => {
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
},
|
||||
connect: async () => {
|
||||
throw new Error('DATABASE_URL environment variable is not set');
|
||||
},
|
||||
end: async () => {},
|
||||
on: () => {},
|
||||
removeListener: () => {},
|
||||
} as unknown as Pool;
|
||||
}
|
||||
return new Pool({ connectionString });
|
||||
}
|
||||
|
||||
export const db = new Pool({ connectionString });
|
||||
export const db = createPool();
|
||||
|
||||
export async function query(text: string, params?: unknown[]) {
|
||||
const result = await db.query(text, params);
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user