Mixin Hooks

View code Hooks are function that are called before and after (bulk-) creation/updating/deletion and validation. Hooks can be added to you models in three ways:

  1. By specifying them as options in sequelize.define
  2. By calling hook() with a string and your hook handler function
  3. By calling the function with the same name as the hook you want
// Method 1
sequelize.define(name, { attributes }, {
  hooks: {
    beforeBulkCreate: function () {
      // can be a single function
    },
    beforeValidate: [
      function () {},
      function() {} // Or an array of several
    ]
  }
})

// Method 2
Model.hook('afterDestroy', function () {})

// Method 3
Model.afterBulkUpdate(function () {})

See:


addHook(hooktype, [name], fn)

View code Add a hook to the model

Params:

Name Type Description
hooktype String
[name] String Provide a name for the hook function. This serves no purpose, other than the ability to be able to order hooks based on some sort of priority system in the future.
fn Function The hook function

Aliases: hook


beforeValidate(name, fn)

View code A hook that is run before validation

Params:

Name Type Description
name String
fn Function A callback function that is called with instance, options, callback(err)

afterValidate(name, fn)

View code A hook that is run after validation

Params:

Name Type Description
name String
fn Function A callback function that is called with instance, options, callback(err)

beforeCreate(name, fn)

View code A hook that is run before creating a single instance

Params:

Name Type Description
name String
fn Function A callback function that is called with attributes, options, callback(err)

afterCreate(name, fn)

View code A hook that is run after creating a single instance

Params:

Name Type Description
name String
fn Function A callback function that is called with attributes, options, callback(err)

beforeDestroy(name, fn)

View code A hook that is run before destroying a single instance

Params:

Name Type Description
name String
fn Function A callback function that is called with instance, options, callback(err)

Aliases: beforeDelete


afterDestroy(name, fn)

View code A hook that is run after destroying a single instance

Params:

Name Type Description
name String
fn Function A callback function that is called with instance, options, callback(err)

Aliases: afterDelete


beforeUpdate(name, fn)

View code A hook that is run before updating a single instance

Params:

Name Type Description
name String
fn Function A callback function that is called with instance, options, callback(err)

afterUpdate(name, fn)

View code A hook that is run after updating a single instance

Params:

Name Type Description
name String
fn Function A callback function that is called with instance, options, callback(err)

beforeBulkCreate(name, fn)

View code A hook that is run before creating instances in bulk

Params:

Name Type Description
name String
fn Function A callback function that is called with instances, options, callback(err)

afterBulkCreate(name, fn)

View code A hook that is run after creating instances in bulk

Params:

Name Type Description
name String
fn Function A callback function that is called with instances, options, callback(err)

beforeBulkDestroy(name, fn)

View code A hook that is run before destroying instances in bulk

Params:

Name Type Description
name String
fn Function A callback function that is called with options, callback(err)

Aliases: beforeBulkDelete


afterBulkDestroy(name, fn)

View code A hook that is run after destroying instances in bulk

Params:

Name Type Description
name String
fn Function A callback function that is called with options, callback(err)

Aliases: afterBulkDelete


beforeBulkUpdate(name, fn)

View code A hook that is run after updating instances in bulk

Params:

Name Type Description
name String
fn Function A callback function that is called with options, callback(err)

afterBulkUpdate(name, fn)

View code A hook that is run after updating instances in bulk

Params:

Name Type Description
name String
fn Function A callback function that is called with options, callback(err)

beforeFind(name, fn)

View code A hook that is run before a find (select) query

Params:

Name Type Description
name String
fn Function A callback function that is called with options, callback(err)

beforeFindAfterExpandIncludeAll(name, fn)

View code A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded

Params:

Name Type Description
name String
fn Function A callback function that is called with options, callback(err)

beforeFindAfterOptions(name, fn)

View code A hook that is run before a find (select) query, after all option parsing is complete

Params:

Name Type Description
name String
fn Function A callback function that is called with options, callback(err)

afterFind(name, fn)

View code A hook that is run after a find (select) query

Params:

Name Type Description
name String
fn Function A callback function that is called with instance(s), options, callback(err)

beforeDefine(name, fn)

View code A hook that is run before a define call

Params:

Name Type Description
name String
fn Function A callback function that is called with attributes, options, callback(err)

afterDefine(name, fn)

View code A hook that is run after a define call

Params:

Name Type Description
name String
fn Function A callback function that is called with factory, callback(err)

beforeInit(name, fn)

View code A hook that is run before Sequelize() call

Params:

Name Type Description
name String
fn Function A callback function that is called with config, options, callback(err)

afterInit(name, fn)

View code A hook that is run after Sequelize() call

Params:

Name Type Description
name String
fn Function A callback function that is called with sequelize, callback(err)

This document is automatically generated based on source code comments. Please do not edit it directly, as your changes will be ignored. Please write on IRC, open an issue or a create a pull request if you feel something can be improved. For help on how to write source code documentation see JSDoc and dox