Skip to content

feat(dispatcher): add new onKnexCreated callback - #35

Merged
jhoncool merged 1 commit into
mainfrom
feat(displatcher)_add_on_knex_created_callback
Aug 26, 2026
Merged

feat(dispatcher): add new onKnexCreated callback#35
jhoncool merged 1 commit into
mainfrom
feat(displatcher)_add_on_knex_created_callback

Conversation

@Qbit982

@Qbit982 Qbit982 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Summary

Add an optional onKnexCreated callback that is called synchronously for every Knex instance created by PGDispatcher, before database health checks start.

Motivation

Consumers may need to attach metrics, tracing, event listeners, or plugins directly to every Knex instance.

Currently, Knex instances are only accessible through the primary and replica getters. These getters require a healthy database connection and can throw No connections available during application startup. They may also return different clients after a failover.

The new callback provides access to every Knex instance at creation time without depending on database availability or primary/replica selection.

Behavior

  • The callback is optional and backward-compatible.
  • It is called once for every configured connection.
  • It runs immediately after the Knex instance is created.
  • It runs before the initial database health check.
  • If the callback throws, PGDispatcher construction fails.

Example

const {db} = initDB({
    connectionString,
    onKnexCreated(knex) {
        knex.on('query', handleQuery);
        knex.on('query-response', handleQueryResponse);
        knex.on('query-error', handleQueryError);
    },
});

Comment thread lib/dispatcher.ts Outdated
logger: ExLogger;

/**
* Called synchronously once for each Knex instance created by the dispatcher,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's add text to README.md
No need to add a comment here

@jhoncool

Copy link
Copy Markdown
Collaborator

@Qbit982 typo feat(displather)

@Qbit982
Qbit982 force-pushed the feat(displatcher)_add_on_knex_created_callback branch from 50ce9e5 to 891198f Compare August 26, 2026 12:37
@Qbit982 Qbit982 changed the title feat(displather): add new onKnexCreated callback in constructor feat(dispatcher): add new onKnexCreated callback Aug 26, 2026
@Qbit982

Qbit982 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor Author

@jhoncool FIXED, see again please

@jhoncool
jhoncool merged commit 3a2bd56 into main Aug 26, 2026
5 checks passed
@jhoncool
jhoncool deleted the feat(displatcher)_add_on_knex_created_callback branch August 26, 2026 12:39
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.

2 participants