在node.js中关闭Postgres(pg)客户端连接 [英] Closing postgres (pg) client connection in node.js

查看:247
本文介绍了在node.js中关闭Postgres(pg)客户端连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在节点中按计划运行的脚本。该脚本没有终止并退出。我怀疑这是因为我的数据库客户端仍处于打开状态。

I have a script that I want to run on a scheduled basis in node. The script is not terminating and exiting. I suspect that this is because my database client is still open.

var client = new pg.Client(conString);
client.connect();

function registerBundle (innerHash, outterHash) {
    // some stuff here
}

var query = client.query("SELECT id, chain FROM mytable where \
    state_ready = true and transaction_id='' ");

query.on('row', function(row) {
    var chain = row['chain'];
    var pg_record = row['id'];
    console.log(pg_record);
    var innerHash = "something";
    var outerHash = "something else";
    var registrar = registerBundle(innerHash, outerHash);

    var update = client.query('UPDATE mytable SET transaction_id = $1::text \
    where id=$2::int', [transactionHash, pg_record]);
    console.log(chain);
});

如果我添加以下内容,则客户端连接会在更新有时间触发之前关闭。

if I include the following, the client connection closes before the updates have a time to fire.

query.on('end', function() {
    client.end();
});

我无法使用setTimeout或任何其他此类机制,因为我不知道等待多长时间registerBundle函数完成。我也认为query.on('end'会在更新完成时触发。不确定如何测试。

I cannot use setTimeout or any other such mechanism because I don't know how long to wait for the registerBundle function to complete. Also I think query.on('end' will fire when the update is completed. Not sure how to test this.

我的问题是,我需要按顺序触发

My question, I need things to fire in order.


  • 查询数据库

  • 处理每一行(query.on

  • 使用registerBundle返回的值更新每一行

  • 在处理完所有行后关闭数据库客户端/连接。

  • 终止脚本并退出node

  • Query DB
  • Process each row (query.on
  • Update each row with value returned from registerBundle
  • Close db client/connection when all rows have been processed.
  • Terminate script and exit node

似乎从python / php世界中非常简单,但是在我的JavaScript世界中却分崩离析。

Seems pretty straightforward from a python/php world but falls apart in my javascript world.

推荐答案

基于承诺的界面,例如 pg-promise 是必经之路:

A promise-based interface like pg-promise is the way to go:

var bluebird = require('bluebird');
var pgp = require('pg-promise')({
    promiseLib: bluebird
});
var db = pgp(/*connection details*/);

db.tx(t => {
    // BEGIN executed
    return t.map('SELECT id, chain FROM mytable where state_ready = $1 and transaction_id = $2', [true, 123], a => {
        var chain = data.chain;
        var pg_record = data.id;
        return t.none('UPDATE mytable SET transaction_id = $1::text where id=$2::int', [transactionHash, pg_record]);
    }).then(t.batch); // settling all internal queries
})
    .then(data => {
        // success, COMMIT executed
    })
    .catch(error => {
        // error, ROLLBACK executed
    })
    .finally(pgp.end); // shuts down the connection pool

上面的示例完全符合您的要求,并且使用了交易。但实际上,出于性能原因,您将想要在一个查询中完成所有操作;)

The example above does exactly what you asked for, plus it uses a transaction. But in reality you're gonna want to do it all in one query, for performance reasons ;)

请参见更多示例

这篇关于在node.js中关闭Postgres(pg)客户端连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆