2026-02-07 14:02:03 -05:00
|
|
|
// src/db/index.ts
|
|
|
|
|
import 'dotenv/config';
|
2026-02-15 15:25:08 -05:00
|
|
|
import { relations } from './relations.ts';
|
2026-03-11 19:18:45 -04:00
|
|
|
import { drizzle } from "drizzle-orm/node-postgres";
|
|
|
|
|
import { Pool } from "pg";
|
2026-02-07 14:02:03 -05:00
|
|
|
|
2026-03-11 19:18:45 -04:00
|
|
|
const pool = new Pool({
|
|
|
|
|
connectionString: process.env.DATABASE_URL,
|
|
|
|
|
max: 10,
|
|
|
|
|
idleTimeoutMillis: 30000,
|
|
|
|
|
connectionTimeoutMillis: 2000,
|
|
|
|
|
});
|
2026-02-13 21:28:33 -05:00
|
|
|
|
2026-03-11 19:18:45 -04:00
|
|
|
// Handle pool errors to prevent connection corruption
|
|
|
|
|
pool.on('error', (err) => {
|
|
|
|
|
console.error('Unexpected error on idle client', err);
|
|
|
|
|
});
|
2026-02-07 14:02:03 -05:00
|
|
|
|
2026-03-11 19:18:45 -04:00
|
|
|
export const db = drizzle({ client: pool, relations: relations, casing: 'snake_case' });
|
|
|
|
|
|
|
|
|
|
export const ClosePool = () => {
|
|
|
|
|
pool.end();
|
|
|
|
|
}
|