[英]NodeJS mysql2 Bluebird?
Been testing mysql vs mysql2, seems like 2 has made some improvments however it's not an exact drop in replacement. 在测试mysql vs mysql2时,似乎2已作了一些改进,但并不是替代品的确切下降。 At the same time Q is a good library which seems easier to integrate with however bluebird seems to take less memory and run faster so...
同时Q是一个很好的库,似乎易于集成,但是bluebird似乎占用更少的内存并且运行更快,因此...
My current mysql-bluebird connector is as follows and allows for straight forward use of query('SELECT email FROM users.users WHERE id=?',id).then(function(res){var email=res[0][0];});
我当前的mysql-bluebird连接器如下,并允许直接使用
query('SELECT email FROM users.users WHERE id=?',id).then(function(res){var email=res[0][0];});
/* global module, require */
var conf=require('./conf.js').conf;
var mysql = require('mysql');
var Promise = require('bluebird');
var using = Promise.using;
Promise.promisifyAll(require('mysql/lib/Connection').prototype);
Promise.promisifyAll(require('mysql/lib/Pool').prototype);
var pool = mysql.createPool(conf.mysql);
var getConnection = function () {
return pool.getConnectionAsync().disposer(function (connection) {
return connection.release();
});
};
var query = function (command) {
return using(getConnection(), function (connection) {
return connection.queryAsync(command);
});
};
function queryWrapper(q,a){
if(a){
return query(mysql.format(q,a));
}
else{
return query(mysql.format(q));
}
}
module.exports = {
query: queryWrapper
};
So far my attempts ad doing this with mysql2 haven't panned out. 到目前为止,我使用mysql2进行广告的尝试还没有完成。
Does anyone have any insights on how to convert this? 有人对如何转换此有任何见解吗? Thanks, Jegsar
谢谢,杰格萨尔
You can use mysql2-promise . 您可以使用mysql2-promise 。 It's a simple wrapper, using q, that promisifies mysql2.
这是一个使用q的简单包装器,可实现mysql2。 If you'd rather use Bluebird, you can look at how this wrapper was created and do it yourself.
如果您想使用Bluebird,则可以查看如何创建此包装器并自己完成。
node-mysql2 now has Promise api built in , and you can choose which promise implementation you want to use node-mysql2现在内置了Promise api ,您可以选择要使用的Promise实现。
var mysql = require('mysql2/promise');
mysql.createConnection({
Promise: require('bluebird'), // if not set, global Promise is used
user: 'foo',
password: 'bar',
database: 'baz'
})
.then((conn) => conn.query('select 1+1 as test'))
.then(([rows, fields]) => console.log(rows[0]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.