Options
All
  • Public
  • Public/Protected
  • All
Menu

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

Sequelize.define

Type parameters

  • TInstance

Hierarchy

Index

Methods

addHook

  • addHook(hookType: string, name: string, fn: Function): Hooks<TInstance>
  • addHook(hookType: string, fn: Function): Hooks<TInstance>
  • Add a hook to the model

    alias

    hook

    Parameters

    • 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

    Returns Hooks<TInstance>

  • Parameters

    • hookType: string
    • fn: Function

    Returns Hooks<TInstance>

afterBulkCreate

  • afterBulkCreate(name: string, fn: function): void
  • afterBulkCreate(fn: function): void
  • A hook that is run after creating instances in bulk

    name

    afterBulkCreate

    Parameters

    • name: string
    • fn: function

      A callback function that is called with instances, options

        • (instances: TInstance[], options: Object, fn?: Function): void
        • Parameters

          • instances: TInstance[]
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instances: TInstance[], options: Object, fn?: Function): void
        • Parameters

          • instances: TInstance[]
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

afterBulkDelete

  • afterBulkDelete(name: string, fn: function): void
  • afterBulkDelete(fn: function): void
  • Parameters

    • name: string
    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

afterBulkDestroy

  • afterBulkDestroy(name: string, fn: function): void
  • afterBulkDestroy(fn: function): void
  • A hook that is run after destroying instances in bulk

    alias

    afterBulkDelete

    Parameters

    • name: string
    • fn: function

      A callback function that is called with options

        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

afterBulkUpdate

  • afterBulkUpdate(name: string, fn: function): void
  • afterBulkUpdate(fn: function): void
  • A hook that is run after updating instances in bulk

    Parameters

    • name: string
    • fn: function

      A callback function that is called with options

        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

afterCreate

  • afterCreate(name: string, fn: function): void
  • afterCreate(fn: function): void
  • A hook that is run after creating a single instance

    Parameters

    • name: string
    • fn: function

      A callback function that is called with attributes, options

        • (attributes: TInstance, options: Object, fn?: Function): void
        • Parameters

          • attributes: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (attributes: TInstance, options: Object, fn?: Function): void
        • Parameters

          • attributes: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

afterDefine

  • afterDefine(name: string, fn: function): void
  • afterDefine(fn: function): void
  • A hook that is run after a define call

    Parameters

    • name: string
    • fn: function

      A callback function that is called with factory

        • (model: Model<TInstance, any>): void
        • Parameters

          • model: Model<TInstance, any>

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (model: Model<TInstance, any>): void
        • Parameters

          • model: Model<TInstance, any>

          Returns void

    Returns void

afterDelete

  • afterDelete(name: string, fn: function): void
  • afterDelete(fn: function): void
  • Parameters

    • name: string
    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

afterDestroy

  • afterDestroy(name: string, fn: function): void
  • afterDestroy(fn: function): void
  • A hook that is run after destroying a single instance

    alias

    afterDelete

    Parameters

    • name: string
    • fn: function

      A callback function that is called with instance, options

        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

afterFind

  • afterFind(name: string, fn: function): void
  • afterFind(fn: function): void
  • A hook that is run after a find (select) query

    Parameters

    • name: string
    • fn: function

      A callback function that is called with instance(s), options

        • (instancesOrInstance: TInstance[] | TInstance, options: Object, fn?: Function): void
        • Parameters

          • instancesOrInstance: TInstance[] | TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instancesOrInstance: TInstance[] | TInstance, options: Object, fn?: Function): void
        • Parameters

          • instancesOrInstance: TInstance[] | TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

afterInit

  • afterInit(name: string, fn: function): void
  • afterInit(fn: function): void
  • A hook that is run after Sequelize() call

    Parameters

    • name: string
    • fn: function

      A callback function that is called with sequelize

    Returns void

  • Parameters

    Returns void

afterUpdate

  • afterUpdate(name: string, fn: function): void
  • afterUpdate(fn: function): void
  • A hook that is run after updating a single instance

    Parameters

    • name: string
    • fn: function

      A callback function that is called with instance, options

        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

afterValidate

  • afterValidate(name: string, fn: function): void
  • afterValidate(fn: function): void
  • A hook that is run after validation

    Parameters

    • name: string
    • fn: function

      A callback function that is called with instance, options

        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeBulkCreate

  • beforeBulkCreate(name: string, fn: function): void
  • beforeBulkCreate(fn: function): void
  • A hook that is run before creating instances in bulk

    Parameters

    • name: string
    • fn: function

      A callback function that is called with instances, options

        • (instances: TInstance[], options: Object, fn?: Function): void
        • Parameters

          • instances: TInstance[]
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instances: TInstance[], options: Object, fn?: Function): void
        • Parameters

          • instances: TInstance[]
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeBulkDelete

  • beforeBulkDelete(name: string, fn: function): void
  • beforeBulkDelete(fn: function): void
  • Parameters

    • name: string
    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeBulkDestroy

  • beforeBulkDestroy(name: string, fn: function): void
  • beforeBulkDestroy(fn: function): void
  • A hook that is run before destroying instances in bulk

    alias

    beforeBulkDelete

    Parameters

    • name: string
    • fn: function

      A callback function that is called with options

        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeBulkUpdate

  • beforeBulkUpdate(name: string, fn: function): void
  • beforeBulkUpdate(fn: function): void
  • A hook that is run after updating instances in bulk

    Parameters

    • name: string
    • fn: function

      A callback function that is called with options

        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeCreate

  • beforeCreate(name: string, fn: function): void
  • beforeCreate(fn: function): void
  • A hook that is run before creating a single instance

    Parameters

    • name: string
    • fn: function

      A callback function that is called with attributes, options

        • (attributes: TInstance, options: Object, fn?: Function): void
        • Parameters

          • attributes: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (attributes: TInstance, options: Object, fn?: Function): void
        • Parameters

          • attributes: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeDefine

  • beforeDefine(name: string, fn: function): void
  • beforeDefine(fn: function): void
  • A hook that is run before a define call

    Parameters

    • name: string
    • fn: function

      A callback function that is called with attributes, options

    Returns void

  • Parameters

    Returns void

beforeDelete

  • beforeDelete(name: string, fn: function): void
  • beforeDelete(fn: function): void
  • Parameters

    • name: string
    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeDestroy

  • beforeDestroy(name: string, fn: function): void
  • beforeDestroy(fn: function): void
  • A hook that is run before destroying a single instance

    alias

    beforeDelete

    Parameters

    • name: string
    • fn: function

      A callback function that is called with instance, options

        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeFind

  • beforeFind(name: string, fn: function): void
  • beforeFind(fn: function): void
  • A hook that is run before a find (select) query

    Parameters

    • name: string
    • fn: function

      A callback function that is called with options

        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeFindAfterExpandIncludeAll

  • beforeFindAfterExpandIncludeAll(name: string, fn: function): void
  • beforeFindAfterExpandIncludeAll(fn: function): void
  • A hook that is run before a find (select) query, after any { include: {all: ...} } options are expanded

    Parameters

    • name: string
    • fn: function

      A callback function that is called with options

        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeFindAfterOptions

  • beforeFindAfterOptions(name: string, fn: function): void
  • beforeFindAfterOptions(fn: function): void
  • A hook that is run before a find (select) query, after all option parsing is complete

    Parameters

    • name: string
    • fn: function

      A callback function that is called with options

        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (options: Object, fn?: Function): void
        • Parameters

          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeInit

  • beforeInit(name: string, fn: function): void
  • beforeInit(fn: function): void
  • A hook that is run before Sequelize() call

    Parameters

    • name: string
    • fn: function

      A callback function that is called with config, options

        • (config: Object, options: Object): void
        • Parameters

          • config: Object
          • options: Object

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (config: Object, options: Object): void
        • Parameters

          • config: Object
          • options: Object

          Returns void

    Returns void

beforeUpdate

  • beforeUpdate(name: string, fn: function): void
  • beforeUpdate(fn: function): void
  • A hook that is run before updating a single instance

    Parameters

    • name: string
    • fn: function

      A callback function that is called with instance, options

        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

beforeValidate

  • beforeValidate(name: string, fn: function): void
  • beforeValidate(fn: function): void
  • A hook that is run before validation

    Parameters

    • name: string
    • fn: function

      A callback function that is called with instance, options

        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

  • Parameters

    • fn: function
        • (instance: TInstance, options: Object, fn?: Function): void
        • Parameters

          • instance: TInstance
          • options: Object
          • Optional fn: Function

          Returns void

    Returns void

hasHook

  • hasHook(hookType: string): boolean
  • Check whether the mode has any hooks of this type

    alias

    hasHooks

    Parameters

    • hookType: string

    Returns boolean

hasHooks

  • hasHooks(hookType: string): boolean
  • Parameters

    • hookType: string

    Returns boolean

hook

  • hook(hookType: string, name: string, fn: Function): Hooks<TInstance>
  • hook(hookType: string, fn: Function): Hooks<TInstance>
  • Parameters

    • hookType: string
    • name: string
    • fn: Function

    Returns Hooks<TInstance>

  • Parameters

    • hookType: string
    • fn: Function

    Returns Hooks<TInstance>

removeHook

  • removeHook(hookType: string, name: string): Hooks<TInstance>
  • Remove hook from the model

    Parameters

    • hookType: string
    • name: string

    Returns Hooks<TInstance>

Generated using TypeDoc