11import { Pool , PoolClient } from 'pg' ;
22import Config from '../config' ;
3+ import logger from '../utils/logger' ;
34export class ConnectionPool {
45 private _pool : Pool ;
56 private _isProduction : 'development' | 'production' ;
@@ -8,19 +9,30 @@ export class ConnectionPool {
89 this . _isProduction = Config . environment ;
910 this . _pool = this . initializePool ( connectionString ) ;
1011 }
11- async connect ( ) : Promise < void > {
12+ async connect ( ) : Promise < boolean > {
1213 try {
1314 const client = await this . _pool . connect ( ) ;
14- client . release ( ) ; // Connection successful
15+ client . release ( ) ;
16+ logger . info ( 'Database connection successful' ) ;
17+ return true ;
1518 } catch ( error : any ) {
1619 console . error ( 'Database connection error:' , error . message ) ;
1720 if ( error . code === '3D000' ) {
1821 console . log ( `Database does not exist. Creating database ${ Config . database . databaseName } ...` ) ;
1922 await this . createDatabase ( ) ;
2023 await this . reinitializePool ( ) ;
21- await this . _pool . connect ( ) ;
24+ try {
25+ const client = await this . _pool . connect ( ) ;
26+ client . release ( ) ;
27+ logger . info ( 'Database connection successful after reinitialization' ) ;
28+ return true ;
29+ } catch ( reconnectError : any ) {
30+ console . error ( 'Reconnection failed:' , reconnectError . message ) ;
31+ return false ;
32+ }
2233 } else {
2334 console . error ( 'Unexpected error connecting to the database:' , error ) ;
35+ return false ;
2436 }
2537 }
2638 }
@@ -56,8 +68,8 @@ export class ConnectionPool {
5668 connectionString,
5769 ssl : this . _isProduction === 'production' ? { rejectUnauthorized : false } : false ,
5870 max : 20 ,
59- idleTimeoutMillis : 10000 ,
60- connectionTimeoutMillis : 5000 ,
71+ idleTimeoutMillis : 30000 , // Increase idle timeout
72+ connectionTimeoutMillis : 10000 , // Increase connection timeout
6173 keepAlive : true ,
6274 } ) ;
6375 }
0 commit comments