nodejs,postgres和bluebird

我一直在尝试使用pg库的蓝鸟许诺,甚至发现这个post,但可悲的是我太StackOverflow用户的新来简单地直接评论: 与蓝鸟手动promisifying pg.connect

简而言之,在执行剪切和粘贴代码之后,用蓝鸟Promisfy函数尝试的所有内容都不会引入任何查询构造函数,也许我在尝试中滥用了ClientAsync函数,但是希望这是一个快速和容易的帮助,因为我尝试的一切都会导致一些变化:

Possibly unhandled TypeError: Object function (err) { if(err) { pool.destroy(client); } else { pool.release(client); } } has no method 'queryAsync' 

我抛弃了PromisfyAll函数的结果,并确保足够的queryAsync不存在:

相关片段:

 Client: { [Function] Query: { [Function] super_: [Object] } }, Query: { [Function] super_: { [Function: EventEmitter] listenerCount: [Function] } }, pools: { all: {}, Client: { [Function] Query: [Object] }, getOrCreate: [Function] }, Connection: { [Function] super_: { [Function: EventEmitter] listenerCount: [Function] } }, types: { getTypeParser: [Function], setTypeParser: [Function], arrayParser: { create: [Function] } }, ClientAsync: { [Function: ClientAsync] __isPromisified__: true }, endAsync: { [Function: endAsync] __isPromisified__: true }, connectAsync: { [Function: connectAsync] __isPromisified__: true }, cancelAsync: { [Function: cancelAsync] __isPromisified__: true }, setMaxListenersAsync: { [Function: setMaxListenersAsync] __isPromisified__: true }, emitAsync: { [Function: emitAsync] __isPromisified__: true }, addListenerAsync: { [Function: addListenerAsync] __isPromisified__: true }, onAsync: { [Function: onAsync] __isPromisified__: true }, onceAsync: { [Function: onceAsync] __isPromisified__: true }, removeListenerAsync: { [Function: removeListenerAsync] __isPromisified__: true }, removeAllListenersAsync: { [Function: removeAllListenersAsync] __isPromisified__: true }, listenersAsync: { [Function: listenersAsync] __isPromisified__: true } } 

它发现在parsing相关的function,但不promisfy查询:有没有人知道我可以解决这个问题 – 进一步发射或用ClientAsync执行SQL查询的潜在语法? 我试图从蓝鸟github页面上的信息中手动添加pg query.js文件,但无济于事。

那么使用Promisfy就会发现这是Javascript与本地绑定库的区别。

 var pg = require('pg'); var Promise = require('bluebird'); var db = Promise.promisifyAll(pg); var connectionString = "postgres://node:node@localhost:5432/postgres"; db.connectAsync("postgres://node:node@localhost:5432/postgres").spread(function(connection, release) { return connection.queryAsync("select * from howdy") .then(function(result) { console.log("rows", result.rows); }) .finally(function() { release(); }); }); 

工程,而这个:

 var pg = require('pg').native; var Promise = require('bluebird'); 

与丑陋的错误消息打破。

我想我需要在各种选项(bluebird w / promisfy和JS-bindings与C-bindings(libpq)以及手动promise之间做一些基准testing。

如果您想充分利用Promises / A +架构,同时joinPG库和Bluebird ,请尝试pg-promise 。

手动promisification将几乎没有任何好处,承诺可以提供给正常使用时的数据库,如连接pipe理,自动交易等

为了给你一个想法,下面是一个完整的交易看起来如何与pg-promise ,很好地隐藏连接和交易的细节:

 db.tx(function () { return this.batch([ this.query("update users set active=$1 where id=$2", [true, 123]), this.query("insert into audit(status, id) values($1, $2)", ['active', 123]) ]); }) .then(function (data) { // success; }, function (reason) { // error; }); 

而通过手动提供的版本的相同的逻辑会长很多倍,而且要复杂得多。 事实上,你仍然需要做以下所有的事情:

  • 打开连接;
  • 检查连接是否成功;
  • 执行BEGIN命令;
  • 执行查询的顺序;
  • 检查您的查询是否成功;
  • 根据您的查询成功执行COMMITROLLBACK ;
  • 检查交易是否成功结束;
  • 释放连接回到游泳池;
  • 返回结果进行进一步处理;

现在,考虑嵌套交易:)

bluebird的创造者在这里回答相关的问题手动promisifying与蓝鸟pg.connect 。 我已经修改了这个解决scheme。

 var Promise = require('bluebird'); var pg = require('pg'); Object.keys(pg).forEach(function (key) { var Cls = null; try { Cls = pg[key]; if (typeof Cls === 'function') { Promise.promisifyAll(Cls.prototype); Promise.promisifyAll(Cls); } } catch (e) { console.log(e); } }); Promise.promisifyAll(pg); 

这里'pg[key]封装在try-catch块中,因为当试图访问pg['native']时, pg[key]可能会重try-catch error