1- // Database connection utility for Nextlog PostgreSQL
2-
31import { Pool , PoolClient , QueryResult } from 'pg' ;
2+ import { logger } from './logger' ;
43
54// Global connection pool
65let pool : Pool ;
@@ -10,47 +9,51 @@ let pool: Pool;
109 */
1110function getPool ( ) : Pool {
1211 if ( ! pool ) {
12+ const sslConfig = process . env . DATABASE_SSL === 'true'
13+ ? { rejectUnauthorized : false }
14+ : false ;
15+
1316 pool = new Pool ( {
1417 connectionString : process . env . DATABASE_URL ,
15- ssl : process . env . NODE_ENV === 'production' ? { rejectUnauthorized : false } : false ,
18+ ssl : sslConfig ,
1619 max : 20 , // Maximum number of clients in the pool
17- idleTimeoutMillis : 30000 , // Close idle clients after 30 seconds
18- connectionTimeoutMillis : 2000 , // Return an error after 2 seconds if connection could not be established
20+ idleTimeoutMillis : 60000 , // Close idle clients after 60 seconds
21+ connectionTimeoutMillis : 10000 , // Return an error after 10 seconds if connection could not be established
22+ query_timeout : 30000 , // Query timeout: 30 seconds
23+ statement_timeout : 30000 , // Statement timeout: 30 seconds
1924 } ) ;
2025
2126 // Handle pool errors
2227 pool . on ( 'error' , ( err ) => {
23- console . error ( 'Unexpected error on idle client' , err ) ;
28+ logger . error ( 'Unexpected error on idle client' , err ) ;
2429 } ) ;
30+
31+ logger . info ( 'Database connection pool initialized' ) ;
2532 }
2633
2734 return pool ;
2835}
2936
37+
3038/**
3139 * Execute a query using the connection pool
3240 */
3341export async function query ( text : string , params ?: unknown [ ] ) : Promise < QueryResult > {
3442 const pool = getPool ( ) ;
3543 const start = Date . now ( ) ;
36-
44+
3745 try {
3846 const result = await pool . query ( text , params ) ;
3947 const duration = Date . now ( ) - start ;
40-
48+
4149 // Log slow queries (> 100ms)
4250 if ( duration > 100 ) {
43- console . warn ( 'Slow query detected:' , {
44- duration : `${ duration } ms` ,
45- text : text . substring ( 0 , 100 ) + ( text . length > 100 ? '...' : '' ) ,
46- params : params ?. length ? `${ params . length } params` : 'no params'
47- } ) ;
51+ logger . slowQuery ( duration , text , params ) ;
4852 }
49-
53+
5054 return result ;
5155 } catch ( error ) {
52- console . error ( 'Database query error:' , {
53- error : error instanceof Error ? error . message : 'Unknown error' ,
56+ logger . error ( 'Database query error' , error , {
5457 query : text . substring ( 0 , 100 ) + ( text . length > 100 ? '...' : '' ) ,
5558 params : params ?. length ? `${ params . length } params` : 'no params'
5659 } ) ;
@@ -73,7 +76,7 @@ export async function transaction<T>(
7376 callback : ( client : PoolClient ) => Promise < T >
7477) : Promise < T > {
7578 const client = await getClient ( ) ;
76-
79+
7780 try {
7881 await client . query ( 'BEGIN' ) ;
7982 const result = await callback ( client ) ;
@@ -98,13 +101,13 @@ export async function closePool(): Promise<void> {
98101
99102// Handle process termination
100103process . on ( 'SIGINT' , async ( ) => {
101- console . log ( ' Closing database pool... ') ;
104+ logger . info ( 'Shutting down: Closing database pool') ;
102105 await closePool ( ) ;
103106 process . exit ( 0 ) ;
104107} ) ;
105108
106109process . on ( 'SIGTERM' , async ( ) => {
107- console . log ( ' Closing database pool... ') ;
110+ logger . info ( 'Shutting down: Closing database pool') ;
108111 await closePool ( ) ;
109112 process . exit ( 0 ) ;
110113} ) ;
0 commit comments