Skip to content

feat: allow connection config to be a function resolver#1164

Open
TheAdamGalloway wants to merge 3 commits intoadonisjs:22.xfrom
TheAdamGalloway:feat/connection-resolver
Open

feat: allow connection config to be a function resolver#1164
TheAdamGalloway wants to merge 3 commits intoadonisjs:22.xfrom
TheAdamGalloway:feat/connection-resolver

Conversation

@TheAdamGalloway
Copy link
Copy Markdown

🔗 Linked issue

#1141

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • 👌 Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

📚 Description

Add support for providing a ConnectionResolver<T> (a sync or async function) as the connection value in database config types. This mirrors Knex's own ConnectionConfigProvider and enables dynamic credentials such as short-lived IAM authentication tokens that must be refreshed on each connection acquisition.

Affected config types: MysqlConfig, PostgreConfig, OracleConfig, MssqlConfig.

When a resolver function is provided, the Connection class stores it and substitutes an empty placeholder for the initial Knex setup. The patched knex-dynamic-connection resolver calls the function on each connection acquisition, so credentials are always fresh.

I have run the test suite and it passed, and I have tested the connection function locally and can confirm it works. I appreciate you may have an opinion @thetutlage on how the new types are set up, so I'm happy to make any changes you see fit.

Closes #1141

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

TheAdamGalloway and others added 3 commits March 23, 2026 10:16
Add support for providing a `ConnectionResolver<T>` (a sync or async
function) as the `connection` value in database config types. This
mirrors Knex's own `ConnectionConfigProvider` and enables dynamic
credentials such as short-lived IAM authentication tokens that must
be refreshed on each connection acquisition.

Affected config types: MysqlConfig, PostgreConfig, OracleConfig,
MssqlConfig.

When a resolver function is provided, the `Connection` class stores
it and substitutes an empty placeholder for the initial Knex setup.
The patched `knex-dynamic-connection` resolver calls the function on
each connection acquisition, so credentials are always fresh.

Closes adonisjs#1141
@TheAdamGalloway
Copy link
Copy Markdown
Author

@thetutlage I appreciate you're very busy right now, but I was wondering if you had any feedback on the way this is implemented? I'm happy to make any changes

@thetutlage
Copy link
Copy Markdown
Member

@TheAdamGalloway Thanks for the PR. On the surface it does look fine. However, I will have to try it manually to make sure everything works fine.

Let's see if I can take out some time today

Comment thread src/connection/index.ts

switch (this.clientName) {
case 'postgres':
client.acquireRawConnection = function acquireRawConnection(this: any) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we really have to do this? Doesn't Knex support async config resolver?

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.

connection param in PostgreConfig doesn't accept a function

2 participants