Here is what i tried :
var User = sequelize.define('user', {
firstName: Sequelize.STRING,
lastName: Sequelize.STRING
}, {
hooks: {
beforeDestroy: function(instance, options, cb) {
console.log('before destroy');
return cb();
},
afterDestroy: function(instance, options, cb) {
console.log('after destroy');
return cb();
},
beforeCreate: function(instance, options, cb) {
console.log('before Create');
return cb();
},
afterCreate: function(instance, options, cb) {
console.log('after Create');
return cb();
}
}
});
sequelize.sync({
force: false
}).
then(function() {
User.create({
firstName: 'e',
lastName: 'e'
}).then(function(user) {
return User.destroy({
where: {
id: user.id
}
});
});
});
In the console :
before Create
Executing (default): INSERT INTO users
(id
,firstName
,lastName
,updatedAt
,createdAt
) VALUES (DEFAULT,'e','e','2015-04-29 15:41:46','2015-04-29 15:41:46');
after Create
Executing (default): DELETE FROM users
WHERE id
= 7
Before and after destroy is invoked when destroying a single instance
Use before and after bulkDestroy instead or pass individualHooks: true to destroy
Thanks for the quick reply
I would like to add this since it could be useful:
To set individualHooks to default to true ( so that you don't have to go back and manually add the params on every call to Model.destroy()
)
beforeBulkDestroy: function(options){
options.individualHooks = true;
return options;
}
Most helpful comment
I would like to add this since it could be useful:
To set individualHooks to default to true ( so that you don't have to go back and manually add the params on every call to
Model.destroy()
)