- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个关于在 Postgres 中使用 Koa 的最佳方式的问题。我也(真的)喜欢使用 Bluebird,所以我采用了这种方法。
'use strict';
var db = require('./modules/db.js');
var koa = require('koa');
var app = koa();
app.use(function *(){
yield db.withConnection(function *(client){
let id = this.request.query.id;
let simple_data = yield client.queryAsync('select name from table1 where id = $1', [id]);
this.response.body = simple_data;
}.bind(this));
});
app.listen(3000);
这是 db.js
文件,基本上它使用了 Bluebird 文档中提到的内容。
... bla bla, some imports
Promise.promisifyAll(pg);
function getSqlConnection() {
var close;
return pg.connectAsync(connectionString).spread(function(client, done) {
close = done;
return client;
}).disposer(function() {
if (close) close();
});
}
function* withConnection(func){
yield Promise.using(getSqlConnection(), function (client){
return Promise.coroutine(func)(client);
});
}
module.exports.withConnection = withConnection;
你有什么改进的建议吗?我现在真的很喜欢它,我已经对它进行了广泛的测试(在负载下,出现错误/异常等),它似乎可以正常工作。我对这些生成器和其他 ES6 东西还很陌生,所以我可能遗漏了一些东西。我的问题基本上是为什么很少有人使用这种方法(我发现很难在网上找到例子)?
除了 pg 和 bluebird 之外,我还可以使用其他库,但我喜欢这些库,因为它们的下载量很大,我更喜欢使用流行的东西,因为我发现更容易找到这些库的博客文章、帮助和文档.谢谢!
最佳答案
Bluebird 是一个 promise 库,在这方面非常出色,但不应将其用作指导如何使用或使用什么数据库库。所有那些 Promise.promisifyAll(pg);
的东西实际上与现有的所有 promise 解决方案相比都很差——knex、massive.js、pg-promise 等。
如果您想要pg
+ bluebird
的最佳组合,请尝试pg-promise .
var promise = require('bluebird');
var options = {
promiseLib: promise // Use Bluebird as the promise library
};
var pgp = require("pg-promise")(options);
var db = pgp('postgres://username:password@host:port/database');
db.query('select name from table1 where id = $1', [1])
.then(function (data) {
})
.catch(function (error) {
});
该库还支持 ES6 生成器,因此您可以编写与示例中完全相同的代码。
来自 Tasks示例:
db.task(function * (t) {
let user = yield t.one("select * from users where id=$1", 123);
return yield t.any("select * from events where login=$1", user.name);
})
.then(function (events) {
// success;
})
.catch(function (error) {
// error;
});
关于javascript - 将 Koa 与 bluebird 和 pg 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35046040/
bluebird.js和bluebird.core.js有什么区别? 什么时候应该使用bluebird.core.js而不是bluebird.js? 我无法在bluebird site或其他地方找到任
我正在从异步转换为 Bluebird,但不知道如何打破循环 这是我想要实现的目标: 循环数据数组。 对于每个项目,检查它是否存在于数据库中。 将一个项添加到数据库(第一个不存在的项),然后退出.eac
我试图用 .try(function(){}).catch(function(){}) 块返回一个 promise 。我的问题是由我的 promise 类型引起的。 deleteProcess
我正在尝试使用 bluebird 的 .return() 来扩展 promise 解析值方法。 目前我正在使用以下代码: doSomethingAsync() // assu
我刚刚开始使用 Promise 和 Bluebird。调试时我可以看到我的函数执行了两次: 首先我收到此错误:TypeError:未捕获错误:无法读取未定义的属性“then” 然后我看到函数再次执行,
我想测试数组的每个元素,直到满足条件,然后跳过其余的。这是我想出的代码,它似乎有效,但我不确定它是否真的安全或有意想不到的副作用。欢迎其他解决方案。 let buddyAdded = false; r
假设我有以下 node.js 代码 function foo() { return promiseOne().then(function(result) { return pr
假设我想在从数据库查找用户后同时发送电子邮件并向客户端推送通知,我可以这样写 User.findById(userId).exec() .then(() => sendMail()) .then(()
.call 方法的 Bluebird 文档有 code sample标记为“链接破折号或下划线方法”。 下面的代码片段中链接的 .then(_) 的用途是什么? var Promise = requi
是否有某种方法可以检索从上一个 then 回调返回的任何内容(或传递给初始 Promise.resolve()/resolve())? const p = Bluebird.resolve().the
我有以下代码。当 f2 没有抛出错误时,它工作正常。 如果有错误,它会生成一个Unhandled rejection Error。 重写代码以避免 Unhandled rejection Error
我有一个来自这篇文章的后续问题:Chaining Requests using BlueBird/ Request-Promise 我对 promise 很陌生,所以请原谅我的天真。我成功地实现了这段
我是 Bluebird 的新手,我正在尝试创建一个新用户,但 reject 函数没有按预期工作。 问题是它为我创建了用户,即使它启动了错误There nickname is already in us
我正在尝试实现剪刀石头布游戏的 CLI 版本。我正在使用查询器模块来处理 IO。我的主要功能如下所示: RockPaperScissors.prototype.gameLoop = function(
此代码运行正常: let promise; try { promise = parent(); //but I want: await parent(); await cont
在 promise 了 fs-extra 后,我知道我可以使用 then 来访问文件。我猜想有某种机制,在获取文件后,它知道要移动到 then 链中的下一个链接。然而,接下来的then我只是放置了一个
我有以下代码。它按预期工作,没有抛出未处理的拒绝错误。 p = new Promise (fulfill, reject) -> reject new Error 'some error' p.c
我期待 Bluebird forgotten return warning出现,但由于某种原因它不起作用。 A demo : const Bluebird = require('bluebird');
我正在使用 bluebird图书馆结束memcached . memcached.set('foo', 'bar', 10, function (err) { /* stuff */ }); 此函数不
我正在尝试如下使用 Bluebird 的协程: var p = require('bluebird'); //this should return a promise resolved to valu
我是一名优秀的程序员,十分优秀!