beforeDestroy and afterDestroy hooks not working
See original GitHub issueHere 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
Issue Analytics
- State:
- Created 8 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
beforeBulkDestroy not finding model property to change
One way you could do this is defining a before/after Destroy hook: hooks: { beforeDestroy: (user, { transaction }) => { user.update({ ...
Read more >Hooks - Sequelize
Hooks (also known as lifecycle events), are functions which are called before and after calls in sequelize are executed. For example, if you...
Read more >hooks · Sequelize-docs
Hooks (also known as callbacks or lifecycle events), are functions which are called before and after calls in sequelize are executed. For example,...
Read more >Sequelize hooks explained with code examples
Learn how Sequelize hooks can be useful for manipulating your database. ... options) beforeDestroy(instance, options) beforeUpdate(instance, ...
Read more >DS#destroyAll - js-data
Override the default beforeDestroy hook. options.afterDestroy, function, Override the default afterDestroy hook. options.eagerEject, function, Whether to eject ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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()
)Before and after destroy is invoked when destroying a single instance
Use before and after bulkDestroy instead or pass individualHooks: true to destroy