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

Commit c33046b9 by Jan Aagaard Meier

Promises for hooks

1 parent 620d8e77
Notice: All 1.7.x changes are present in 2.0.x aswell Notice: All 1.7.x changes are present in 2.0.x aswell
# next # next
- [FEATURE] Hooks can now return promises
#### Breaking changes #### 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)`. - 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. - `.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.
......
...@@ -86,7 +86,7 @@ Hooks.runHooks = function() { ...@@ -86,7 +86,7 @@ Hooks.runHooks = function() {
hook = hook.fn hook = hook.fn
} }
hook.apply(self, fnArgs.concat(function() { var maybePromise = hook.apply(self, fnArgs.concat(function() {
tick++ tick++
if (!!arguments[0]) { if (!!arguments[0]) {
...@@ -96,6 +96,16 @@ Hooks.runHooks = function() { ...@@ -96,6 +96,16 @@ Hooks.runHooks = function() {
return run(hooks[tick]) return run(hooks[tick])
})) }))
if (Utils.Promise.is(maybePromise)) {
maybePromise.spread(function () {
tick++
resolveArgs = Array.prototype.slice.call(arguments)
return run(hooks[tick])
}, reject)
}
} }
run(hooks[tick]) run(hooks[tick])
...@@ -129,7 +139,7 @@ Hooks.addHook = function(hookType, name, fn) { ...@@ -129,7 +139,7 @@ Hooks.addHook = function(hookType, name, fn) {
} }
var method = function() { var method = function() {
fn.apply(this, Array.prototype.slice.call(arguments, 0, arguments.length-1).concat(arguments[arguments.length-1])) return fn.apply(this, Array.prototype.slice.call(arguments, 0, arguments.length-1).concat(arguments[arguments.length-1]))
} }
// Aliases // Aliases
......
...@@ -7219,4 +7219,48 @@ describe(Support.getTestDialectTeaser("Hooks"), function () { ...@@ -7219,4 +7219,48 @@ describe(Support.getTestDialectTeaser("Hooks"), function () {
}) })
}) })
}) })
describe('promises', function () {
beforeEach(function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING,
mood: {
type: DataTypes.ENUM,
values: ['happy', 'sad', 'neutral']
}
})
return this.User.sync({ force: true })
})
it('can return a promise and modify fields', function () {
var self = this
this.User.beforeBulkCreate(function (daos, fields) {
return self.sequelize.Promise.resolve([daos, ['username']])
})
return this.User.bulkCreate([
{username: 'Bob', mood: 'cold'},
{username: 'Tobi', mood: 'hot'}
], { fields: [], hooks: false }).success(function(bulkUsers) {
return self.User.all().success(function(users) {
expect(users[0].mood).to.equal(null)
expect(users[1].mood).to.equal(null)
})
})
})
it('can return an error by rejecting', function () {
var self = this
this.User.beforeCreate(function () {
return self.sequelize.Utils.Promise.reject("I'm afraid I can't let you do that");
})
return this.User.create({}).catch(function (err) {
expect(err).to.equal("I'm afraid I can't let you do that")
})
})
})
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!