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

Commit 3859bd57 by Jan Aagaard Meier

Testing out cls

1 parent f5e3448a
"use strict";
var Sequelize = require('./index');
var DataTypes = Sequelize;
var inflection = require('inflection');
var Bluebird = require('q');
var Promise = Sequelize.Promise;
var db, sequelize;
// db = sequelize = new Sequelize('sequelize_test', 'postgres', 'postgres', {
// dialect: 'postgres',
var sequelize = new Sequelize('sequelize_test', 'root', null, {
// dialect: 'sqlite',
// dialect: 'mariadb',
omitNull: true,
// logging: console.log,
// logging: false,
host: '127.0.0.1',
define: {
// freezeTableName:true,
// underscoredAll: true,
underscored: false,
timestamps: true,
}
});
var cls = require('continuation-local-storage');
var sequelize_cls = cls.getNamespace('sequelize');
var User = sequelize.define('User', {});
// var Project = sequelize.define('project', {});
sequelize.authenticate({
// logging: console.log,
force: true
}).then(function() {
// sequelize.Promise.resolve().then(function () {
sequelize.transaction(function () {
console.log('inside transaction 1');
console.log(sequelize_cls.get('transaction').id);
return User.findAll();
});
sequelize.transaction(function () {
console.log('inside transaction 2');
console.log(sequelize_cls.get('transaction').id);
return User.findAll();
});
sequelize.Promise.delay(2000).then(function () {
console.log('in a totally different context');
console.log(!!sequelize_cls.get('transaction'));
});
sequelize.transaction(function () {
console.log('inside transaction 3');
console.log(sequelize_cls.get('transaction').id);
return sequelize.Promise.delay(2000).then(function () {
console.log('inside transaction 3, very delayed');
console.log(sequelize_cls.get('transaction').id);
});
}).then(function () {
console.log('transaction done ')
console.log(!!sequelize_cls.get('transaction'));
});
sequelize.transaction(function () {
console.log('inside transaction 4');
console.log(sequelize_cls.get('transaction').id);
User.findAll();
return sequelize.Promise.delay(200).then(function () {
console.log('inside transaction 4, delayed');
console.log(sequelize_cls.get('transaction').id);
return User.findAll();
})
})
// sequelize.authenticate().then(function () {
// sequelize.transaction(function() {
// console.log('inside authenticate, inside transaction')
// console.log(sequelize_cls.get('transaction').id);
// return User.findAll();
// })
// })
}).then(function(user) {
}).catch(function (e) {
console.log('catcher!')
console.log(e);
}).done();
...@@ -677,12 +677,18 @@ module.exports = (function() { ...@@ -677,12 +677,18 @@ module.exports = (function() {
, tableNames = { }; , tableNames = { };
tableNames[this.getTableName(options)] = true; tableNames[this.getTableName(options)] = true;
var cls = require('continuation-local-storage')
, sequelize_cls = cls.getNamespace('sequelize');
options = optClone(options || {}); options = optClone(options || {});
options = Utils._.defaults(options, { options = Utils._.defaults(options, {
hooks: true hooks: true
}); });
if (!options.transaction && sequelize_cls.get('transaction')) {
options.transaction = sequelize_cls.get('transaction');
}
return Promise.bind(this).then(function() { return Promise.bind(this).then(function() {
conformOptions(options); conformOptions(options);
......
...@@ -11,6 +11,10 @@ var Promise = require('bluebird/js/main/promise')() // use this syntax to be abl ...@@ -11,6 +11,10 @@ var Promise = require('bluebird/js/main/promise')() // use this syntax to be abl
deprecatedSeen[message] = true; deprecatedSeen[message] = true;
}; };
var cls = require('continuation-local-storage')
, ns = cls.createNamespace('sequelize');
/** /**
* A slightly modified version of bluebird promises. This means that, on top of the methods below, you can also call all the methods listed on the link below. * A slightly modified version of bluebird promises. This means that, on top of the methods below, you can also call all the methods listed on the link below.
* *
...@@ -45,6 +49,15 @@ for (var method in Promise) { ...@@ -45,6 +49,15 @@ for (var method in Promise) {
} }
} }
// var bluebird_addcb = Promise.prototype._addCallbacks;
// Promise.prototype._addCallbacks = function (fulfill, reject, progress, promise, receiver) {
// if (typeof fulfill === 'function') fulfill = ns.bind(fulfill);
// if (typeof reject === 'function') reject = ns.bind(reject);
// if (typeof progress === 'function') progress = ns.bind(progress);
// return bluebird_addcb.call(this, fulfill, reject, progress, promise, receiver);
// };
var bluebirdThen = Promise.prototype._then; var bluebirdThen = Promise.prototype._then;
Promise.prototype._then = function (didFulfill, didReject, didProgress, receiver, internalData) { Promise.prototype._then = function (didFulfill, didReject, didProgress, receiver, internalData) {
var ret = bluebirdThen.call(this, didFulfill, didReject, didProgress, receiver, internalData); var ret = bluebirdThen.call(this, didFulfill, didReject, didProgress, receiver, internalData);
...@@ -60,7 +73,6 @@ Promise.prototype._then = function (didFulfill, didReject, didProgress, receiver ...@@ -60,7 +73,6 @@ Promise.prototype._then = function (didFulfill, didReject, didProgress, receiver
return ret; return ret;
}; };
var bluebirdSettle = Promise.prototype._settlePromiseAt; var bluebirdSettle = Promise.prototype._settlePromiseAt;
Promise.prototype._settlePromiseAt = function (index) { Promise.prototype._settlePromiseAt = function (index) {
bluebirdSettle.call(this, index); bluebirdSettle.call(this, index);
......
...@@ -664,15 +664,20 @@ module.exports = (function() { ...@@ -664,15 +664,20 @@ module.exports = (function() {
return Promise.reject(options.transaction.finished+' has been called on this transaction, you can no longer use it'); return Promise.reject(options.transaction.finished+' has been called on this transaction, you can no longer use it');
} }
return Promise.resolve( var cls = require('continuation-local-storage');
options.transaction ? options.transaction.connection : self.connectionManager.getConnection(options) var sequelize_cls = cls.getNamespace('sequelize');
).then(function (connection) { return sequelize_cls.bind(function () {
var query = new self.dialect.Query(connection, self, callee, options); sequelize_cls.set('test', true)
return query.run(sql).finally(function() { return Promise.resolve(
if (options.transaction) return; options.transaction ? options.transaction.connection : self.connectionManager.getConnection(options)
return self.connectionManager.releaseConnection(connection); ).then(function (connection) {
var query = new self.dialect.Query(connection, self, callee, options);
return query.run(sql).finally(function() {
if (options.transaction) return;
return self.connectionManager.releaseConnection(connection);
});
}); });
}); })();
}; };
/** /**
...@@ -793,7 +798,6 @@ module.exports = (function() { ...@@ -793,7 +798,6 @@ module.exports = (function() {
return Promise.reject('Database does not match sync match parameter'); return Promise.reject('Database does not match sync match parameter');
} }
} }
var when; var when;
if (options.force) { if (options.force) {
when = this.drop(options); when = this.drop(options);
......
'use strict'; 'use strict';
var Utils = require('./utils') var Utils = require('./utils')
, util = require('util'); , util = require('util')
, cls = require('continuation-local-storage');
/** /**
* The transaction object is used to identify a running transaction. It is created by calling `Sequelize.transaction()`. * The transaction object is used to identify a running transaction. It is created by calling `Sequelize.transaction()`.
...@@ -110,21 +111,28 @@ Transaction.prototype.rollback = function() { ...@@ -110,21 +111,28 @@ Transaction.prototype.rollback = function() {
}); });
}; };
var sequelize_cls = cls.getNamespace('sequelize');
Transaction.prototype.prepareEnvironment = function() { Transaction.prototype.prepareEnvironment = function() {
var self = this; var self = this;
return Utils.Promise.resolve( return sequelize_cls.bind(function () {
self.options.transaction ? self.options.transaction.connection : self.sequelize.connectionManager.getConnection({ uuid: self.id }) return Utils.Promise.resolve(
).then(function (connection) { self.options.transaction ? self.options.transaction.connection : self.sequelize.connectionManager.getConnection({ uuid: self.id })
self.connection = connection; ).then(function (connection) {
self.connection.uuid = self.id; self.connection = connection;
}).then(function () { self.connection.uuid = self.id;
return self.begin(); sequelize_cls.set('transaction', self);
}).then(function () { }).catch(function () {
return self.setIsolationLevel(); console.log('yes, this is a noop')
}).then(function () { sequelize_cls.set('transaction', self);
return self.setAutocommit(); }).then(function () {
}); return self.begin();
}).then(function () {
return self.setIsolationLevel();
}).then(function () {
return self.setAutocommit();
});
})();
}; };
Transaction.prototype.begin = function() { Transaction.prototype.begin = function() {
...@@ -150,5 +158,8 @@ Transaction.prototype.setIsolationLevel = function() { ...@@ -150,5 +158,8 @@ Transaction.prototype.setIsolationLevel = function() {
Transaction.prototype.cleanup = function() { Transaction.prototype.cleanup = function() {
this.connection.uuid = undefined; this.connection.uuid = undefined;
return this.sequelize.connectionManager.releaseConnection(this.connection);
return this.sequelize.connectionManager.releaseConnection(this.connection).tap(function () {
cls.getNamespace('sequelize').set('transaction', undefined);
});
}; };
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!