Skip to content

Commit

Permalink
add explicit pgbouncer pool
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Feb 21, 2024
1 parent a7db670 commit e5f1925
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
14 changes: 11 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,21 @@ export const getTestPostgresDatabaseFactory = <
connectionDetailsFromWorker: ConnectionDetailsFromWorker
): ConnectionDetails => {
const pool = new Pool({
connectionString:
connectionDetailsFromWorker.pgbouncerConnectionString ??
connectionDetailsFromWorker.connectionString,
connectionString: connectionDetailsFromWorker.connectionString,
})

let pgbouncerPool: Pool | undefined
if (connectionDetailsFromWorker.pgbouncerConnectionString) {
pgbouncerPool = new Pool({
connectionString:
connectionDetailsFromWorker.pgbouncerConnectionString,
})
}

t.teardown(async () => {
try {
await pool.end()
await pgbouncerPool?.end()
} catch (error) {
if (
(error as Error).message.includes(
Expand All @@ -96,6 +103,7 @@ export const getTestPostgresDatabaseFactory = <
return {
...connectionDetailsFromWorker,
pool,
pgbouncerPool,
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export interface ConnectionDetails {
database: string

pool: Pool

// TODO if pgbouncer is enabled, this is defined, otherwise undefined
pgbouncerPool?: Pool
}

export interface GetTestPostgresDatabaseFactoryOptions<
Expand Down
4 changes: 3 additions & 1 deletion src/tests/pgbouncer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ test("pgbouncer", async (t) => {
t.is(result.rows[0].result, 1)

// can't use a transaction with statement pool mode
const err = await t.throwsAsync(postgres13.pool.query(`BEGIN TRANSACTION`))
const err = await t.throwsAsync(
postgres13.pgbouncerPool!.query(`BEGIN TRANSACTION`)
)

t.truthy(
err!
Expand Down

0 comments on commit e5f1925

Please sign in to comment.