@@ -178,7 +178,9 @@ export class Worker {
178178 const databaseName = getRandomDatabaseName ( )
179179
180180 // Create database
181- const { postgresClient, container } = await this . startContainerPromise
181+ const { postgresClient, container, pgbouncerContainer } = await this
182+ . startContainerPromise
183+
182184 await postgresClient . query ( `CREATE DATABASE ${ databaseName } ;` )
183185
184186 const msg = message . reply ( {
@@ -246,16 +248,29 @@ export class Worker {
246248 }
247249
248250 private async getConnectionDetails ( databaseName : string ) {
249- const { container, network } = await this . startContainerPromise
251+ const { container, network, pgbouncerContainer } = await this
252+ . startContainerPromise
250253 const externalDatabaseUrl = `postgresql://postgres:@${ container . getHost ( ) } :${ container . getMappedPort (
251254 5432
252255 ) } /${ databaseName } `
253256
257+ let pgbouncerConnectionString , pgbouncerConnectionStringDocker
258+ if ( pgbouncerContainer ) {
259+ pgbouncerConnectionString = `postgresql://postgres:@${ pgbouncerContainer . getHost ( ) } :${ pgbouncerContainer . getMappedPort (
260+ 6432
261+ ) } /${ databaseName } `
262+ pgbouncerConnectionStringDocker = `postgresql://postgres:@${ pgbouncerContainer
263+ . getName ( )
264+ . replace ( "/" , "" ) } :5432/${ databaseName } `
265+ }
266+
254267 return {
255268 connectionString : externalDatabaseUrl ,
256269 connectionStringDocker : `postgresql://postgres:@${ container
257270 . getName ( )
258271 . replace ( "/" , "" ) } :5432/${ databaseName } `,
272+ pgbouncerConnectionString,
273+ pgbouncerConnectionStringDocker,
259274 dockerNetworkId : network . getId ( ) ,
260275 host : container . getHost ( ) ,
261276 port : container . getMappedPort ( 5432 ) ,
@@ -300,13 +315,35 @@ export class Worker {
300315 )
301316 }
302317
318+ const connectionString = `postgresql://postgres:@${ startedContainer . getHost ( ) } :${ startedContainer . getMappedPort (
319+ 5432
320+ ) } /postgres`
321+
322+ let startedPgbouncerContainer
323+ if ( this . initialData . pgbouncerOptions ?. enabled ) {
324+ const pgbouncerContainer = new GenericContainer ( "edoburu/pgbouncer" )
325+ . withExposedPorts ( 6432 )
326+ . withName ( getRandomDatabaseName ( ) )
327+ . withEnvironment ( {
328+ DB_HOST : startedContainer . getName ( ) . replace ( "/" , "" ) ,
329+ DB_USER : "postgres" ,
330+ DB_NAME : "*" ,
331+ POOL_MODE :
332+ this . initialData . pgbouncerOptions ?. poolMode ?? "transaction" ,
333+ LISTEN_PORT : "6432" ,
334+ AUTH_TYPE : "trust" ,
335+ } )
336+ . withStartupTimeout ( 120_000 )
337+ . withNetwork ( network )
338+ startedPgbouncerContainer = await pgbouncerContainer . start ( )
339+ }
340+
303341 return {
304342 container : startedContainer ,
343+ pgbouncerContainer : startedPgbouncerContainer ,
305344 network,
306345 postgresClient : new pg . Pool ( {
307- connectionString : `postgresql://postgres:@${ startedContainer . getHost ( ) } :${ startedContainer . getMappedPort (
308- 5432
309- ) } /postgres`,
346+ connectionString,
310347 } ) ,
311348 }
312349 }
0 commit comments