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

Commit 316b2687 by Jan Aagaard Meier

Moved dependency from sequelize-bluebird to vanilla bluebird

1 parent c55127b2
...@@ -51,9 +51,7 @@ module.exports = (function() { ...@@ -51,9 +51,7 @@ module.exports = (function() {
this.sequelize.log('Executing (' + (this.client.uuid || 'default') + '): ' + this.sql); this.sequelize.log('Executing (' + (this.client.uuid || 'default') + '): ' + this.sql);
} }
return new Promise(function(resolve, reject) { var promise = new Promise(function(resolve, reject) {
var promise = this;
query.on('row', function(row) { query.on('row', function(row) {
rows.push(row); rows.push(row);
}); });
...@@ -219,7 +217,7 @@ module.exports = (function() { ...@@ -219,7 +217,7 @@ module.exports = (function() {
} }
}); });
return this; return promise;
}; };
Query.prototype.formatError = function (err) { Query.prototype.formatError = function (err) {
......
...@@ -34,15 +34,16 @@ module.exports = (function() { ...@@ -34,15 +34,16 @@ module.exports = (function() {
this.sequelize.log('Executing (' + (this.database.uuid || 'default') + '): ' + this.sql); this.sequelize.log('Executing (' + (this.database.uuid || 'default') + '): ' + this.sql);
} }
return new Utils.Promise(function(resolve) { promise = new Utils.Promise(function(resolve) {
var columnTypes = {}; var columnTypes = {};
promise = this;
self.database.serialize(function() { self.database.serialize(function() {
var executeSql = function() { var executeSql = function() {
if (self.sql.indexOf('-- ') === 0) { if (self.sql.indexOf('-- ') === 0) {
// the sql query starts with a comment. don't bother the server with that ... // the sql query starts with a comment. don't bother the server with that ...
promise.emit('sql', self.sql, self.options.uuid); Utils.tick(function () {
promise.emit('sql', self.sql, self.options.uuid);
});
return resolve(); return resolve();
} else { } else {
resolve(new Utils.Promise(function(resolve, reject) { resolve(new Utils.Promise(function(resolve, reject) {
...@@ -171,6 +172,8 @@ module.exports = (function() { ...@@ -171,6 +172,8 @@ module.exports = (function() {
} }
}); });
}); });
return promise;
}; };
Query.prototype.formatError = function (err) { Query.prototype.formatError = function (err) {
......
'use strict'; 'use strict';
var Promise = require('sequelize-bluebird') var Promise = require('bluebird/js/main/promise')() // use this syntax to be able to modify bluebird without affecting other users
, EventEmitter = require('events').EventEmitter , EventEmitter = require('events').EventEmitter
, proxyEventKeys = ['success', 'error', 'sql'] , proxyEventKeys = ['success', 'error', 'sql']
, Utils = require('./utils') , Utils = require('./utils')
...@@ -19,7 +19,82 @@ var Promise = require('sequelize-bluebird') ...@@ -19,7 +19,82 @@ var Promise = require('sequelize-bluebird')
* @mixes https://github.com/petkaantonov/bluebird/blob/master/API.md * @mixes https://github.com/petkaantonov/bluebird/blob/master/API.md
* @class Promise * @class Promise
*/ */
var SequelizePromise = Promise; var SequelizePromise = function (resolver) {
var self = this;
var promise = new Promise(function sequelizeResolver(resolve, reject) {
self.seqResolve = resolve;
self.seqReject = reject;
return resolver(resolve, reject);
});
promise.seqResolve = this.seqResolve;
promise.seqReject = this.seqReject;
promise.$sql = [];
return promise;
};
for (var method in Promise) {
if (Promise.hasOwnProperty(method)) {
SequelizePromise[method] = Promise[method];
}
}
var bluebirdThen = Promise.prototype._then;
Promise.prototype._then = function (didFulfill, didReject, didProgress, receiver, internalData) {
var ret = bluebirdThen.call(this, didFulfill, didReject, didProgress, receiver, internalData);
// Needed to transfer sql events accross .then() calls
if (ret && ret.emit) {
if (!ret.$sql) {
ret.$sql = [];
}
this.proxySql(ret);
}
return ret;
};
var bluebirdSettle = Promise.prototype._settlePromiseAt;
Promise.prototype._settlePromiseAt = function (index) {
bluebirdSettle.call(this, index);
var receiver = this._receiverAt(index);
if (this.$sql && receiver && receiver.emit) {
this.$sql.forEach(function (sql) {
receiver.emit("sql", sql);
});
}
};
var bluebirdAll = Promise.all;
SequelizePromise.all = function (promises) {
var ret = bluebirdAll.call(this, promises);
// Propagate sql events
var self = this;
if (Array.isArray(promises)) {
promises.forEach(function (promise) {
if (Promise.is(promise)) {
promise.on("sql", function (sql) {
ret.emit("sql", sql);
});
if (!promise.$sql) {
promise.$sql = [];
}
promise.$sql.forEach(function (sql) {
ret.emit("sql", sql);
});
}
});
}
return ret;
};
/** /**
* Listen for events, event emitter style. Mostly for backwards compat. with EventEmitter * Listen for events, event emitter style. Mostly for backwards compat. with EventEmitter
...@@ -27,7 +102,7 @@ var SequelizePromise = Promise; ...@@ -27,7 +102,7 @@ var SequelizePromise = Promise;
* @param {String} evt * @param {String} evt
* @param {Function} fct * @param {Function} fct
*/ */
SequelizePromise.prototype.on = function(evt, fct) { Promise.prototype.on = function(evt, fct) {
if (evt === 'success') { if (evt === 'success') {
this.then(fct); this.then(fct);
} }
...@@ -46,7 +121,7 @@ SequelizePromise.prototype.on = function(evt, fct) { ...@@ -46,7 +121,7 @@ SequelizePromise.prototype.on = function(evt, fct) {
* @param {string} type The type of event * @param {string} type The type of event
* @param {any} value(s)* All other arguments will be passed to the event listeners * @param {any} value(s)* All other arguments will be passed to the event listeners
*/ */
SequelizePromise.prototype.emit = function(evt) { Promise.prototype.emit = function(evt) {
var args = arguments.length > 1 ? Array.prototype.slice.call(arguments, 1) : []; var args = arguments.length > 1 ? Array.prototype.slice.call(arguments, 1) : [];
if (evt === 'success') { if (evt === 'success') {
...@@ -56,6 +131,9 @@ SequelizePromise.prototype.emit = function(evt) { ...@@ -56,6 +131,9 @@ SequelizePromise.prototype.emit = function(evt) {
} else { } else {
// Needed to transfer sql across .then() calls // Needed to transfer sql across .then() calls
if (evt === 'sql') { if (evt === 'sql') {
if (!this.$sql) {
this.$sql = [];
}
this.$sql.push(args[0]); this.$sql.push(args[0]);
} }
...@@ -79,8 +157,8 @@ SequelizePromise.prototype.emit = function(evt) { ...@@ -79,8 +157,8 @@ SequelizePromise.prototype.emit = function(evt) {
* @alias ok * @alias ok
* @return this * @return this
*/ */
SequelizePromise.prototype.success = Promise.prototype.success =
SequelizePromise.prototype.ok = function(fct) { Promise.prototype.ok = function(fct) {
deprecated('EventEmitter#success|ok is deprecated, please use promise-style instead.'); deprecated('EventEmitter#success|ok is deprecated, please use promise-style instead.');
if (fct.length > 1) { if (fct.length > 1) {
return this.spread(fct); return this.spread(fct);
...@@ -104,9 +182,9 @@ SequelizePromise.prototype.ok = function(fct) { ...@@ -104,9 +182,9 @@ SequelizePromise.prototype.ok = function(fct) {
* @alias failure * @alias failure
* @return this * @return this
*/ */
SequelizePromise.prototype.failure = Promise.prototype.error =
SequelizePromise.prototype.fail = Promise.prototype.failure =
SequelizePromise.prototype.error = function(fct) { Promise.prototype.fail = function(fct) {
deprecated('EventEmitter#failure|fail|error is deprecated, please use promise-style instead.'); deprecated('EventEmitter#failure|fail|error is deprecated, please use promise-style instead.');
return this.then(null, fct); return this.then(null, fct);
}; };
...@@ -126,8 +204,8 @@ SequelizePromise.prototype.error = function(fct) { ...@@ -126,8 +204,8 @@ SequelizePromise.prototype.error = function(fct) {
* @return this * @return this
*/ */
var bluebirdDone = Promise.prototype.done; var bluebirdDone = Promise.prototype.done;
SequelizePromise.prototype.done = Promise.prototype.done =
SequelizePromise.prototype.complete = function(fct) { Promise.prototype.complete = function(fct) {
if (!fct) { if (!fct) {
// If no callback is provided, map to the promise.done function, which explicitly ends a promise chain // If no callback is provided, map to the promise.done function, which explicitly ends a promise chain
return bluebirdDone.call(this); return bluebirdDone.call(this);
...@@ -150,7 +228,7 @@ SequelizePromise.prototype.complete = function(fct) { ...@@ -150,7 +228,7 @@ SequelizePromise.prototype.complete = function(fct) {
* @param {function} onSQL * @param {function} onSQL
* @return this * @return this
*/ */
SequelizePromise.prototype.sql = function(fct) { Promise.prototype.sql = function(fct) {
this.on('sql', fct); this.on('sql', fct);
return this; return this;
}; };
...@@ -163,7 +241,7 @@ SequelizePromise.prototype.sql = function(fct) { ...@@ -163,7 +241,7 @@ SequelizePromise.prototype.sql = function(fct) {
* @param {Array} [options.events] An array of the events to proxy. Defaults to sql, error and success * @param {Array} [options.events] An array of the events to proxy. Defaults to sql, error and success
* @return this * @return this
*/ */
SequelizePromise.prototype.proxy = function(promise, options) { Promise.prototype.proxy = function(promise, options) {
options = Utils._.extend({ options = Utils._.extend({
events: proxyEventKeys, events: proxyEventKeys,
skipEvents: [] skipEvents: []
...@@ -181,7 +259,7 @@ SequelizePromise.prototype.proxy = function(promise, options) { ...@@ -181,7 +259,7 @@ SequelizePromise.prototype.proxy = function(promise, options) {
return this; return this;
}; };
SequelizePromise.prototype.proxySql = function(promise) { Promise.prototype.proxySql = function(promise) {
return this.proxy(promise, { return this.proxy(promise, {
events: ['sql'] events: ['sql']
}); });
......
...@@ -870,7 +870,7 @@ module.exports = (function() { ...@@ -870,7 +870,7 @@ module.exports = (function() {
* ``` * ```
* *
* A syntax for automatically committing or rolling back based on the promise chain resolution is also supported: * A syntax for automatically committing or rolling back based on the promise chain resolution is also supported:
* *
* ```js * ```js
* sequelize.transaction(function (t) { // Note that we use a callback rather than a promise.then() * sequelize.transaction(function (t) { // Note that we use a callback rather than a promise.then()
* return User.find(..., { transaction: t}).then(function (user) { * return User.find(..., { transaction: t}).then(function (user) {
......
...@@ -38,7 +38,7 @@ ...@@ -38,7 +38,7 @@
"lodash": "~2.4.0", "lodash": "~2.4.0",
"moment": "~2.7.0", "moment": "~2.7.0",
"node-uuid": "~1.4.1", "node-uuid": "~1.4.1",
"sequelize-bluebird": "git://github.com/sequelize/bluebird.git#9df139af53c5d346ffd38df30fc9dc60c62a9c98", "bluebird": "2.3.2",
"sql": "~0.39.0", "sql": "~0.39.0",
"toposort-class": "~0.3.0", "toposort-class": "~0.3.0",
"validator": "~3.16.0" "validator": "~3.16.0"
......
...@@ -172,13 +172,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -172,13 +172,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
, firstCreated = first[1] , firstCreated = first[1]
, secondInstance = second[0] , secondInstance = second[0]
, secondCreated = second[1]; , secondCreated = second[1];
// Depending on execution order and MAGIC either the first OR the second call should return true // Depending on execution order and MAGIC either the first OR the second call should return true
expect(firstCreated ? !secondCreated : secondCreated).to.be.ok // XOR expect(firstCreated ? !secondCreated : secondCreated).to.be.ok // XOR
expect(firstInstance).to.be.ok; expect(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok; expect(secondInstance).to.be.ok;
expect(firstInstance.id).to.equal(secondInstance.id); expect(firstInstance.id).to.equal(secondInstance.id);
return transaction.commit(); return transaction.commit();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!