Knex

Digging deeper into the Knex queries can be done with the knex configuration option debug set to true.

Enabling Debug Output for Knex

On the OB API server you can turn debugging output from Knex by updating the knexInstance definition as follows:

In the BE file: .env

Add a line like this…

# Knex/Objection
DEBUG=knex:*

In the BE file: app/config/database.ts

const knexInstance = knex({ ...config, debug: true });

Reviewing Output

The output from Knex debugging should appear in the API console.

Notes

Knex debug uses the debug-js library.

The output will be displayed in the console — usually the command line terminal session where the API service was started.

The connection can be to any database.

Adding Rudimentary Timings To Knex Debug Output

With Knex fully installed, look for this file and hack it…

node_modules/knex/lib/logger.js

These last two Date.now() entries setup the initial timing data:

constructor(config = {}) {
  const {
    log: {
      debug,
      warn,
      error,
      deprecate,
      inspectionDepth,
      enableColors,
    } = {},
  } = config;
  this._inspectionDepth = inspectionDepth || 5;
  this._enableColors = resolveIsEnabledColors(enableColors);
  this._debug = debug;
  this._warn = warn;
  this._error = error;
  this._deprecate = deprecate;
  this.lastTime = Date.now();
  this.firstTime = Date.now();
}

// Then hack the _log() method as well to add a couple timer updates:


  _log(message, userFn, colorFn) {
    if (userFn != null && !isFunction(userFn)) {
      throw new TypeError('Extensions to knex logger must be functions!');
    }

    if (isFunction(userFn)) {
      userFn(message);
      return;
    }

    if (!isString(message)) {
      message = inspect(message, {
        depth: this._inspectionDepth,
        colors: this._enableColors,
      });
    }

    let theTime = Date.now();
    console.log( 'since last: ', theTime - this.lastTime, 'since' +
      ' first:', theTime - this.firstTime );
    this.lastTime = theTime;
    console.log(colorFn ? colorFn(message) : message);
  }