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. It can be used to remove the hook later or to order hooks based on some sort of priority system in the future.
fn Function The hook function

Aliases: hook


removeHook(hookType, name)

View code

Remove hook from the model

Params:

Name Type Description
hookType String
name String

hasHook(hookType)

View code

Check whether the mode has any hooks of this type

Params:

Name Type Description
hookType String

Aliases: hasHooks


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

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

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

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

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

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

Aliases: afterDelete


beforeRestore(name, fn)

View code

A hook that is run before restoring a single instance

Params:

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

afterRestore(name, fn)

View code

A hook that is run after restoring a single instance

Params:

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

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

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

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

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

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

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

Aliases: afterBulkDelete


beforeBulkRestore(name, fn)

View code

A hook that is run before restoring instances in bulk

Params:

Name Type Description
name String
fn Function A callback function that is called with options

afterBulkRestore(name, fn)

View code

A hook that is run after restoring instances in bulk

Params:

Name Type Description
name String
fn Function A callback function that is called with options

beforeBulkUpdate(name, fn)

View code

A hook that is run before updating instances in bulk

Params:

Name Type Description
name String
fn Function A callback function that is called with options

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

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

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

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

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

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

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

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

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

beforeSync(name, fn)

View code

A hook that is run before Model.sync call

Params:

Name Type Description
name String
fn Function A callback function that is called with options passed to Model.sync

afterSync(name, fn)

View code

A hook that is run after Model.sync call

Params:

Name Type Description
name String
fn Function A callback function that is called with options passed to Model.sync

beforeBulkSync(name, fn)

View code

A hook that is run before sequelize.sync call

Params:

Name Type Description
name String
fn Function A callback function that is called with options passed to sequelize.sync

afterBulkSync

View code

A hook that is run after sequelize.sync call

Params:

Name Type Description
name String
fn Function A callback function that is called with options passed to sequelize.sync

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