Aethel Bot OSS repository!
aethel.xyz
bot
fun
ai
discord
discord-bot
aethel
1import * as config from '@/config';
2import { Pool } from 'pg';
3import logger from './logger';
4
5if (!config.DATABASE_URL) {
6 throw new Error('DATABASE_URL environment variable is required');
7}
8
9const pool = new Pool({
10 connectionString: config.DATABASE_URL,
11 max: 20,
12 idleTimeoutMillis: 30000,
13 connectionTimeoutMillis: 2000,
14 maxUses: 7500,
15});
16
17pool.on('error', (err) => {
18 logger.error('Postgres Pool Error:', err);
19});
20
21pool.on('connect', () => {
22 logger.debug('New database connection established');
23});
24
25pool.on('remove', () => {
26 logger.debug('Database connection removed from pool');
27});
28
29process.on('SIGINT', async () => {
30 logger.info('Closing database pool...');
31 await pool.end();
32 process.exit(0);
33});
34
35process.on('SIGTERM', async () => {
36 logger.info('Closing database pool...');
37 await pool.end();
38 process.exit(0);
39});
40
41export default pool;