javascript - Knex 查询事件日志到 Winston 文件。

标签 javascript knex.js

我想将所有 knex 查询事件保存到winston 文件中。我发现了这样的东西 http://knexjs.org/#Interfaces-Events 。它对我很有用,但现在我必须添加

 .on('query-response', function(response, obj, builder)...

对于每个 knex 查询。

我想为所有 qnex 查询添加一个全局函数。有可能的?

最佳答案

您可以从单独的 dbConnection 文件导出 knex 对象,并将其导入到您需要的其他文件中。在您的 dbConnection 文件中将事件监听器添加到 knex。像这儿: 在您的 dbConnection 文件中写入以下内容:

const knex = require('knex')({ 
    //Your db configuration here
});

knex.on('query', console.log);

module.exports = knex;

在您的其他文件中需要它并使用它。

const knex = require('/dbConnection');

关于javascript - Knex 查询事件日志到 Winston 文件。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47306142/

相关文章:

javascript - 如何查找给定数组中总和为零的子数组的数量

javascript - 光滑的 slider 创建意外的行

javascript - Postgres 迁移错误 - 错误 : type already exists

相当于 C# LINQ Select 的 Javascript

javascript - 如何在 GSAP 或 Javascript 中以不同的时间间隔调用函数?

javascript - Series 中的 Promise 没有按顺序执行

node.js - MySQL knex 数学运算

javascript - bookshelf js 中的多个 order By() 列

node.js - 如何在 sails js 应用程序中全局使用 knexjs

javascript - 使用 NeDB 进行测试,同时在 FeatherJS 应用程序中使用其他数据库