@@ -3,11 +3,13 @@ import Config from '../config';
33import { Catch } from '../decorators/Catch' ;
44export class ConnectionPool {
55 private _pool : Pool ;
6+ private _isProduction : 'development' | 'production' ;
67 constructor ( ) {
78 const connectionString = this . getConnectionString ( ) ;
9+ this . _isProduction = Config . environment ;
810 this . _pool = new Pool ( {
911 connectionString,
10- ssl : { rejectUnauthorized : false } ,
12+ ssl : this . _isProduction === 'production' ? { rejectUnauthorized : false } : false ,
1113 } ) ;
1214 }
1315 @Catch ( {
@@ -17,17 +19,20 @@ export class ConnectionPool {
1719 } )
1820 async connect ( ) : Promise < void > {
1921 try {
20- await this . _pool . connect ( ) ;
22+ const client = await this . _pool . connect ( ) ;
23+ client . release ( ) ; // Verify and release immediately
2124 } catch ( error : any ) {
22- console . log ( ' error:', error . code ) ;
25+ console . error ( 'Database connection error:', error . message ) ;
2326 if ( error . code === '3D000' ) {
2427 console . log ( `Database does not exist. Creating database ${ Config . database . databaseName } ...` ) ;
2528 await this . createDatabase ( ) ;
2629 await this . _pool . end ( ) ; // End the current pool connection
2730 const newConnectionString = this . getConnectionString ( ) ;
2831 this . _pool = new Pool ( {
2932 connectionString : newConnectionString ,
33+ ssl : this . _isProduction === 'production' ? { rejectUnauthorized : false } : false ,
3034 } ) ;
35+ console . log ( 'Retrying connection after creating the database...' ) ;
3136 await this . _pool . connect ( ) ;
3237 }
3338 }
@@ -46,7 +51,12 @@ export class ConnectionPool {
4651 port,
4752 database : 'postgres' ,
4853 } ) ;
49- await client . query ( `CREATE DATABASE ${ databaseName } ` ) ;
54+ try {
55+ await client . query ( `CREATE DATABASE "${ databaseName } "` ) ;
56+ console . log ( `Database "${ databaseName } " created successfully.` ) ;
57+ } finally {
58+ await client . end ( ) ;
59+ }
5060 }
5161 private getConnectionString ( ) : string {
5262 const { user, host, databaseName, password, port, url } = Config . database ;
0 commit comments