question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pool.afterCreate no longer supported?

See original GitHub issue

Environment

Knex version: 0.19.0 Database + version: PostgreSQL 10.5 OS: macOS Mojave 10.14.15

It appears the afterCreate hook inside of the pool configuration is no longer available, yet the documentation still lists it as a viable option: http://knexjs.org/#Installation-pooling-afterCreate

Based on reading the changelog and upgrade guide, I can see that there is some callout to using tarn.js to manage these types of hooks ourselves, but I’m not seeing any actionable information on how to actually do this.

  1. It would be nice for the documentation to be updated when a breaking change like this is introduced.

  2. I’m looking for guidance on the “tarn.js way” of doing the following (this is effectively the same example provided in the knex documentation) - see code below.

The issue I’m facing is that I can’t seem to figure out how to override the pool configuration with my own tarn.js pool, since instantiating a tarn.js pool requires passing in the DB client somehow. (see here: https://github.com/vincit/tarn.js#usage)

const dbClient = Knex({
  client: 'pg',
  useNullAsDefault: true,
  connection: {
    host: config.get('dbHost'),
    user: config.get('dbUser'),
    password: config.get('dbPassword'),
    database: config.get('dbName'),
    timezone: 'UTC',
  },
  searchPath: ['public'],
  pool: {
    min: 2,
    afterCreate: (connection, callback) => {
        connection.query(`SET timezone to 'UTC'`, err =>
        callback(err, connection),
      );
    }
  },
});

Any help would be greatly appreciated, and as always, thank you for the stellar work on knex.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
mrbarlettacommented, Jul 11, 2022

In my case, on PG 12.11 and knex 2.1 the afterCreate didn’t work, thankfully the other option worked just fine

My example in case another googler ends up here

knex.client.pool.on('createSuccess', (eventId, resource) => {
  resource.query('('hasura.user', '{ "x-hasura-role": "admin",  "x-hasura-application": "cli.import_clients_contacts", "x-hasura-lxusername": "mrb"}', false)', () => {})
})
1reaction
elhigucommented, Nov 13, 2019

Anyways test case for afterCreate should be added. That is why I have left this open.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Installation | Knex.js
afterCreate callback (rawDriverConnection, done) is called when the pool aquires a new connection from the database server. done(err, connection) callback must ...
Read more >
How to ensure the Knex connection in nodejs - Stack Overflow
If no queries are made, pool doesn't necessary create any initial ... You may also also wire up pool's afterCreate callback to notify...
Read more >
f5 sdk can't load pool objects after create pool. DevCentral
I would restructure the code to make it more simple/logical. I understand the for loop to create the different pool names. Maybe try...
Read more >
Hooks - Sequelize
Available hooks​ ... Note: this list is not exhaustive. (1) ... as a permanent global hook, as the connection pool is shared by...
Read more >
Core Events - SQLAlchemy 1.4 Documentation
Available events for Pool . ... The connection is no longer associated with the given connection record. ... Example using the after_create event:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found