@@ -5,13 +5,8 @@ function debug(message: string, ...args: any[]) {
55 process . stderr . write ( `DEBUG [Pools]: ${ message } ${ args . map ( arg => JSON . stringify ( arg ) ) . join ( ' ' ) } \n` ) ;
66}
77
8- // Connection pools for each environment
9- export const pools = new Map < string , Pool > ( ) ;
10-
11- debug ( 'Initializing database pools...' ) ;
12- debug ( 'Environment enum options:' , Object . values ( Environment . enum ) ) ;
13- debug ( 'Environment enum type:' , typeof Environment ) ;
14- debug ( 'Environment enum:' , Environment ) ;
8+ export const pools = new Map < Environment , Pool > ( ) ;
9+ let poolsInitialized = false ;
1510
1611// Map of environment to env var prefix
1712const ENV_PREFIX_MAP = {
@@ -21,59 +16,85 @@ const ENV_PREFIX_MAP = {
2116 production : 'PRODUCTION'
2217} as const ;
2318
24- // Initialize pools
25- Object . values ( Environment . enum ) . forEach ( ( env ) => {
26- const envPrefix = ENV_PREFIX_MAP [ env ] ;
27-
28- debug ( `=== Initializing pool for ${ env } environment ===` ) ;
29- debug ( `Using prefix: ${ envPrefix } ` ) ;
30- debug ( 'Environment variables:' ) ;
31- debug ( `HOST: ${ process . env [ `${ envPrefix } _DB_HOST` ] } ` ) ;
32- debug ( `USER: ${ process . env [ `${ envPrefix } _DB_USER` ] } ` ) ;
33- debug ( `DB: ${ process . env [ `${ envPrefix } _DB_NAME` ] } ` ) ;
34- debug ( `PASS: ${ process . env [ `${ envPrefix } _DB_PASS` ] ? 'set' : 'not set' } ` ) ;
35- debug ( `SSL: ${ process . env . MCP_MYSQL_SSL } ` ) ;
36-
37- const config : PoolOptions = {
38- host : process . env [ `${ envPrefix } _DB_HOST` ] ,
39- user : process . env [ `${ envPrefix } _DB_USER` ] ,
40- password : process . env [ `${ envPrefix } _DB_PASS` ] ,
41- database : process . env [ `${ envPrefix } _DB_NAME` ] ,
42- ssl : process . env . MCP_MYSQL_SSL === "true" ? { } : undefined ,
43- connectionLimit : 5 ,
44- enableKeepAlive : true ,
45- keepAliveInitialDelay : 10000 ,
46- } ;
19+ export function initializePools ( ) {
20+ if ( poolsInitialized ) {
21+ debug ( "Pools already initialized" ) ;
22+ return ;
23+ }
24+
25+ debug ( "Initializing database pools..." ) ;
26+ debug ( "Environment enum options:" , Object . values ( Environment . enum ) ) ;
27+
28+ Object . values ( Environment . enum ) . forEach ( ( env ) => {
29+ const envPrefix = ENV_PREFIX_MAP [ env ] ;
30+ const portEnv = process . env [ `${ envPrefix } _DB_PORT` ] ;
31+ const parsedPort = portEnv ? Number . parseInt ( portEnv , 10 ) : undefined ;
32+ const sslEnv = process . env [ `${ envPrefix } _DB_SSL` ] ?? process . env . MCP_MYSQL_SSL ;
33+
34+ debug ( `=== Initializing pool for ${ env } environment ===` ) ;
35+ debug ( `Using prefix: ${ envPrefix } ` ) ;
36+ debug ( `HOST: ${ process . env [ `${ envPrefix } _DB_HOST` ] } ` ) ;
37+ debug ( `USER: ${ process . env [ `${ envPrefix } _DB_USER` ] } ` ) ;
38+ debug ( `DB: ${ process . env [ `${ envPrefix } _DB_NAME` ] } ` ) ;
39+ debug ( `PASS: ${ process . env [ `${ envPrefix } _DB_PASS` ] ? "set" : "not set" } ` ) ;
40+ debug ( `PORT: ${ portEnv ?? "default" } ` ) ;
41+ debug ( `SSL: ${ sslEnv } ` ) ;
42+
43+ const config : PoolOptions = {
44+ host : process . env [ `${ envPrefix } _DB_HOST` ] ,
45+ user : process . env [ `${ envPrefix } _DB_USER` ] ,
46+ password : process . env [ `${ envPrefix } _DB_PASS` ] ,
47+ database : process . env [ `${ envPrefix } _DB_NAME` ] ,
48+ ssl : sslEnv === "true" ? { } : undefined ,
49+ connectionLimit : 5 ,
50+ enableKeepAlive : true ,
51+ keepAliveInitialDelay : 10000 ,
52+ } ;
53+
54+ if ( portEnv && Number . isNaN ( parsedPort ) ) {
55+ debug ( `Invalid port for ${ env } , skipping configured value:` , portEnv ) ;
56+ } else if ( parsedPort !== undefined ) {
57+ config . port = parsedPort ;
58+ }
4759
48- if ( config . host && config . user && config . password && config . database ) {
49- debug ( `Creating pool for ${ env } with config:` , {
50- host : config . host ,
51- user : config . user ,
52- database : config . database ,
53- ssl : config . ssl ,
54- hasPassword : ! ! config . password
55- } ) ;
56-
60+ if ( config . host && config . user && config . password && config . database ) {
61+ debug ( `Creating pool for ${ env } with config:` , {
62+ host : config . host ,
63+ user : config . user ,
64+ database : config . database ,
65+ port : config . port ?? 3306 ,
66+ ssl : config . ssl ,
67+ hasPassword : ! ! config . password ,
68+ } ) ;
69+
70+ try {
71+ pools . set ( env , createPool ( config ) ) ;
72+ debug ( `Pool created successfully for ${ env } ` ) ;
73+ } catch ( error ) {
74+ debug ( `Error creating pool for ${ env } :` , error ) ;
75+ }
76+ } else {
77+ debug ( `Missing configuration for ${ env } :` , {
78+ hasHost : ! ! config . host ,
79+ hasUser : ! ! config . user ,
80+ hasPass : ! ! config . password ,
81+ hasDB : ! ! config . database ,
82+ } ) ;
83+ }
84+ } ) ;
85+
86+ poolsInitialized = true ;
87+ debug ( "Pools map keys:" , Array . from ( pools . keys ( ) ) ) ;
88+ }
89+
90+ export async function closePools ( ) {
91+ for ( const [ env , pool ] of pools . entries ( ) ) {
5792 try {
58- const pool = createPool ( config ) ;
59- pools . set ( env , pool ) ;
60- debug ( `Pool created successfully for ${ env } ` ) ;
61- debug ( `Pool type for ${ env } :` , typeof pool ) ;
62- debug ( `Pool methods for ${ env } :` , Object . keys ( pool ) ) ;
93+ debug ( `Closing pool for ${ env } ...` ) ;
94+ await pool . end ( ) ;
95+ debug ( `Pool for ${ env } closed successfully` ) ;
6396 } catch ( error ) {
64- debug ( `Error creating pool for ${ env } :` , error ) ;
97+ debug ( `Error closing pool for ${ env } :` , error ) ;
6598 }
66- } else {
67- debug ( `Missing configuration for ${ env } :` , {
68- hasHost : ! ! config . host ,
69- hasUser : ! ! config . user ,
70- hasPass : ! ! config . password ,
71- hasDB : ! ! config . database
72- } ) ;
7399 }
74- } ) ;
75-
76- debug ( 'Final pools state:' ) ;
77- debug ( 'Pools map keys:' , Array . from ( pools . keys ( ) ) ) ;
78- debug ( 'Pools map size:' , pools . size ) ;
79- debug ( 'Pools map entries:' , Array . from ( pools . entries ( ) ) . map ( ( [ env ] ) => env ) ) ;
100+ }
0 commit comments