33 lines
983 B
TypeScript
33 lines
983 B
TypeScript
import { Pool } from 'pg';
|
|
|
|
function createPool() {
|
|
const connectionString = process.env.DATABASE_URL;
|
|
|
|
if (!connectionString) {
|
|
if (typeof process.env.NEXT_PHASE === 'string' && process.env.NEXT_PHASE.includes('phase-production-build')) {
|
|
// Next.js collects page data during the build. We don't need a real DB for that.
|
|
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;
|
|
}
|
|
throw new Error('DATABASE_URL environment variable is not set');
|
|
}
|
|
|
|
return new Pool({ connectionString });
|
|
}
|
|
|
|
export const db = createPool();
|
|
|
|
export async function query(text: string, params?: unknown[]) {
|
|
const result = await db.query(text, params);
|
|
return result;
|
|
}
|