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:

  • By specifying them as options in sequelize.define
  • By calling hook() with a string and your hook handler function
  • By calling the function with the same name as the hook you want
  1. // Method 1
  2. sequelize.define(name, { attributes }, {
  3. hooks: {
  4. beforeBulkCreate: function () {
  5. // can be a single function
  6. },
  7. beforeValidate: [
  8. function () {},
  9. function() {} // Or an array of several
  10. ]
  11. }
  12. })
  13. // Method 2
  14. Model.hook('afterDestroy', function () {})
  15. // Method 3
  16. Model.afterBulkUpdate(function () {})

See:


addHook(hooktype, [name], fn)

View code

Add a hook to the model

Params:

NameTypeDescription
hooktypeString
[name]StringProvide 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.
fnFunctionThe hook function

Aliases: hook


removeHook(hookType, name)

View code

Remove hook from the model

Params:

NameTypeDescription
hookTypeString
nameString

hasHook(hookType)

View code

Check whether the mode has any hooks of this type

Params:

NameTypeDescription
hookTypeString

Aliases: hasHooks


beforeValidate(name, fn)

View code

A hook that is run before validation

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instance, options

afterValidate(name, fn)

View code

A hook that is run after validation

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instance, options

validationFailed(name, fn)

View code

A hook that is run when validation fails

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instance, options, error. Error is the SequelizeValidationError. If the callback throws an error, it will replace the original validation error.

beforeCreate(name, fn)

View code

A hook that is run before creating a single instance

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with attributes, options

afterCreate(name, fn)

View code

A hook that is run after creating a single instance

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with attributes, options

beforeDestroy(name, fn)

View code

A hook that is run before destroying a single instance

Params:

NameTypeDescription
nameString
fnFunctionA 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:

NameTypeDescription
nameString
fnFunctionA 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:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instance, options

afterRestore(name, fn)

View code

A hook that is run after restoring a single instance

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instance, options

beforeUpdate(name, fn)

View code

A hook that is run before updating a single instance

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instance, options

afterUpdate(name, fn)

View code

A hook that is run after updating a single instance

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instance, options

beforeBulkCreate(name, fn)

View code

A hook that is run before creating instances in bulk

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instances, options

afterBulkCreate(name, fn)

View code

A hook that is run after creating instances in bulk

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instances, options

beforeBulkDestroy(name, fn)

View code

A hook that is run before destroying instances in bulk

Params:

NameTypeDescription
nameString
fnFunctionA 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:

NameTypeDescription
nameString
fnFunctionA 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:

NameTypeDescription
nameString
fnFunctionA callback function that is called with options

afterBulkRestore(name, fn)

View code

A hook that is run after restoring instances in bulk

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with options

beforeBulkUpdate(name, fn)

View code

A hook that is run before updating instances in bulk

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with options

afterBulkUpdate(name, fn)

View code

A hook that is run after updating instances in bulk

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with options

beforeFind(name, fn)

View code

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

Params:

NameTypeDescription
nameString
fnFunctionA 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:

NameTypeDescription
nameString
fnFunctionA 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:

NameTypeDescription
nameString
fnFunctionA callback function that is called with options

afterFind(name, fn)

View code

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

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with instance(s), options

beforeCount(name, fn)

View code

A hook that is run before a count query

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with options

beforeDefine(name, fn)

View code

A hook that is run before a define call

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with attributes, options

afterDefine(name, fn)

View code

A hook that is run after a define call

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with factory

beforeInit(name, fn)

View code

A hook that is run before Sequelize() call

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with config, options

afterInit(name, fn)

View code

A hook that is run after Sequelize() call

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with sequelize

beforeConnect(name, fn)

View code

A hook that is run before a connection is created

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with config passed to connection

afterConnect(name, fn)

View code

A hook that is run after a connection is created

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with the connection object and thye config passed to connection

beforeSync(name, fn)

View code

A hook that is run before Model.sync call

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with options passed to Model.sync

beforeConnect(name, fn)

View code

A hook that is run before a connection is created

Params:

NameTypeDescription
nameString
fnFunctionA callback function that is called with config passed to connection

beforeSync(name, fn)

View code

A hook that is run before Model.sync call

Params:

NameTypeDescription
nameString
fnFunctionA 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:

NameTypeDescription
nameString
fnFunctionA 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:

NameTypeDescription
nameString
fnFunctionA callback function that is called with options passed to sequelize.sync

afterBulkSync

View code

A hook that is run after sequelize.sync call

Params:

NameTypeDescription
nameString
fnFunctionA 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