Skip to content

Add onConnect callback which allows "setup" of a pooled client#3620

Open
brianc wants to merge 6 commits intomasterfrom
bmc/pool-callbacks
Open

Add onConnect callback which allows "setup" of a pooled client#3620
brianc wants to merge 6 commits intomasterfrom
bmc/pool-callbacks

Conversation

@brianc
Copy link
Owner

@brianc brianc commented Mar 2, 2026

This allows you to do

const pool = new Pool({
  onConnect: async (client) => await client.query('SET search_path=foo,public')
})

for example.

If the onConnect function throws or rejects the error will be sent out to the callsite of pool.connect or pool.query & the client will be disposed of. This only triggers when the client is first connected to postgres not when an existing connected client is checked out.

Why did I call it pool.connect so long ago instead of pool.getClient() or pool.aquire() or something? I don't know. All I know is I'm not very fond of myself from 10 years ago. Oh well! I'll make sure to include good documentation on this change before I actually merge it but want to put it up for some 👀

This should fix #3617

I omitted the other hook ideas I had as I need to think through them more, and in the case of onClose the release method is sync, and some times clients are closed in the background so error handling there is a different issue I'll need to think about.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 2, 2026

Deploying node-postgres with  Cloudflare Pages  Cloudflare Pages

Latest commit: ca8c336
Status: ✅  Deploy successful!
Preview URL: https://a97607d8.node-postgres.pages.dev
Branch Preview URL: https://bmc-pool-callbacks.node-postgres.pages.dev

View logs

maxLifetimeTimeout.unref()
client.once('end', () => clearTimeout(maxLifetimeTimeout))
if (this.options.onConnect) {
let hookResult
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a single promiseTry(this.options.onConnect).then(…, …) path? Where

const promiseTry = Promise.try ?? (f) => new Promise((resolve) => {
  resolve(f())
})

Copy link
Collaborator

@charmander charmander Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this gets a little more complicated if it has to support the custom Promise config option.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Note: I’m bringing this up ahead of publishing instead of making a follow-up PR because the behaviour is slightly different, so it would be a breaking change.)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much appreciated! Though I'm not sure what you mean by a breaking change...what's a breaking change, exactly?

Copy link
Collaborator

@charmander charmander Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The promiseTry suggestion, compared to what’s currently in this PR. They’re slightly different from each other, so it couldn’t wait until afterwards, I mean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Future Support for Setup Queries When a New Pool Client Connects

2 participants