1
過去一週裏,我一直在爲藍鳥承諾庫和MySQL而努力。我經常發現,假設我完全沉浸在他們的術語中,並且往往只給出一半的答案,或者根本沒有答案。讓我停下來試圖弄清楚如何使用它。節點/藍鳥/ MySQL交易
目前我正試圖執行一系列SQL命令來創建事務內的數據庫記錄。我使用此代碼:
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
從我讀過我的代碼應該工作。有人知道它有什麼問題嗎?
其中是'pool'定義? – flaviodesousa