This adds a default hook to all models, which is run if the model does not define its own hook:

    1. class User extends Model {}
    2. User.init({}, { sequelize });
    3. class Project extends Model {}
    4. Project.init({}, {
    5. hooks: {
    6. beforeCreate: () => {
    7. // Do other stuff
    8. }
    9. sequelize
    10. User.create() // Runs the global hook
    11. Project.create() // Runs its own hook (because the global hook is overwritten)
    1. class User extends Model {}
    2. User.init({}, { sequelize });
    3. class Project extends Model {}
    4. Project.init({}, {
    5. hooks: {
    6. beforeCreate: () => {
    7. },
    8. sequelize
    9. });
    10. User.create() // Runs the global hook
    11. Project.create() // Runs its own hook, followed by the global hook

    Permanent hooks may also be defined in Sequelize.options:

    1. beforeConnect(config)
    2. afterConnect(connection, config)
    3. 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.