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

Commit d8c32ff4 by Mick Hansen

feat(promises): more refactoring

1 parent f10c6929
Notice: All 1.7.x changes are present in 2.0.x aswell
# next
#### Breaking changes
- Sequelize now returns promises instead of its custom event emitter from most calls. This affects methods that return multiple values (like `findOrCreate` or `findOrInitialize`). If your current callbacks do not accept the 2nd success parameter you might be seeing an array as the first param. Either use `.spread()` for these methods or add another argument to your callback: `.success(instance)` -> `.success(instance, created)`.
- `.success()`/`.done()` and any other non promise methods are now deprecated (we will keep the codebase around for a few versions though). on('sql') persists for debugging purposes.
# v2.0.0-dev11
### Caution: This release contains many changes and is highly experimental
- [PERFORMANCE] increased build performance when using include, which speeds up findAll etc.
......
......@@ -113,15 +113,9 @@ module.exports = (function() {
options.transaction = fieldsOrOptions.transaction
}
return new Utils.CustomEventEmitter(function(emitter) {
association.target
.create(values, fieldsOrOptions)
.proxy(emitter, { events: ['error', 'sql'] })
.success(function(newAssociatedObject) {
instance[association.accessors.set](newAssociatedObject, options)
.proxy(emitter)
})
}).run()
return association.target.create(values, fieldsOrOptions).then(function(newAssociatedObject) {
return instance[association.accessors.set](newAssociatedObject, options)
})
}
return this
......
......@@ -400,7 +400,7 @@ module.exports = (function() {
* @param {Object} [options.fields] An alternative way of setting which fields should be persisted
* @param {Transaction} [options.transaction]
*
* @return {EventEmitter}
* @return {Promise}
*/
DAO.prototype.save = function(fieldsOrOptions, options) {
if (fieldsOrOptions instanceof Array) {
......@@ -585,7 +585,7 @@ module.exports = (function() {
* @param {Array} [options.skip] An array of strings. All properties that are in this array will not be validated
* @see {DAOValidator}
*
* @return {EventEmitter}
* @return {Promise}
*/
DAO.prototype.validate = function(options) {
return new DaoValidator(this, options).validate()
......
......@@ -139,7 +139,7 @@ SequelizePromise.prototype.emit = function(evt) {
if (evt === 'success') {
this.seqResolve.apply(this, args);
} else if (evt === 'error') {;
} else if (evt === 'error') {
this.seqReject.apply(this, args);
} else {
// Needed to transfer sql across .then() calls
......@@ -170,7 +170,11 @@ SequelizePromise.prototype.emit = function(evt) {
SequelizePromise.prototype.success =
SequelizePromise.prototype.ok = function(fct) {
return this.then(fct);
if (fct.length > 1) {
return this.spread(fct);
} else {
return this.then(fct);
}
}
/**
......
......@@ -569,9 +569,9 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
User.sync({ force: true }).success(function() {
sequelize.transaction(function(t) {
User.create({ username: 'foo' }, { transaction: t }).success(function() {
User.findOrInitialize({ username: 'foo' }).success(function(user1) {
User.findOrInitialize({ username: 'foo' }, { transaction: t }).success(function(user2) {
User.findOrInitialize({ username: 'foo' }, { foo: 'asd' }, { transaction: t }).success(function(user3) {
User.findOrInitialize({ username: 'foo' }).spread(function(user1) {
User.findOrInitialize({ username: 'foo' }, { transaction: t }).spread(function(user2) {
User.findOrInitialize({ username: 'foo' }, { foo: 'asd' }, { transaction: t }).spread(function(user3) {
expect(user1.isNewRecord).to.be.true
expect(user2.isNewRecord).to.be.false
expect(user3.isNewRecord).to.be.false
......@@ -592,7 +592,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
this.User.create({ username: 'Username' }).success(function (user) {
self.User.findOrInitialize({
username: user.username
}).success(function (_user, initialized) {
}).spread(function (_user, initialized) {
expect(_user.id).to.equal(user.id)
expect(_user.username).to.equal('Username')
expect(initialized).to.be.false
......@@ -608,7 +608,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
self.User.findOrInitialize({
username: user.username,
data: user.data
}).success(function (_user, initialized) {
}).spread(function (_user, initialized) {
expect(_user.id).to.equal(user.id)
expect(_user.username).to.equal('Username')
expect(_user.data).to.equal('data')
......@@ -626,7 +626,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
data: 'ThisIsData'
}
this.User.findOrInitialize(data, default_values).success(function(user, initialized) {
this.User.findOrInitialize(data, default_values).spread(function(user, initialized) {
expect(user.id).to.be.null
expect(user.username).to.equal('Username')
expect(user.data).to.equal('ThisIsData')
......
......@@ -67,7 +67,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
this.User.create(data).success(function (user) {
self.User.findOrCreate({
username: user.username
}).success(function (_user, created) {
}).spread(function (_user, created) {
expect(_user.id).to.equal(user.id)
expect(_user.username).to.equal('Username')
expect(created).to.be.false
......
......@@ -245,7 +245,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
it("should have no problem performing findOrCreate", function(done) {
this.ScopeMe.findOrCreate({username: 'fake'}).success(function(user) {
this.ScopeMe.findOrCreate({username: 'fake'}).spread(function(user) {
expect(user.username).to.equal('fake')
done()
})
......
......@@ -319,16 +319,6 @@ describe(Support.getTestDialectTeaser("Promise"), function () {
this.User.create({ id: 1, aNumber: 0, bNumber: 0 }).done(done)
})
it('with then', function (done) {
this.User
.findOrCreate({ id: 1})
.then(function(user) {
expect(user.id).to.equal(1)
expect(arguments.length).to.equal(1)
done()
})
})
describe('with spread', function () {
it('user not created', function (done) {
this.User
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!