stock.geolock.fr/apps/api/dist/db/index.js
2026-02-24 16:10:30 +00:00

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);
};