Search code examples
node.jsknex.js

Fetching currently performing queries from the Knex.js pool


I'm trying to fetch currently running queries by inspecting to tarn-based pool. Previously with node-pool, I could inspect _inUseObjects, however, if I try to inspect pendingCreates, I have a number of PendingOperation objects with promises.

Is it even possible to inspect connections to get the queries?


Solution

  • I don't think it is possible to monitor which connections are in use directly from pool, because when connection is taken out of pool, pool forgets it.

    You could use global knex events to track started / completed queries https://knexjs.org/#Interfaces-Events

    Something like this:

    knex.on('query', querySpec => {
      console.log(`Query ${querySpec.__knexUid} started`);
    }); 
    
    knex.on('query-response', (res, querySpec) => {
      console.log(`Query ${querySpec.__knexUid} ended`);
    }); 
    
    knex.on('query-error', (err, querySpec) => {
      console.log(`Query ${querySpec.__knexUid} failed`);
    });