@@ -4,31 +4,156 @@ import { MYSQL } from "../../index.js";
44
55const pools = new Map < string , mysql . Pool > ( ) ;
66
7+ /**
8+ * Creates a MySQL connection with the given configuration.
9+ *
10+ * @param config - The configuration options for the MySQL connection.
11+ *
12+ * @returns The MySQL connection.
13+ */
714export const createConnection = async ( config : mysql . ConnectionOptions ) : Promise < mysql . Connection > => {
815 return mysql . createConnection ( config ) ;
916} ;
10- export const getStandardPool = ( config : MYSQL . ModelTypes . TDBCreds , poolName = "00" ) : mysql . Pool => {
11- const { database, host, password, port, user } = config ;
17+
18+ /**
19+ * Retrieves or creates a standard connection pool.
20+ *
21+ * @param config - The database credentials configuration object.
22+ * @param [poolName="00"] - The name suffix for the pool, default is "00".
23+ *
24+ * @returns The retrieved or newly created standard connection pool.
25+ */
26+ export const getStandardPool = (
27+ config : MYSQL . ModelTypes . TDBCreds ,
28+ poolName : string = "00" ,
29+ ) : mysql . Pool => {
1230 const poolNameResult = "st" + poolName ;
13- const credsString = `${ poolNameResult } #${ user } :${ password } @${ host } :${ port } /${ database } ` ;
1431
15- const poolCandidate = pools . get ( credsString ) ;
32+ return getPool ( config , poolNameResult ) ;
33+ } ;
1634
17- if ( poolCandidate ) {
18- return poolCandidate ;
19- } else {
20- const pool = mysql . createPool ( config ) ;
35+ /**
36+ * Retrieves or creates a transaction-specific connection pool.
37+ *
38+ * @param config - The database credentials configuration object.
39+ * @param [poolName="00"] - The name suffix for the pool, default is "00".
40+ *
41+ * @returns The retrieved or newly created transaction connection pool.
42+ */
43+ export const getTransactionPool = (
44+ config : MYSQL . ModelTypes . TDBCreds ,
45+ poolName : string = "00" ,
46+ ) : mysql . Pool => {
47+ const poolNameResult = "tr" + poolName ;
2148
22- pools . set ( credsString , pool ) ;
49+ return getPool ( config , poolNameResult ) ;
50+ } ;
2351
24- return pool ;
52+ /**
53+ * Removes and closes a standard connection pool.
54+ *
55+ * @param config - The database credentials configuration object.
56+ * @param [poolName="00"] - The name suffix for the pool, default is "00".
57+ *
58+ * @returns A promise that resolves when the pool is closed.
59+ */
60+ export const removeStandardPool = async (
61+ config : MYSQL . ModelTypes . TDBCreds ,
62+ poolName : string = "00" ,
63+ ) : Promise < void > => {
64+ const poolNameResult = "st" + poolName ;
65+
66+ return removePool ( config , poolNameResult ) ;
67+ } ;
68+
69+ /**
70+ * Removes and closes a transaction-specific connection pool.
71+ *
72+ * @param config - The database credentials configuration object.
73+ * @param [poolName="00"] - The name suffix for the pool, default is "00".
74+ *
75+ * @returns A promise that resolves when the pool is closed.
76+ */
77+ export const removeTransactionPool = async (
78+ config : MYSQL . ModelTypes . TDBCreds ,
79+ poolName : string = "00" ,
80+ ) : Promise < void > => {
81+ const poolNameResult = "tr" + poolName ;
82+
83+ return removePool ( config , poolNameResult ) ;
84+ } ;
85+
86+ /**
87+ * Gracefully shuts down all active connection pools.
88+ *
89+ * @returns A promise that resolves when all pools are closed.
90+ */
91+ export const shutdown = async ( ) : Promise < void > => {
92+ const poolShutdowns : Promise < void > [ ] = [ ] ;
93+
94+ for ( const [ _credsString , pool ] of pools ) {
95+ poolShutdowns . push ( pool . end ( ) ) ;
2596 }
97+
98+ try {
99+ await Promise . all ( poolShutdowns ) ;
100+ } finally {
101+ pools . clear ( ) ;
102+ }
103+ } ;
104+
105+ /**
106+ * Creates a connection credentials string.
107+ * This string is used as a key in the pools map to identify different pools.
108+ *
109+ * @param poolName - The name prefix for the pool.
110+ * @param creds - The credentials object.
111+ * @param creds.user - The database user.
112+ * @param creds.password - The database password.
113+ * @param creds.host - The database host.
114+ * @param creds.port - The database port.
115+ * @param creds.database - The database name.
116+ *
117+ * @returns The constructed credentials string.
118+ */
119+ const createCredsString = (
120+ poolName : string ,
121+ creds : { user : string ; password : string ; host : string ; port : number ; database : string ; } ,
122+ ) : string => {
123+ return `${ poolName } #${ creds . user } :${ creds . password } @${ creds . host } :${ creds . port } /${ creds . database } ` ;
124+ } ;
125+
126+ /**
127+ * Masks sensitive information in a connection string.
128+ *
129+ * @param credsString - The connection credentials string.
130+ *
131+ * @returns The credentials string with sensitive information masked.
132+ */
133+ const _maskSensitiveInfo = ( credsString : string ) : string => {
134+ return credsString . replace ( / : .+ @ / , ":<hidden>@" ) ;
26135} ;
27136
28- export const getTransactionPool = ( config : MYSQL . ModelTypes . TDBCreds , poolName = "00" ) : mysql . Pool => {
137+ /**
138+ * Retrieves or creates a named connection pool.
139+ *
140+ * @param config - The database credentials configuration object.
141+ * @param poolName - The name suffix for the pool.
142+ *
143+ * @returns The retrieved or newly created standard connection pool.
144+ */
145+ const getPool = (
146+ config : MYSQL . ModelTypes . TDBCreds ,
147+ poolName : string ,
148+ ) : mysql . Pool => {
29149 const { database, host, password, port, user } = config ;
30- const poolNameResult = "tr" + poolName ;
31- const credsString = `${ poolNameResult } #${ user } :${ password } @${ host } :${ port } /${ database } ` ;
150+ const credsString = createCredsString ( poolName , {
151+ database,
152+ host,
153+ password,
154+ port,
155+ user,
156+ } ) ;
32157
33158 const poolCandidate = pools . get ( credsString ) ;
34159
@@ -43,24 +168,26 @@ export const getTransactionPool = (config: MYSQL.ModelTypes.TDBCreds, poolName =
43168 }
44169} ;
45170
46- export const removeStandardPool = async ( config : MYSQL . ModelTypes . TDBCreds , poolName = "00" ) : Promise < void > => {
47- const { database, host, password, port, user } = config ;
48- const poolNameResult = "st" + poolName ;
49- const credsString = `${ poolNameResult } #${ user } :${ password } @${ host } :${ port } /${ database } ` ;
50-
51- const pool = pools . get ( credsString ) ;
52-
53- if ( pool ) {
54- pools . delete ( credsString ) ;
55-
56- await pool . end ( ) ;
57- }
58- } ;
59-
60- export const removeTransactionPool = async ( config : MYSQL . ModelTypes . TDBCreds , poolName = "00" ) : Promise < void > => {
171+ /**
172+ * Removes and closes a named connection pool.
173+ *
174+ * @param config - The database credentials configuration object.
175+ * @param poolName - The name suffix for the pool.
176+ *
177+ * @returns A promise that resolves when the pool is closed.
178+ */
179+ const removePool = async (
180+ config : MYSQL . ModelTypes . TDBCreds ,
181+ poolName : string ,
182+ ) : Promise < void > => {
61183 const { database, host, password, port, user } = config ;
62- const poolNameResult = "tr" + poolName ;
63- const credsString = `${ poolNameResult } #${ user } :${ password } @${ host } :${ port } /${ database } ` ;
184+ const credsString = createCredsString ( poolName , {
185+ database,
186+ host,
187+ password,
188+ port,
189+ user,
190+ } ) ;
64191
65192 const pool = pools . get ( credsString ) ;
66193
0 commit comments