Options
All
  • Public
  • Public/Protected
  • All
Menu

This is the main class, the entry point to sequelize. To use it, you just need to import sequelize:

var Sequelize = require('sequelize');

In addition to sequelize, the connection library for the dialect you want to use should also be installed in your project. You don't need to import it however, as sequelize will take care of that.

Hierarchy

Index

Properties

AccessDeniedError

AccessDeniedError: AccessDeniedError

ConnectionError

ConnectionError: ConnectionError

ConnectionRefusedError

ConnectionRefusedError: ConnectionRefusedError

ConnectionTimedOutError

ConnectionTimedOutError: ConnectionTimedOutError

DatabaseError

DatabaseError: DatabaseError

Deferrable

Deferrable: Deferrable

A reference to the deferrable collection. Use this to access the different deferrable options.

Error

Error: BaseError

ExclusionConstraintError

ExclusionConstraintError: ExclusionConstraintError

ForeignKeyConstraintError

ForeignKeyConstraintError: ForeignKeyConstraintError

HostNotFoundError

HostNotFoundError: HostNotFoundError

HostNotReachableError

HostNotReachableError: HostNotReachableError

Instance

Instance: Instance<any>

A reference to the sequelize instance class.

InvalidConnectionError

InvalidConnectionError: InvalidConnectionError

Model

Model: Model<any, any>

A Model represents a table in the database. Sometimes you might also see it referred to as model, or simply as factory. This class should not be instantiated directly, it is created using sequelize.define, and already created models can be loaded using sequelize.import

Promise

Promise: PromiseConstructor

A modified version of bluebird promises, that allows listening for sql events

QueryTypes

QueryTypes: QueryTypes

Available query types for use with sequelize.query

Sequelize

Sequelize: SequelizeStatic

A reference to Sequelize constructor from sequelize. Useful for accessing DataTypes, Errors etc.

TimeoutError

TimeoutError: TimeoutError

Transaction

Transaction: TransactionStatic

A reference to the sequelize transaction class. Use this to access isolationLevels when creating a transaction

UniqueConstraintError

UniqueConstraintError: UniqueConstraintError

Utils

Utils: Utils

A reference to sequelize utilities. Most users will not need to use these utils directly. However, you might want to use Sequelize.Utils._, which is a reference to the lodash library, if you don't already have it imported in your project.

ValidationError

ValidationError: ValidationError

ValidationErrorItem

ValidationErrorItem: ValidationErrorItem

Validator

Validator: Validator

Exposes the validator.js object, so you can extend it with custom validation functions. The validator is exposed both on the instance, and on the constructor.

models

Defined models.

Methods

addHook

  • addHook(hookType: string, name: string, fn: Function): Hooks<any>
  • addHook(hookType: string, fn: Function): Hooks<any>
  • 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<any>

  • Parameters

    • hookType: string
    • fn: Function

    Returns Hooks<any>

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: any[], options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • instances: any[]
          • 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: any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • attributes: any
          • 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<any, any>): void
        • Parameters

          Returns void

    Returns void

  • Parameters

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

          Returns void

    Returns void

afterDelete

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

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

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

          Returns void

    Returns void

  • Parameters

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

          • instance: any
          • 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: any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • instance: any
          • 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: any[] | any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • instancesOrInstance: any[] | any
          • 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: any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • instance: any
          • 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: any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

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

          Returns void

    Returns void

and

  • and(...args: Array<string | Object>): and
  • An AND query

    Parameters

    • Rest ...args: Array<string | Object>

      Each argument will be joined by AND

    Returns and

asIs

authenticate

  • Test the connection by trying to authenticate

    Parameters

    • Optional options: QueryOptions

      Query Options for authentication

    Returns Promise<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: any[], options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • instances: any[]
          • 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: any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • attributes: any
          • 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: any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • instance: any
          • 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: any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • instance: any
          • 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: any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

          • instance: any
          • 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: any, options: Object, fn?: Function): void
        • Parameters

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

          Returns void

    Returns void

  • Parameters

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

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

          Returns void

    Returns void

cast

  • cast(val: any, type: string): cast
  • Creates a object representing a call to the cast function.

    Parameters

    • val: any

      The value to cast

    • type: string

      The type to cast it to

    Returns cast

close

  • close(): void
  • Close all connections used by this sequelize instance, and free all references so the instance can be garbage collected.

    Normally this is done on process exit, so you only need to call this method if you are creating multiple instances, and want to garbage collect some of them.

    Returns void

col

  • col(col: string): col
  • Creates a object representing a column in the DB. This is often useful in conjunction with sequelize.fn, since raw string arguments to fn will be escaped.

    Parameters

    • col: string

      The name of the column

    Returns col

condition

  • condition(attr: Object, logic: string | Object): where

createSchema

  • createSchema(schema: string, options: object): Promise<any>
  • Create a new database schema.

    Note,that this is a schema in the postgres sense of the word, not a database table. In mysql and sqlite, this command will do nothing.

    Parameters

    • schema: string

      Name of the schema

    • options: object

      Options supplied

      • Optional logging?: boolean | Function

        A function that logs sql queries, or false for no logging

    Returns Promise<any>

databaseVersion

  • databaseVersion(): Promise<string>
  • Returns the database version

    Returns Promise<string>

define

  • Define a new model, representing a table in the DB.

    The table columns are define by the hash that is given as the second argument. Each attribute of the hash represents a column. A short table definition might look like this:

    sequelize.define('modelName', {
        columnA: {
            type: Sequelize.BOOLEAN,
            validate: {
              is: ["[a-z]",'i'],        // will only allow letters
              max: 23,                  // only allow values <= 23
              isIn: {
                args: [['en', 'zh']],
                msg: "Must be English or Chinese"
              }
            },
            field: 'column_a'
            // Other attributes here
        },
        columnB: Sequelize.STRING,
        columnC: 'MY VERY OWN COLUMN TYPE'
    })
    
    sequelize.models.modelName // The model will now be available in models under the name given to define
    

    As shown above, column definitions can be either strings, a reference to one of the datatypes that are predefined on the Sequelize constructor, or an object that allows you to specify both the type of the column, and other attributes such as default values, foreign key constraints and custom setters and getters.

    For a list of possible data types, see http://docs.sequelizejs.com/en/latest/docs/models-definition/#data-types

    For more about getters and setters, see http://docs.sequelizejs.com/en/latest/docs/models-definition/#getters-setters

    For more about instance and class methods, see http://docs.sequelizejs.com/en/latest/docs/models-definition/#expansion-of-models

    For more about validation, see http://docs.sequelizejs.com/en/latest/docs/models-definition/#validations

    Type parameters

    • TInstance

    • TAttributes

    Parameters

    • modelName: string

      The name of the model. The model will be stored in sequelize.models under this name

    • attributes: DefineAttributes

      An object, where each attribute is a column of the table. Each column can be either a DataType, a string or a type-description object, with the properties described below:

    • Optional options: DefineOptions<TInstance>

      These options are merged with the default define options provided to the Sequelize constructor

    Returns Model<TInstance, TAttributes>

drop

  • Drop all tables defined through this sequelize instance. This is done by calling Model.drop on each model

    see

    {Model#drop} for options

    Parameters

    • Optional options: DropOptions

      The options passed to each call to Model.drop

    Returns Promise<any>

dropAllSchemas

  • dropAllSchemas(options: object): Promise<any>
  • Drop all schemas

    Note,that this is a schema in the postgres sense of the word, not a database table. In mysql and sqlite, this is the equivalent of drop all tables.

    Parameters

    • options: object

      Options supplied

      • Optional logging?: boolean | Function

        A function that logs sql queries, or false for no logging

    Returns Promise<any>

dropSchema

  • dropSchema(schema: string, options: object): Promise<any>
  • Drop a single schema

    Note,that this is a schema in the postgres sense of the word, not a database table. In mysql and sqlite, this drop a table matching the schema name

    Parameters

    • schema: string

      Name of the schema

    • options: object

      Options supplied

      • Optional logging?: boolean | Function

        A function that logs sql queries, or false for no logging

    Returns Promise<any>

escape

  • escape(value: string): string
  • Escape value.

    Parameters

    • value: string

      Value that needs to be escaped

    Returns string

fn

  • fn(fn: string, ...args: any[]): fn
  • Creates a object representing a database function. This can be used in search queries, both in where and order parts, and as default values in column definitions. If you want to refer to columns in your function, you should use sequelize.col, so that the columns are properly interpreted as columns and not a strings.

    Convert a user's username to upper case

    instance.updateAttributes({
      username: self.sequelize.fn('upper', self.sequelize.col('username'))
    })
    

    Parameters

    • fn: string

      The function you want to call

    • Rest ...args: any[]

      All further arguments will be passed as arguments to the function

    Returns fn

getDialect

  • getDialect(): string
  • Returns the specified dialect.

    Returns string

getQueryInterface

  • Returns an instance of QueryInterface.

    Returns QueryInterface

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<any>
  • hook(hookType: string, fn: Function): Hooks<any>
  • Parameters

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

    Returns Hooks<any>

  • Parameters

    • hookType: string
    • fn: Function

    Returns Hooks<any>

import

  • import<TInstance, TAttributes>(path: string, defineFunction?: function): Model<TInstance, TAttributes>
  • Imports a model defined in another file

    Imported models are cached, so multiple calls to import with the same path will not load the file multiple times

    See https://github.com/sequelize/sequelize/blob/master/examples/using-multiple-model-files/Task.js for a short example of how to define your models in separate files so that they can be imported by sequelize.import

    Type parameters

    • TInstance

    • TAttributes

    Parameters

    • path: string

      The path to the file that holds the model you want to import. If the part is relative, it will be resolved relatively to the calling file

    • Optional defineFunction: function

      An optional function that provides model definitions. Useful if you do not want to use the module root as the define function

    Returns Model<TInstance, TAttributes>

isDefined

  • isDefined(modelName: string): boolean
  • Checks whether a model with the given name is defined

    Parameters

    • modelName: string

      The name of a model defined with Sequelize.define

    Returns boolean

json

  • json(conditionsOrPath: string | Object, value?: string | number | boolean): json
  • Creates an object representing nested where conditions for postgres's json data-type.

    Parameters

    • conditionsOrPath: string | Object

      A hash containing strings/numbers or other nested hash, a string using dot notation or a string using postgres json syntax.

    • Optional value: string | number | boolean

      An optional value to compare against. Produces a string of the form " = ''".

    Returns json

literal

  • Creates a object representing a literal, i.e. something that will not be escaped.

    Parameters

    • val: any

    Returns literal

model

  • model<TInstance, TAttributes>(modelName: string): Model<TInstance, TAttributes>
  • Fetch a Model which is already defined

    Type parameters

    • TInstance

    • TAttributes

    Parameters

    • modelName: string

      The name of a model defined with Sequelize.define

    Returns Model<TInstance, TAttributes>

or

  • or(...args: Array<string | Object>): or
  • An OR query

    Parameters

    • Rest ...args: Array<string | Object>

      Each argument will be joined by OR

    Returns or

query

  • query(sql: string | object, options?: QueryOptions): Promise<any>
  • Execute a query on the DB, with the posibility to bypass all the sequelize goodness.

    By default, the function will return two arguments: an array of results, and a metadata object, containing number of affected rows etc. Use .spread to access the results.

    If you are running a type of query where you don't need the metadata, for example a SELECT query, you can pass in a query type to make sequelize format the results:

    sequelize.query('SELECT...').spread(function (results, metadata) {
      // Raw query - use spread
    });
    
    sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }).then(function (results) {
      // SELECT query - use then
    })
    

    Parameters

    • sql: string | object
    • Optional options: QueryOptions

      Query options

    Returns Promise<any>

removeHook

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

    Parameters

    • hookType: string
    • name: string

    Returns Hooks<any>

set

  • Execute a query which would set an environment or user variable. The variables are set per connection, so this function needs a transaction.

    Only works for MySQL.

    Parameters

    Returns Promise<any>

showAllSchemas

  • showAllSchemas(options: object): Promise<any>
  • Show all defined schemas

    Note,that this is a schema in the postgres sense of the word, not a database table. In mysql and sqlite, this will show all tables.

    Parameters

    • options: object

      Options supplied

      • Optional logging?: boolean | Function

        A function that logs sql queries, or false for no logging

    Returns Promise<any>

sync

  • Sync all defined models to the DB.

    Parameters

    Returns Promise<any>

transaction

  • transaction(options: TransactionOptions, autoCallback: function): Promise<any>
  • transaction(autoCallback: function): Promise<any>
  • transaction(): Promise<Transaction>
  • Start a transaction. When using transactions, you should pass the transaction in the options argument in order for the query to happen under that transaction

    sequelize.transaction().then(function (t) {
      return User.find(..., { transaction: t}).then(function (user) {
        return user.updateAttributes(..., { transaction: t});
      })
      .then(t.commit.bind(t))
      .catch(t.rollback.bind(t));
    })
    

    A syntax for automatically committing or rolling back based on the promise chain resolution is also supported:

    sequelize.transaction(function (t) { // Note that we use a callback rather than a promise.then()
      return User.find(..., { transaction: t}).then(function (user) {
        return user.updateAttributes(..., { transaction: t});
      });
    }).then(function () {
      // Commited
    }).catch(function (err) {
      // Rolled back
      console.error(err);
    });
    

    If you have CLS enabled, the transaction will automatically be passed to any query that runs witin the callback. To enable CLS, add it do your project, create a namespace and set it on the sequelize constructor:

    var cls = require('continuation-local-storage'),
        ns = cls.createNamespace('....');
    var Sequelize = require('sequelize');
    Sequelize.cls = ns;
    

    Note, that CLS is enabled for all sequelize instances, and all instances will share the same namespace

    Parameters

    Returns Promise<any>

  • Parameters

    Returns Promise<any>

  • Returns Promise<Transaction>

truncate

  • Truncate all tables defined through the sequelize models. This is done by calling Model.truncate() on each model.

    Parameters

    Returns Promise<any>

validate

where

  • where(attr: Object, comparator: string, logic: string | Object): where
  • where(attr: Object, logic: string | Object): where
  • A way of specifying attr = condition.

    The attr can either be an object taken from Model.rawAttributes (for example Model.rawAttributes.id or Model.rawAttributes.name). The attribute should be defined in your model definition. The attribute can also be an object from one of the sequelize utility functions (sequelize.fn, sequelize.col etc.)

    For string attributes, use the regular { where: { attr: something }} syntax. If you don't want your string to be escaped, use sequelize.literal.

    Parameters

    • attr: Object

      The attribute, which can be either an attribute object from Model.rawAttributes or a sequelize object, for example an instance of sequelize.fn. For simple string attributes, use the POJO syntax

    • comparator: string

      Comparator

    • logic: string | Object

      The condition. Can be both a simply type, or a further condition (.or, .and, .literal etc.)

    Returns where

  • Parameters

    • attr: Object
    • logic: string | Object

    Returns where

Generated using TypeDoc