使用pg-promise记录特定的Postgresql查询 [英] Log specific postgresql query using pg-promise
问题描述
我在Nodejs中使用 pg-promise 包来执行PostgreSQL查询。我想查看执行的查询。仅特定查询,例如,我要调试的一个查询。
I am using pg-promise package with Nodejs to execute PostgreSQL queries. I want to see the queries executed. Only specific queries, say, just one query that I want to debug.
我可以看到一种推荐的方法是使用 pg-monitor 捕获事件并记录它们,如示例文档。
I can see that one recommended way is to use the pg-monitor to catch the events and log them as mentioned here in the examples documentation.
如果不使用 pg-monitor ,有一种简单的方法可以只打印准备好的查询被执行。我在文档中看不到它。
Without using pg-monitor, is there a simple way to just print the prepared query that is executed. I can't see it in the docs.
示例:
db.query("SELECT * FROM table WHERE id = $/id/", {id: 2})
如何打印此查询以产生收益?
How to print this query to yield?
SELECT * FROM table WHERE id = 2
推荐答案
是否有一种简单的方法来打印准备好的执行的查询...
is there a simple way to just print the prepared query that is executed...
一般的查询-是的,请参见下文。 准备的查询-不,按照定义,这些是在服务器端。
A query in general - yes, see below. A Prepared Query - no, those are by definition formatted on the server-side.
const query = pgp.as.format('SELECT * FROM table WHERE id = $/id/', {id: 2});
console.log(query);
db.any(query).then(...).catch(...)
如果您要打印模块执行的所有查询,而无需使用 pg-monitor ,只需在初始化时添加事件 query 处理程序库:
And if you want to print all queries executed by your module, without using pg-monitor, simply add event query handler when initializing the library:
const initOptions = {
query(e) {
console.log(e.query);
}
};
const pgp = require('pg-promise')(initOptions);
这篇关于使用pg-promise记录特定的Postgresql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!