不要怂,就是干,撸起袖子干!

Commit 82bb9796 by Mick Hansen

feat(transactions): promisify transactions

1 parent 7f714c72
...@@ -21,6 +21,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell ...@@ -21,6 +21,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
- Model association calls (belongsTo/hasOne/hasMany) are no longer chainable. (this is to support being able to pass association references to include rather than model/as combinations) - Model association calls (belongsTo/hasOne/hasMany) are no longer chainable. (this is to support being able to pass association references to include rather than model/as combinations)
- `QueryInterface` no longer emits global events. This means you can no longer do things like `QueryInterface.on('showAllSchemas', function ... ` - `QueryInterface` no longer emits global events. This means you can no longer do things like `QueryInterface.on('showAllSchemas', function ... `
- `sequelize.showAllSchemas` now returns an array of schemas, instead of an array containinig an array of schemas - `sequelize.showAllSchemas` now returns an array of schemas, instead of an array containinig an array of schemas
- `sequelize.transaction()` now returns a promise rather than a instance of Sequelize.Transaction
# v2.0.0-dev11 # v2.0.0-dev11
### Caution: This release contains many changes and is highly experimental ### Caution: This release contains many changes and is highly experimental
......
...@@ -172,7 +172,11 @@ module.exports = (function() { ...@@ -172,7 +172,11 @@ module.exports = (function() {
}; };
ConnectorManager.prototype.setTimezone = function(client, timezone, callback) { ConnectorManager.prototype.setTimezone = function(client, timezone, callback) {
client.query("SET TIME ZONE '" + (timezone ||  'UTC') + "'").on('end', callback); client.query("SET TIME ZONE '" + (timezone ||  'UTC') + "'").on('error', function (err) {
callback(err);
}).on('end', function () {
callback();
});
}; };
ConnectorManager.prototype.disconnect = function() { ConnectorManager.prototype.disconnect = function() {
......
...@@ -747,22 +747,14 @@ module.exports = (function() { ...@@ -747,22 +747,14 @@ module.exports = (function() {
Sequelize.prototype.transaction = function(_options, _callback) { Sequelize.prototype.transaction = function(_options, _callback) {
var options = (typeof _options === 'function') ? {} : _options var options = (typeof _options === 'function') ? {} : _options
, callback = (typeof _options === 'function') ? _options : _callback , callback = (typeof _options === 'function') ? _options : _callback
, wantsError = (callback.length === 2) , wantsError = callback && callback.length === 2
, transaction = new Transaction(this, options); , transaction = new Transaction(this, options);
Utils.tick(function() { return transaction.prepareEnvironment().then(function () {
if (wantsError) { wantsError ? callback(null, transaction) : callback && callback(transaction);
transaction.error(function(err) { }).catch(function (err) {
callback(err, transaction); if (wantsError) callback(err);
}); });
}
transaction.prepareEnvironment(function() {
wantsError ? callback(null, transaction) : callback(transaction);
});
});
return transaction;
}; };
Sequelize.prototype.log = function() { Sequelize.prototype.log = function() {
......
...@@ -18,8 +18,6 @@ var Transaction = module.exports = function(sequelize, options) { ...@@ -18,8 +18,6 @@ var Transaction = module.exports = function(sequelize, options) {
}, options || {}); }, options || {});
}; };
util.inherits(Transaction, Utils.CustomEventEmitter);
/** /**
* The possible isolations levels to use when starting a transaction * The possible isolations levels to use when starting a transaction
* *
...@@ -71,7 +69,6 @@ Transaction.prototype.commit = function() { ...@@ -71,7 +69,6 @@ Transaction.prototype.commit = function() {
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
.commitTransaction(this, {}) .commitTransaction(this, {})
.proxy(this)
.done(this.cleanup.bind(this)); .done(this.cleanup.bind(this));
}; };
...@@ -86,53 +83,45 @@ Transaction.prototype.rollback = function() { ...@@ -86,53 +83,45 @@ Transaction.prototype.rollback = function() {
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
.rollbackTransaction(this, {}) .rollbackTransaction(this, {})
.proxy(this)
.done(this.cleanup.bind(this)); .done(this.cleanup.bind(this));
}; };
Transaction.prototype.prepareEnvironment = function(callback) { Transaction.prototype.prepareEnvironment = function() {
var self = this var self = this
, connectorManager = self.sequelize.transactionManager.getConnectorManager(this.id); , connectorManager = self.sequelize.transactionManager.getConnectorManager(this.id);
this.begin(function() { return this.begin().then(function () {
self.setIsolationLevel(function() { return self.setIsolationLevel();
self.setAutocommit(function() { }).then(function () {
connectorManager.afterTransactionSetup(callback); return self.setAutocommit();
}).then(function () {
return new Utils.Promise(function (resolve, reject) {
connectorManager.afterTransactionSetup(function (err, result) {
if (err) return reject(err);
return resolve(result);
}); });
}); });
}); });
}; };
// private
var onError = function(err) {
this.emit('error', err);
};
Transaction.prototype.begin = function(callback) { Transaction.prototype.begin = function(callback) {
this return this
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
.startTransaction(this, {}) .startTransaction(this, {});
.success(callback)
.error(onError.bind(this));
}; };
Transaction.prototype.setAutocommit = function(callback) { Transaction.prototype.setAutocommit = function(callback) {
this return this
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
.setAutocommit(this, this.options.autocommit) .setAutocommit(this, this.options.autocommit);
.success(callback)
.error(onError.bind(this));
}; };
Transaction.prototype.setIsolationLevel = function(callback) { Transaction.prototype.setIsolationLevel = function(callback) {
this return this
.sequelize .sequelize
.getQueryInterface() .getQueryInterface()
.setIsolationLevel(this, this.options.isolationLevel) .setIsolationLevel(this, this.options.isolationLevel);
.success(callback)
.error(onError.bind(this));
}; };
Transaction.prototype.cleanup = function() { Transaction.prototype.cleanup = function() {
......
...@@ -42,7 +42,9 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() { ...@@ -42,7 +42,9 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
Group.all({ transaction: t }).success(function(groups) { Group.all({ transaction: t }).success(function(groups) {
groups[0].getUser({ transaction: t }).success(function(associatedUser) { groups[0].getUser({ transaction: t }).success(function(associatedUser) {
expect(associatedUser).to.be.not.null expect(associatedUser).to.be.not.null
t.rollback().success(function() { done() }) t.rollback().success(function() {
done()
})
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!