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

Commit 6d94d5f4 by Mick Hansen

Merge pull request #2328 from sequelize/promise

Moved dependency from sequelize-bluebird to vanilla bluebird
2 parents 0a537b2a 316b2687
......@@ -51,9 +51,7 @@ module.exports = (function() {
this.sequelize.log('Executing (' + (this.client.uuid || 'default') + '): ' + this.sql);
}
return new Promise(function(resolve, reject) {
var promise = this;
var promise = new Promise(function(resolve, reject) {
query.on('row', function(row) {
rows.push(row);
});
......@@ -219,7 +217,7 @@ module.exports = (function() {
}
});
return this;
return promise;
};
Query.prototype.formatError = function (err) {
......
......@@ -34,15 +34,16 @@ module.exports = (function() {
this.sequelize.log('Executing (' + (this.database.uuid || 'default') + '): ' + this.sql);
}
return new Utils.Promise(function(resolve) {
promise = new Utils.Promise(function(resolve) {
var columnTypes = {};
promise = this;
self.database.serialize(function() {
var executeSql = function() {
if (self.sql.indexOf('-- ') === 0) {
// 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();
} else {
resolve(new Utils.Promise(function(resolve, reject) {
......@@ -171,6 +172,8 @@ module.exports = (function() {
}
});
});
return promise;
};
Query.prototype.formatError = function (err) {
......
'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
, proxyEventKeys = ['success', 'error', 'sql']
, Utils = require('./utils')
......@@ -19,7 +19,82 @@ var Promise = require('sequelize-bluebird')
* @mixes https://github.com/petkaantonov/bluebird/blob/master/API.md
* @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
......@@ -27,7 +102,7 @@ var SequelizePromise = Promise;
* @param {String} evt
* @param {Function} fct
*/
SequelizePromise.prototype.on = function(evt, fct) {
Promise.prototype.on = function(evt, fct) {
if (evt === 'success') {
this.then(fct);
}
......@@ -46,7 +121,7 @@ SequelizePromise.prototype.on = function(evt, fct) {
* @param {string} type The type of event
* @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) : [];
if (evt === 'success') {
......@@ -56,6 +131,9 @@ SequelizePromise.prototype.emit = function(evt) {
} else {
// Needed to transfer sql across .then() calls
if (evt === 'sql') {
if (!this.$sql) {
this.$sql = [];
}
this.$sql.push(args[0]);
}
......@@ -79,8 +157,8 @@ SequelizePromise.prototype.emit = function(evt) {
* @alias ok
* @return this
*/
SequelizePromise.prototype.success =
SequelizePromise.prototype.ok = function(fct) {
Promise.prototype.success =
Promise.prototype.ok = function(fct) {
deprecated('EventEmitter#success|ok is deprecated, please use promise-style instead.');
if (fct.length > 1) {
return this.spread(fct);
......@@ -104,9 +182,9 @@ SequelizePromise.prototype.ok = function(fct) {
* @alias failure
* @return this
*/
SequelizePromise.prototype.failure =
SequelizePromise.prototype.fail =
SequelizePromise.prototype.error = function(fct) {
Promise.prototype.error =
Promise.prototype.failure =
Promise.prototype.fail = function(fct) {
deprecated('EventEmitter#failure|fail|error is deprecated, please use promise-style instead.');
return this.then(null, fct);
};
......@@ -126,8 +204,8 @@ SequelizePromise.prototype.error = function(fct) {
* @return this
*/
var bluebirdDone = Promise.prototype.done;
SequelizePromise.prototype.done =
SequelizePromise.prototype.complete = function(fct) {
Promise.prototype.done =
Promise.prototype.complete = function(fct) {
if (!fct) {
// If no callback is provided, map to the promise.done function, which explicitly ends a promise chain
return bluebirdDone.call(this);
......@@ -150,7 +228,7 @@ SequelizePromise.prototype.complete = function(fct) {
* @param {function} onSQL
* @return this
*/
SequelizePromise.prototype.sql = function(fct) {
Promise.prototype.sql = function(fct) {
this.on('sql', fct);
return this;
};
......@@ -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
* @return this
*/
SequelizePromise.prototype.proxy = function(promise, options) {
Promise.prototype.proxy = function(promise, options) {
options = Utils._.extend({
events: proxyEventKeys,
skipEvents: []
......@@ -181,7 +259,7 @@ SequelizePromise.prototype.proxy = function(promise, options) {
return this;
};
SequelizePromise.prototype.proxySql = function(promise) {
Promise.prototype.proxySql = function(promise) {
return this.proxy(promise, {
events: ['sql']
});
......
......@@ -899,7 +899,7 @@ module.exports = (function() {
* ```
*
* A syntax for automatically committing or rolling back based on the promise chain resolution is also supported:
*
*
* ```js
* sequelize.transaction(function (t) { // Note that we use a callback rather than a promise.then()
* return User.find(..., { transaction: t}).then(function (user) {
......
......@@ -38,7 +38,7 @@
"lodash": "~2.4.0",
"moment": "~2.7.0",
"node-uuid": "~1.4.1",
"sequelize-bluebird": "git://github.com/sequelize/bluebird.git#9df139af53c5d346ffd38df30fc9dc60c62a9c98",
"bluebird": "2.3.2",
"sql": "~0.39.0",
"toposort-class": "~0.3.0",
"validator": "~3.16.0"
......
......@@ -172,13 +172,13 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
, firstCreated = first[1]
, secondInstance = second[0]
, secondCreated = second[1];
// 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(firstInstance).to.be.ok;
expect(secondInstance).to.be.ok;
expect(firstInstance.id).to.equal(secondInstance.id);
return transaction.commit();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!