@@ -8,6 +8,8 @@ import { RDSConnection } from './connection';
88import { RDSTransaction } from './transaction' ;
99import { RDSPoolConfig } from './PoolConfig' ;
1010import literals from './literals' ;
11+ import channels from './channels' ;
12+ import type { ConnectionMessage , ConnectionEnqueueMessage } from './channels' ;
1113
1214interface PoolPromisify extends Omit < Pool , 'query' > {
1315 query ( sql : string ) : Promise < any > ;
@@ -39,8 +41,8 @@ export class RDSClient extends Operator {
3941 // get connection options from getConnectionConfig method every time
4042 if ( mysqlOptions . getConnectionConfig ) {
4143 // eslint-disable-next-line @typescript-eslint/no-var-requires
42- const Pool = require ( 'mysql/lib/Pool' ) ;
43- this . #pool = new Pool ( {
44+ const MySQLPool = require ( 'mysql/lib/Pool' ) ;
45+ this . #pool = new MySQLPool ( {
4446 config : new RDSPoolConfig ( mysqlOptions , mysqlOptions . getConnectionConfig ) ,
4547 } ) ;
4648 // override _needsChangeUser to return false
@@ -57,11 +59,39 @@ export class RDSClient extends Operator {
5759 } ) ;
5860 this . #connectionStorage = connectionStorage || new AsyncLocalStorage ( ) ;
5961 this . #connectionStorageKey = connectionStorageKey || RDSClient . #DEFAULT_STORAGE_KEY;
62+ // https://github.com/mysqljs/mysql#pool-events
63+ this . #pool. on ( 'connection' , ( connection : PoolConnectionPromisify ) => {
64+ channels . connectionNew . publish ( {
65+ client : this ,
66+ connection,
67+ } as ConnectionMessage ) ;
68+ } ) ;
69+ this . #pool. on ( 'enqueue' , ( ) => {
70+ channels . connectionEnqueue . publish ( {
71+ client : this ,
72+ } as ConnectionEnqueueMessage ) ;
73+ } ) ;
74+ this . #pool. on ( 'acquire' , ( connection : PoolConnectionPromisify ) => {
75+ channels . connectionAcquire . publish ( {
76+ client : this ,
77+ connection,
78+ } as ConnectionMessage ) ;
79+ } ) ;
80+ this . #pool. on ( 'release' , ( connection : PoolConnectionPromisify ) => {
81+ channels . connectionRelease . publish ( {
82+ client : this ,
83+ connection,
84+ } as ConnectionMessage ) ;
85+ } ) ;
6086 }
6187
62- // impl Operator._query
63- protected async _query ( sql : string ) {
64- return await this . #pool. query ( sql ) ;
88+ async query < T = any > ( sql : string , values ?: object | any [ ] ) : Promise < T > {
89+ const conn = await this . getConnection ( ) ;
90+ try {
91+ return await conn . query ( sql , values ) ;
92+ } finally {
93+ conn . release ( ) ;
94+ }
6595 }
6696
6797 get pool ( ) {
@@ -197,7 +227,7 @@ export class RDSClient extends Operator {
197227 * @param scope - scope with code
198228 * @return {Object } - scope return result
199229 */
200- async beginTransactionScope ( scope : TransactionScope ) {
230+ async beginTransactionScope ( scope : TransactionScope ) : Promise < any > {
201231 let ctx = this . #connectionStorage. getStore ( ) ;
202232 if ( ctx ) {
203233 return await this . #beginTransactionScope( scope , ctx ) ;
0 commit comments