我正在使用 pg-promise与 Nodejs 打包以执行 PostgreSQL 查询。我想查看执行的查询。仅特定查询,例如,我想要调试的一个查询。
我可以看到一种推荐的方法是使用 pg-monitor捕获事件并记录它们,如示例中所述 documentation .
不使用 pg-monitor ,是否有一种简单的方法来打印已执行的准备好的查询。我在文档中看不到它。
例子:
db.query("SELECT * FROM table WHERE id = $/id/", {id: 2})
如何打印这个查询以产生?
SELECT * FROM table WHERE id = 2
最佳答案
is there a simple way to just print the prepared query that is executed...
一般查询 - 是的,请参见下文。 Prepared Query - 不,根据定义,这些是在服务器端格式化的。
const query = pgp.as.format('SELECT * FROM table WHERE id = $/id/', {id: 2});
console.log(query);
await db.any(query);
如果您想打印模块执行的所有查询,而不使用 pg-monitor , 只需添加事件 query初始化库时的处理程序:
const initOptions = {
query(e) {
console.log(e.query);
}
};
const pgp = require('pg-promise')(initOptions);
关于node.js - 使用 pg-promise 记录特定的 postgresql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43780737/