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

Commit 6b4efd6e by ns3777k

add optional name arg to .hook function

1 parent 6e899e27
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
- [FIXED] $or/$and inside a where clause always expects the input to be an array [#3767](https://github.com/sequelize/sequelize/issues/3767) - [FIXED] $or/$and inside a where clause always expects the input to be an array [#3767](https://github.com/sequelize/sequelize/issues/3767)
- [ADDED] Unique constraints may now include custom error messages - [ADDED] Unique constraints may now include custom error messages
- [ADDED] It's possible now to remove a hook by name - [ADDED] It's possible now to remove a hook by name
- [ADDED] Hook name can be passed via the direct method [#3901](https://github.com/sequelize/sequelize/issues/3901)
# 3.2.0 # 3.2.0
- [FEATURE] Add support for new option `targetKey` in a belongs-to relationship for situations where the target key is not the id field. - [FEATURE] Add support for new option `targetKey` in a belongs-to relationship for situations where the target key is not the id field.
......
...@@ -89,7 +89,7 @@ User.beforeValidate(function(user, options) { ...@@ -89,7 +89,7 @@ User.beforeValidate(function(user, options) {
return sequelize.Promise.resolve(user) return sequelize.Promise.resolve(user)
}) })
User.afterValidate(function(user, options, fn) { User.afterValidate('myHookAfter', function(user, options, fn) {
user.username = 'Toni' user.username = 'Toni'
fn(null, user) fn(null, user)
}) })
......
...@@ -373,8 +373,8 @@ module.exports = { ...@@ -373,8 +373,8 @@ module.exports = {
var allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases)); var allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases));
allHooks.forEach(function(hook) { allHooks.forEach(function(hook) {
Model.prototype[hook] = function(callback) { Model.prototype[hook] = function(name, callback) {
return this.addHook(hook, callback); return this.addHook(hook, name, callback);
}; };
}); });
} }
......
...@@ -812,7 +812,7 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -812,7 +812,7 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
username: DataTypes.STRING, username: DataTypes.STRING,
mood: { mood: {
type: DataTypes.ENUM, type: DataTypes.ENUM,
values: ['happy', 'sad', 'neutral'] values: ['happy', 'sad', 'neutral', 'angry']
} }
}); });
...@@ -841,6 +841,26 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -841,6 +841,26 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
expect(afterHook).to.be.true; expect(afterHook).to.be.true;
}); });
}); });
it('should run hooks with and without name arg', function() {
var beforeHook = false
, beforeHook2 = false;
this.User.beforeCreate('myHookBefore', function(user, options, fn) {
beforeHook = true;
fn();
});
this.User.beforeCreate(function(user, options, fn) {
beforeHook2 = true;
fn();
});
return this.User.create({username: 'Jack', mood: 'angry'}).then(function() {
expect(beforeHook).to.be.true;
expect(beforeHook2).to.be.true;
});
});
}); });
describe('on error', function() { describe('on error', function() {
...@@ -919,6 +939,36 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -919,6 +939,36 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
expect(afterHook).to.be.true; expect(afterHook).to.be.true;
}); });
}); });
it('should run all hooks with and without name arg', function() {
var beforeHook = false
, afterHook = false;
this.User.beforeCreate('myHookBefore1', function(user, options, fn) {
beforeHook = 'hi';
fn();
});
this.User.beforeCreate(function(user, options, fn) {
beforeHook = true;
fn();
});
this.User.afterCreate('myHookBefore2', function(user, options, fn) {
afterHook = 'hi';
fn();
});
this.User.afterCreate(function(user, options, fn) {
afterHook = true;
fn();
});
return this.User.create({username: 'Toni', mood: 'happy'}).then(function() {
expect(beforeHook).to.be.true;
expect(afterHook).to.be.true;
});
});
}); });
describe('on error', function() { describe('on error', function() {
...@@ -1459,6 +1509,29 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -1459,6 +1509,29 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}); });
}); });
}); });
it('should run hooks with and without name arg', function() {
var beforeHook = false
, afterHook = false;
this.User.beforeUpdate(function(user, options, fn) {
beforeHook = true;
fn();
});
this.User.afterUpdate('myHookAfterUpdate', function(user, options, fn) {
afterHook = true;
fn();
});
return this.User.create({username: 'John', mood: 'sad'}).then(function(user) {
return user.updateAttributes({username: 'Jackie'}).then(function(user) {
expect(beforeHook).to.be.true;
expect(afterHook).to.be.true;
expect(user.username).to.equal('Jackie');
});
});
});
}); });
describe('on error', function() { describe('on error', function() {
...@@ -2064,6 +2137,28 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -2064,6 +2137,28 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}); });
}); });
}); });
it('should run hooks with and without name arg', function() {
var beforeHook = false
, afterHook = false;
this.User.beforeDestroy('myDestroyHook', function(user, options, fn) {
beforeHook = true;
fn();
});
this.User.afterDestroy(function(user, options, fn) {
afterHook = true;
fn();
});
return this.User.create({username: 'Jane', mood: 'sad'}).then(function(user) {
return user.destroy().then(function() {
expect(beforeHook).to.be.true;
expect(afterHook).to.be.true;
});
});
});
}); });
describe('on error', function() { describe('on error', function() {
...@@ -2596,6 +2691,29 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -2596,6 +2691,29 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
expect(afterBulk).to.be.true; expect(afterBulk).to.be.true;
}); });
}); });
it('should run hooks with and without name arg', function() {
var beforeBulk = false
, afterBulk = false;
this.User.beforeBulkCreate(function(daos, options, fn) {
beforeBulk = true;
fn();
});
this.User.afterBulkCreate('onBulkCreateHook', function(daos, options, fn) {
afterBulk = true;
fn();
});
return this.User.bulkCreate([
{username: 'Tamara', mood: 'sad'},
{username: 'Don', mood: 'sad'}
]).then(function() {
expect(beforeBulk).to.be.true;
expect(afterBulk).to.be.true;
});
});
}); });
describe('on error', function() { describe('on error', function() {
...@@ -3134,6 +3252,32 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -3134,6 +3252,32 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
}); });
}); });
}); });
it('should run hooks with and without name arg', function() {
var self = this
, beforeBulk = false
, afterBulk = false;
this.User.beforeBulkUpdate('onBulkUpdateHook', function(options, fn) {
beforeBulk = true;
fn();
});
this.User.afterBulkUpdate(function(options, fn) {
afterBulk = true;
fn();
});
return this.User.bulkCreate([
{username: 'Cheech', mood: 'sad'},
{username: 'Chong', mood: 'sad'}
]).then(function() {
return self.User.update({mood: 'happy'}, {where: {mood: 'sad'}}).then(function() {
expect(beforeBulk).to.be.true;
expect(afterBulk).to.be.true;
});
});
});
}); });
describe('on error', function() { describe('on error', function() {
...@@ -3715,6 +3859,26 @@ describe(Support.getTestDialectTeaser('Hooks'), function() { ...@@ -3715,6 +3859,26 @@ describe(Support.getTestDialectTeaser('Hooks'), function() {
expect(afterBulk).to.be.true; expect(afterBulk).to.be.true;
}); });
}); });
it('should run hooks with and without name arg', function() {
var beforeBulk = false
, afterBulk = false;
this.User.beforeBulkDestroy(function(options, fn) {
beforeBulk = true;
fn();
});
this.User.afterBulkDestroy('onBulkDestroyHook', function(options, fn) {
afterBulk = true;
fn();
});
return this.User.destroy({where: {username: 'Cheech', mood: 'sad'}}).then(function() {
expect(beforeBulk).to.be.true;
expect(afterBulk).to.be.true;
});
});
}); });
describe('on error', function() { describe('on error', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!