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

Commit d4c4e698 by Mick Hansen

Merge pull request #3265 from sequelize/remove-events

refactor: remove events support
2 parents 2cd9db34 84a7655c
...@@ -3,8 +3,11 @@ ...@@ -3,8 +3,11 @@
- [BUG] instance.removeAssociation(s) do not fire the select query twice anymore - [BUG] instance.removeAssociation(s) do not fire the select query twice anymore
- [BUG] Error messages thrown by the db in languages other than english do not crash the app anymore (mysql, mariadb and postgres only) [#3567](https://github.com/sequelize/sequelize/pull/3567) - [BUG] Error messages thrown by the db in languages other than english do not crash the app anymore (mysql, mariadb and postgres only) [#3567](https://github.com/sequelize/sequelize/pull/3567)
- [FEATURE] All querys can be logged individually by inserting `logging: fn` in the query option. - [FEATURE] All querys can be logged individually by inserting `logging: fn` in the query option.
- [DEPRECATED] The query-chainer is deprecated and will be removed in version 2.2. Please use promises instead.
- [REMOVED] Events are no longer supported.
- The query-chainer is deprecated and will be removed in version 2.2. Please use promises instead. #### Backwards compatibility changes
- Events support have been removed so using `.on('succes')` or `.succes()` is no longer supported.
# 2.0.6 # 2.0.6
- [BUG] Don't update virtual attributes in Model.update. Fixes [#2860](https://github.com/sequelize/sequelize/issues/2860) - [BUG] Don't update virtual attributes in Model.update. Fixes [#2860](https://github.com/sequelize/sequelize/issues/2860)
......
...@@ -61,11 +61,8 @@ module.exports = (function() { ...@@ -61,11 +61,8 @@ module.exports = (function() {
var results = []; var results = [];
var request = new self.connection.lib.Request(self.sql, function(err) { var request = new self.connection.lib.Request(self.sql, function(err) {
promise.emit('sql', self.sql, self.connection.uuid);
if (err) { if (err) {
err.sql = sql; err.sql = sql;
reject(self.formatError(err)); reject(self.formatError(err));
} else { } else {
resolve(self.formatResults(results)); resolve(self.formatResults(results));
......
...@@ -29,8 +29,6 @@ module.exports = (function() { ...@@ -29,8 +29,6 @@ module.exports = (function() {
var promise = new Utils.Promise(function(resolve, reject) { var promise = new Utils.Promise(function(resolve, reject) {
self.connection.query(self.sql, function(err, results) { self.connection.query(self.sql, function(err, results) {
promise.emit('sql', self.sql, self.connection.uuid);
if (err) { if (err) {
err.sql = sql; err.sql = sql;
......
...@@ -102,7 +102,6 @@ module.exports = (function() { ...@@ -102,7 +102,6 @@ module.exports = (function() {
query.on('error', function(err) { query.on('error', function(err) {
receivedError = true; receivedError = true;
err.sql = sql; err.sql = sql;
promise.emit('sql', sql, self.client.uuid);
reject(self.formatError(err)); reject(self.formatError(err));
}); });
...@@ -111,7 +110,6 @@ module.exports = (function() { ...@@ -111,7 +110,6 @@ module.exports = (function() {
return; return;
} }
promise.emit('sql', self.sql, self.client.uuid);
resolve([rows, sql, result]); resolve([rows, sql, result]);
}); });
}).spread(function(rows, sql, result) { }).spread(function(rows, sql, result) {
......
...@@ -38,16 +38,10 @@ module.exports = (function() { ...@@ -38,16 +38,10 @@ module.exports = (function() {
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 ...
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) {
self.database[self.getDatabaseMethod()](self.sql, function(err, results) { self.database[self.getDatabaseMethod()](self.sql, function(err, results) {
// allow clients to listen to sql to do their own logging or whatnot
promise.emit('sql', self.sql, self.options.uuid);
if (err) { if (err) {
err.sql = self.sql; err.sql = self.sql;
reject(self.formatError(err)); reject(self.formatError(err));
......
'use strict'; 'use strict';
var Promise = require('bluebird/js/main/promise')() // use this syntax to be able to modify bluebird without affecting other users var Promise = require('bluebird')
, EventEmitter = require('events').EventEmitter , _then = Promise.prototype._then;
, proxyEventKeys = ['success', 'error', 'sql']
, Utils = require('./utils')
, deprecatedSeen = {}
, deprecated = function(message) {
if (deprecatedSeen[message]) return;
console.warn(message);
deprecatedSeen[message] = true;
};
/**
* 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.
*
* The main difference is that sequelize promises allows you to attach a listener that will be called with the generated SQL, each time a query is run.
*
* The sequelize promise class works seamlessly with other A+/thenable libraries, with one exception.
* If you want to propagate SQL events across `then`, `all` calls etc., you must use sequelize promises exclusively.
*
* @mixes https://github.com/petkaantonov/bluebird/blob/master/API.md
* @class 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;
};
var util = require('util');
util.inherits(SequelizePromise, 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) { Promise.prototype._then = function (didFulfill, didReject, didProgress, receiver, internalData) {
if (SequelizePromise.Sequelize.cls) { if (Promise.Sequelize.cls) {
var ns = SequelizePromise.Sequelize.cls; var ns = Promise.Sequelize.cls;
if (typeof didFulfill === 'function') didFulfill = ns.bind(didFulfill); if (typeof didFulfill === 'function') didFulfill = ns.bind(didFulfill);
if (typeof didReject === 'function') didReject = ns.bind(didReject); if (typeof didReject === 'function') didReject = ns.bind(didReject);
if (typeof didProgress === 'function') didProgress = ns.bind(didProgress); if (typeof didProgress === 'function') didProgress = ns.bind(didProgress);
} }
var ret = bluebirdThen.call(this, didFulfill, didReject, didProgress, receiver, internalData); return _then.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) {
var receiver = this._receiverAt(index);
bluebirdSettle.call(this, 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
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
*
* @param {String} evt
* @param {Function} fct
*
* @deprecated
*/
Promise.prototype.on = function(evt, fct) {
if (evt === 'success') {
this.then(fct);
}
else if (evt === 'error') {
this.then(null, fct);
}
else {
EventEmitter.prototype.on.call(this, evt, fct);
}
return this;
};
/**
* Emit an event from the emitter
* @param {string} type The type of event
* @param {any} value(s)* All other arguments will be passed to the event listeners
*
* @deprecated
*/
Promise.prototype.emit = function(evt) {
var args = arguments.length > 1 ? Array.prototype.slice.call(arguments, 1) : [];
if (evt === 'success') {
this.seqResolve.apply(this, args);
} else if (evt === 'error') {
this.seqReject.apply(this, args);
} else {
// Needed to transfer sql across .then() calls
if (evt === 'sql') {
if (!this.$sql) {
this.$sql = [];
}
this.$sql.push(args[0]);
}
EventEmitter.prototype.emit.apply(this, [evt].concat(args));
}
return this;
};
/**
* Listen for success events.
*
* ```js
* promise.success(function (result) {
* //...
* });
* ```
*
* @param {function} onSuccess
* @method success
* @alias ok
* @return this
*
* @deprecated
*/
Promise.prototype.success =
Promise.prototype.ok = function(fct) {
deprecated('success|ok() is deprecated and will be removed in 2.1, please use promise-style instead.');
if (fct.length > 1) {
return this.spread(fct);
} else {
return this.then(fct);
}
};
/**
* Listen for error events
*
* ```js
* promise.error(function (err) {
* //...
* });
* ```
*
* @param {function} onError
* @method error
* @alias fail
* @alias failure
* @return this
*
* @deprecated
*/
Promise.prototype.error =
Promise.prototype.failure =
Promise.prototype.fail = function(fct) {
deprecated('failure|fail|error() is deprecated and will be removed in 2.1, please use promise-style instead.');
return this.then(null, fct);
};
/**
* Listen for both success and error events.
*
* ```js
* promise.done(function (err, result) {
* //...
* });
* ```
*
* @param {function} onDone
* @method done
* @alias complete
* @return this
*
* @deprecated done(fct) is deprecated, done() is regular promise syntax to explicitly signal the end of a promise chain and will not be deprecated
*/
var bluebirdDone = Promise.prototype.done;
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);
}
if (fct.length > 2) {
deprecated('complete|done() is deprecatedand will be removed in 2.1, please use promise-style instead.');
return this.spread(function() {
fct.apply(null, [null].concat(Array.prototype.slice.call(arguments)));
}, fct);
} else {
return this.then(function() {
fct.apply(null, [null].concat(Array.prototype.slice.call(arguments)));
}, fct);
}
};
/*
* Attach a function that is called every time the function that created this emitter executes a query.
* @param {function} onSQL
* @return this
*/
Promise.prototype.sql = function(fct) {
this.on('sql', fct);
return this;
};
/**
* Proxy every event of this promise to another one.
*
* @param {SequelizePromise} promise The promise that should receive the events.
* @param {Object} [options]
* @param {Array} [options.events] An array of the events to proxy. Defaults to sql, error and success
* @return this
*
* @deprecated
*/
Promise.prototype.proxy = function(promise, options) {
options = Utils._.extend({
events: proxyEventKeys,
skipEvents: []
}, options || {});
options.events = Utils._.difference(options.events, options.skipEvents);
options.events.forEach(function(eventKey) {
this.on(eventKey, function() {
var args = [eventKey].concat([].slice.apply(arguments));
promise.emit.apply(promise, args);
});
}.bind(this));
return this;
};
Promise.prototype.proxySql = function(promise) {
return this.proxy(promise, {
events: ['sql']
});
}; };
module.exports = SequelizePromise; module.exports = Promise;
\ No newline at end of file
...@@ -182,11 +182,6 @@ module.exports = (function() { ...@@ -182,11 +182,6 @@ module.exports = (function() {
self.finishedEmits++; self.finishedEmits++;
self.fails.push(err); self.fails.push(err);
finish.call(self, 'emitterResults'); finish.call(self, 'emitterResults');
})
.on('sql', function(sql) {
if (self.eventEmitter) {
self.eventEmitter.emit('sql', sql);
}
}); });
}; };
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!