30 lines
791 B
TypeScript
30 lines
791 B
TypeScript
import { Pool } from 'pg';
|
|
|
|
const pool = new Pool({
|
|
connectionString: process.env.DATABASE_URL || 'postgresql://agendapro:agendapro123@localhost:5432/agendapro',
|
|
max: 20,
|
|
idleTimeoutMillis: 30000,
|
|
connectionTimeoutMillis: 2000,
|
|
});
|
|
|
|
pool.on('error', (err) => {
|
|
console.error('Unexpected error on idle client', err);
|
|
});
|
|
|
|
export async function query(text: string, params?: unknown[]) {
|
|
const start = Date.now();
|
|
const res = await pool.query(text, params);
|
|
const duration = Date.now() - start;
|
|
if (process.env.NODE_ENV === 'development') {
|
|
console.log('Executed query', { text: text.substring(0, 80), duration, rows: res.rowCount });
|
|
}
|
|
return res;
|
|
}
|
|
|
|
export async function getClient() {
|
|
const client = await pool.connect();
|
|
return client;
|
|
}
|
|
|
|
export default pool;
|