@@ -43,6 +43,10 @@ export class TinyPg {
43
43
44
44
this . pool = new Pg . Pool ( pool_config )
45
45
46
+ this . pool . on ( 'error' , ( error ) => {
47
+ TINYPG_LOG && Util . Log ( 'Error with idle client in pool.' , error )
48
+ } )
49
+
46
50
this . sql_files = P . parseFiles ( [ ] . concat ( this . options . root_dir ) )
47
51
48
52
this . sql_db_calls = _ . keyBy ( _ . map ( this . sql_files , sql_file => {
@@ -185,8 +189,12 @@ export class TinyPg {
185
189
186
190
this . events . emit ( 'query' , query_context )
187
191
188
- const callComplete = ( error : Error , data : T . Result < T > ) => {
189
- client . release ( )
192
+ const callComplete = _ . once ( ( error : Error , data : T . Result < T > ) => {
193
+ if ( error && ( ! error [ 'code' ] || _ . startsWith ( error [ 'code' ] , '57P' ) ) ) {
194
+ ( < any > client ) . release ( error )
195
+ } else {
196
+ client . release ( )
197
+ }
190
198
191
199
const end_at = Date . now ( )
192
200
@@ -198,9 +206,9 @@ export class TinyPg {
198
206
} )
199
207
200
208
this . events . emit ( 'result' , complete_context )
201
- }
209
+ } )
202
210
203
- return Promise . resolve ( )
211
+ const query_promise = Promise . resolve ( )
204
212
. then ( ( ) => {
205
213
TINYPG_LOG && Util . Log ( 'executing' , db_call . config . name )
206
214
@@ -226,6 +234,8 @@ export class TinyPg {
226
234
}
227
235
} )
228
236
} )
237
+
238
+ return query_promise
229
239
. then ( result => {
230
240
callComplete ( null , result )
231
241
return result
@@ -234,8 +244,10 @@ export class TinyPg {
234
244
callComplete ( error , null )
235
245
236
246
const tiny_error = new T . TinyPgError ( error . message )
247
+
237
248
tiny_error . stack = stack_trace_accessor . stack
238
249
tiny_error . queryContext = query_context
250
+
239
251
throw this . options . error_transformer ( tiny_error )
240
252
} )
241
253
} )
0 commit comments