/**
* Knex-related utilities.
*
* @module server/utils/knex
* @see http://knexjs.org
*/
const _ = require('lodash');
const config = require('../../config');
const logger = config.logger('db');
/**
* A logger function that can be passed to knex to log all database queries.
*
* @function
* @param {*} query - A knex query.
*/
exports.logger = function(query) {
let message = query.sql;
Eif (query.bindings) {
query.bindings.forEach((binding, i) => {
message = message.replace(new RegExp(`\\$${i + 1}`), logValue(binding));
});
}
if (!message.match(/;$/)) {
message = message + ';';
}
logger.trace(message);
}
function logValue(value) {
Iif (value === undefined) {
return 'undefined';
}
Iif (value instanceof Buffer) {
value = value.toString('hex');
} else {
value = JSON.stringify(value);
}
if (value.length > 50) {
value = `${value.substring(0, 50)}...`;
Eif (value.match(/^"/)) {
value = `${value}"`;
}
}
return value;
}
|