节点/蓝鸟/ MySQL交易

在过去的一周里,我一直在为蓝鸟承诺库和MySQL而苦苦挣扎。 我经常发现,假设我完全沉浸在他们的术语中,并且往往只给出一半的答案,或者根本没有答案。 让我挂着试图找出如何使用它。

目前我正试图执行一系列的SQL命令来创build一个事务内的数据库logging。 我正在使用这个代码:

var Promise = require('bluebird'); var mysql = require('mysql'); Promise.promisifyAll(mysql); Promise.promisifyAll(require('mysql/lib/Connection').prototype); Promise.promisifyAll(require('mysql/lib/Pool').prototype); function getConnection() { return pool.getConnectionAsync().disposer(function (connection) { connection.release(); }); } function getTransaction(connection) { return connection.beginTransactionAsync().disposer(function (tx, promise) { if (promise.isFulfilled()) { tx.commitAsync(); } else { tx.rollbackAsync(); } }); } Database.prototype.addStory = function (projectId, title, text) { return Promise.using(getConnection(), function (connection) { return Promise.using(getTransaction(connection), function () { return connection.queryAsync('INSERT INTO story SELECT ?, MAX(storyNumber) + 1, ?, ?, 0 FROM story WHERE projectID = ?', [projectId, title, text, projectId]) .then(connection.queryAsync('select LAST_INSERT_ID()')) .then(function (rows) { debug("Returning story for %s", rows[0]); return getStory(connection, rows[0]); }); }); }); } 

而目前我收到这个错误:

 TypeError: tx.rollbackAsync is not a function 

从我读过的代码应该工作。 有谁知道它有什么问题吗?

谢谢你们的答案。

我现在已经find了Knex API,它用更less的代码解决了我所有的问题。