1
1
import * as assert from "assert" ;
2
2
import { EventEmitter } from "events" ;
3
3
import { applyHooks , AsyncHooks , resolvePresets } from "graphile-config" ;
4
- import { Client , Pool , PoolClient } from "pg" ;
4
+ import { Client , Pool , PoolClient , PoolConfig } from "pg" ;
5
5
6
6
import { makeWorkerPresetWorkerOptions } from "./config" ;
7
7
import { migrations } from "./generated/sql" ;
@@ -284,7 +284,6 @@ export async function assertPool(
284
284
releasers : Releasers ,
285
285
) : Promise < Pool > {
286
286
const {
287
- logger,
288
287
resolvedPreset : {
289
288
worker : { maxPoolSize, connectionString } ,
290
289
} ,
@@ -299,28 +298,50 @@ export async function assertPool(
299
298
let pgPool : Pool ;
300
299
if ( _rawOptions . pgPool ) {
301
300
pgPool = _rawOptions . pgPool ;
301
+ if ( pgPool . listeners ( "error" ) . length === 0 ) {
302
+ console . warn (
303
+ `Your pool doesn't have error handlers! See: https://err.red/wpeh` ,
304
+ ) ;
305
+ installErrorHandlers ( compiledSharedOptions , releasers , pgPool ) ;
306
+ }
302
307
} else if ( connectionString ) {
303
- pgPool = new Pool ( {
308
+ pgPool = makeNewPool ( compiledSharedOptions , releasers , {
304
309
connectionString,
305
310
max : maxPoolSize ,
306
311
} ) ;
307
- releasers . push ( ( ) => {
308
- pgPool . end ( ) ;
309
- } ) ;
310
312
} else if ( process . env . PGDATABASE ) {
311
- pgPool = new Pool ( {
313
+ pgPool = makeNewPool ( compiledSharedOptions , releasers , {
312
314
/* Pool automatically pulls settings from envvars */
313
315
max : maxPoolSize ,
314
316
} ) ;
315
- releasers . push ( ( ) => {
316
- pgPool . end ( ) ;
317
- } ) ;
318
317
} else {
319
318
throw new Error (
320
319
"You must either specify `pgPool` or `connectionString`, or you must make the `DATABASE_URL` or `PG*` environmental variables available." ,
321
320
) ;
322
321
}
323
322
323
+ return pgPool ;
324
+ }
325
+
326
+ function makeNewPool (
327
+ compiledSharedOptions : CompiledSharedOptions ,
328
+ releasers : Releasers ,
329
+ poolOptions : PoolConfig ,
330
+ ) {
331
+ const pgPool = new Pool ( poolOptions ) ;
332
+ releasers . push ( ( ) => {
333
+ pgPool . end ( ) ;
334
+ } ) ;
335
+ installErrorHandlers ( compiledSharedOptions , releasers , pgPool ) ;
336
+ return pgPool ;
337
+ }
338
+
339
+ function installErrorHandlers (
340
+ compiledSharedOptions : CompiledSharedOptions ,
341
+ releasers : Releasers ,
342
+ pgPool : Pool ,
343
+ ) {
344
+ const { logger } = compiledSharedOptions ;
324
345
const handlePoolError = ( err : Error ) => {
325
346
/*
326
347
* This handler is required so that client connection errors on clients
@@ -358,7 +379,6 @@ export async function assertPool(
358
379
pgPool . removeListener ( "error" , handlePoolError ) ;
359
380
pgPool . removeListener ( "connect" , handlePoolConnect ) ;
360
381
} ) ;
361
- return pgPool ;
362
382
}
363
383
364
384
export type Release = ( ) => PromiseOrDirect < void > ;
0 commit comments