This adds a default hook to all models, which is run if the model does not define its own hook:
class User extends Model {}
User.init({}, { sequelize });
class Project extends Model {}
Project.init({}, {
hooks: {
beforeCreate: () => {
// Do other stuff
}
sequelize
User.create() // Runs the global hook
Project.create() // Runs its own hook (because the global hook is overwritten)
class User extends Model {}
User.init({}, { sequelize });
class Project extends Model {}
Project.init({}, {
hooks: {
beforeCreate: () => {
},
sequelize
});
User.create() // Runs the global hook
Project.create() // Runs its own hook, followed by the global hook
Permanent hooks may also be defined in Sequelize.options
:
beforeConnect(config)
afterConnect(connection, config)
beforeDisconnect(connection)
These hooks can be useful if you need to asynchronously obtain database credentials, or need to directly access the low-level database connection after it has been created.
These hooks may only be declared as a permanent global hook, as the connection pool is shared by all models.