Allow build without DATABASE_URL; only require it at runtime

This commit is contained in:
2026-06-27 17:03:13 -05:00
parent b2727fb298
commit 82fabe5d9b
3 changed files with 22 additions and 4 deletions
Regular → Executable
View File
+21 -3
View File
@@ -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);
+1 -1
View File
File diff suppressed because one or more lines are too long