@@ -79,6 +79,7 @@ export class RDSClient extends Operator {
7979 'query' ,
8080 'getConnection' ,
8181 'end' ,
82+ 'execute' ,
8283 ] . forEach ( method => {
8384 this . #pool[ method ] = promisify ( this . #pool[ method ] ) ;
8485 } ) ;
@@ -113,53 +114,43 @@ export class RDSClient extends Operator {
113114 }
114115
115116 async query < T = any > ( sql : string , values ?: object | any [ ] , options ?: QueryOptions ) : Promise < T > {
116- let conn : RDSConnection | RDSTransaction ;
117- let shouldReleaseConn = false ;
118- if ( options ?. conn ) {
119- conn = options . conn ;
120- } else {
121- const ctx = this . #connectionStorage . getStore ( ) ;
122- const ctxConn = ctx ?. [ this . #connectionStorageKey ] ;
123- if ( ctxConn ) {
124- conn = ctxConn ;
125- } else {
126- conn = await this . getConnection ( ) ;
127- shouldReleaseConn = true ;
128- }
129- }
117+ return this . #executeWithConnection ( 'query' , sql , values , options ) ;
118+ }
119+
120+ async execute < T = any > ( sql : string , values ?: object | any [ ] , options ?: QueryOptions ) : Promise < T > {
121+ return this . #executeWithConnection ( 'execute' , sql , values , options ) ;
122+ }
123+
124+ async #executeWithConnection < T = any > (
125+ method : 'query' | 'execute' ,
126+ sql : string ,
127+ values ?: object | any [ ] ,
128+ options ?: QueryOptions ,
129+ ) : Promise < T > {
130+ const { conn , shouldRelease } = await this . #getConnection ( options ) ;
130131
131132 try {
132- return await conn . query ( sql , values ) ;
133+ return await conn [ method ] ( sql , values ) ;
133134 } finally {
134- if ( shouldReleaseConn ) {
135+ if ( shouldRelease ) {
135136 ( conn as RDSConnection ) . release ( ) ;
136137 }
137138 }
138139 }
139140
140- async execute < T = any > ( sql : string , values ?: object | any [ ] , options ?: QueryOptions ) : Promise < T > {
141- let conn : RDSConnection | RDSTransaction ;
142- let shouldReleaseConn = false ;
141+ async #getConnection( options ?: QueryOptions ) : Promise < { conn : RDSConnection | RDSTransaction ; shouldRelease : boolean } > {
143142 if ( options ?. conn ) {
144- conn = options . conn ;
145- } else {
146- const ctx = this . #connectionStorage. getStore ( ) ;
147- const ctxConn = ctx ?. [ this . #connectionStorageKey] ;
148- if ( ctxConn ) {
149- conn = ctxConn ;
150- } else {
151- conn = await this . getConnection ( ) ;
152- shouldReleaseConn = true ;
153- }
143+ return { conn : options . conn , shouldRelease : false } ;
154144 }
155145
156- try {
157- return await conn . execute ( sql , values ) ;
158- } finally {
159- if ( shouldReleaseConn ) {
160- ( conn as RDSConnection ) . release ( ) ;
161- }
146+ const ctx = this . #connectionStorage. getStore ( ) ;
147+ const ctxConn = ctx ?. [ this . #connectionStorageKey] ;
148+ if ( ctxConn ) {
149+ return { conn : ctxConn , shouldRelease : false } ;
162150 }
151+
152+ const conn = await this . getConnection ( ) ;
153+ return { conn, shouldRelease : true } ;
163154 }
164155
165156 get pool ( ) {
0 commit comments