24 lines
717 B
JavaScript
24 lines
717 B
JavaScript
import { drizzle } from 'drizzle-orm/node-postgres';
|
|
import pg from 'pg';
|
|
const { Pool } = pg;
|
|
let singletonPool = null;
|
|
export const getPool = () => singletonPool;
|
|
export const getDb = () => {
|
|
const databaseUrl = process.env.DATABASE_URL;
|
|
if (!databaseUrl) {
|
|
throw new Error('DATABASE_URL is required');
|
|
}
|
|
if (!singletonPool) {
|
|
singletonPool = new Pool({
|
|
connectionString: databaseUrl,
|
|
max: 10,
|
|
idleTimeoutMillis: 30_000,
|
|
connectionTimeoutMillis: 5_000
|
|
});
|
|
singletonPool.on('error', (err) => {
|
|
console.error('[db] Pool background error', err.message);
|
|
});
|
|
}
|
|
return drizzle(singletonPool);
|
|
};
|