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

import { Sequelize } from '@sequelize/core';

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.

Type Parameters

Hierarchy (view full)

Constructors

Properties

Accessors

Methods

Constructors

Properties

#databaseVersion: undefined | string
#databaseVersionPromise: null | Promise<void> = null
#isClosed: boolean = false
#models: Set<ModelStatic> = ...
#transactionCls: undefined | AsyncLocalStorage<Transaction>
Sequelize: typeof Sequelize

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

afterAssociate: LegacyAddHookFunction<((data, options) => AsyncHookReturn)> = ...

Type declaration

afterBulkCreate: LegacyAddHookFunction<((instances, options) => AsyncHookReturn)> = ...

Type declaration

afterBulkDestroy: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

afterBulkRestore: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

afterBulkSync: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

afterBulkUpdate: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

afterConnect: LegacyAddHookFunction<((connection, config) => AsyncHookReturn)> = ...

Type declaration

afterCreate: LegacyAddHookFunction<((attributes, options) => AsyncHookReturn)> = ...

Type declaration

afterDefine: LegacyAddHookFunction<((model) => void)> = ...

Type declaration

afterDestroy: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

afterDisconnect: LegacyAddHookFunction<((connection) => AsyncHookReturn)> = ...

Type declaration

afterFind: LegacyAddHookFunction<((instancesOrInstance, options) => AsyncHookReturn)> = ...

Type declaration

afterPoolAcquire: LegacyAddHookFunction<((connection, options?) => AsyncHookReturn)> = ...

Type declaration

afterQuery: LegacyAddHookFunction<((options, query) => AsyncHookReturn)> = ...

Type declaration

afterRestore: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

afterSave: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

afterSync: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

afterUpdate: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

afterUpsert: LegacyAddHookFunction<((attributes, options) => AsyncHookReturn)> = ...

Type declaration

afterValidate: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

beforeAssociate: LegacyAddHookFunction<((data, options) => AsyncHookReturn)> = ...

Type declaration

beforeBulkCreate: LegacyAddHookFunction<((instances, options) => AsyncHookReturn)> = ...

Type declaration

beforeBulkDestroy: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

beforeBulkRestore: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

beforeBulkSync: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

beforeBulkUpdate: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

beforeConnect: LegacyAddHookFunction<((config) => AsyncHookReturn)> = ...

Type declaration

beforeCount: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

beforeCreate: LegacyAddHookFunction<((attributes, options) => AsyncHookReturn)> = ...

Type declaration

beforeDefine: LegacyAddHookFunction<((attributes, options) => void)> = ...

Type declaration

beforeDestroy: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

beforeDisconnect: LegacyAddHookFunction<((connection) => AsyncHookReturn)> = ...

Type declaration

beforeFind: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

beforeFindAfterExpandIncludeAll: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

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

      Parameters

      Returns AsyncHookReturn

      Deprecated

      use beforeFind instead

beforeFindAfterOptions: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

beforePoolAcquire: LegacyAddHookFunction<((options?) => AsyncHookReturn)> = ...

Type declaration

beforeQuery: LegacyAddHookFunction<((options, query) => AsyncHookReturn)> = ...

Type declaration

beforeRestore: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

beforeSave: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

beforeSync: LegacyAddHookFunction<((options) => AsyncHookReturn)> = ...

Type declaration

beforeUpdate: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

beforeUpsert: LegacyAddHookFunction<((attributes, options) => AsyncHookReturn)> = ...

Type declaration

beforeValidate: LegacyAddHookFunction<((instance, options) => AsyncHookReturn)> = ...

Type declaration

dialect: Dialect
hasHook: (<HookName>(this, hookName) => boolean) = ...

Type declaration

hasHooks: (<HookName>(this, hookName) => boolean) = ...

Type declaration

models: ModelSetView<Dialect> = ...
rawOptions: Options<Dialect>

The options that were used to create this Sequelize instance. These are an unmodified copy of the options passed to the constructor. They are not normalized or validated.

Mostly available for cloning the Sequelize instance. For other uses, we recommend using options instead.

removeHook: (<HookName>(this, hookName, listenerNameOrListener) => void) = ...

Type declaration

runHooks: LegacyRunHookFunction<SequelizeHooks<AbstractDialect<object, object>>, void> = ...
validationFailed: LegacyAddHookFunction<((instance, options, error) => AsyncHookReturn)> = ...

Type declaration

afterInit: LegacyAddHookFunction<((sequelize) => void)> = ...

Type declaration

    • (sequelize): void
    • A hook that is run at the end of the creation of a Sequelize instance.

      Parameters

      Returns void

beforeInit: LegacyAddHookFunction<((options) => void)> = ...

Type declaration

    • (options): void
    • A hook that is run at the beginning of the creation of a Sequelize instance.

      Parameters

      Returns void

hasHook: (<HookName>(this, hookName) => boolean) = ...

Type declaration

hasHooks: (<HookName>(this, hookName) => boolean) = ...

Type declaration

removeHook: (<HookName>(this, hookName, listenerNameOrListener) => void) = ...

Type declaration

Accessors

Methods

  • 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 Promise<void>

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

    The table columns are defined 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:

    class MyModel extends Model {}
    MyModel.init({
    columnA: {
    type: DataTypes.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: DataTypes.STRING,
    columnC: 'MY VERY OWN COLUMN TYPE'
    }, { sequelize })

    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 https://sequelize.org/docs/v7/other-topics/other-data-types

    For more about getters and setters, see https://sequelize.org/docs/v7/core-concepts/getters-setters-virtuals/

    For more about instance and class methods, see https://sequelize.org/docs/v7/core-concepts/model-basics/#taking-advantage-of-models-being-classes

    For more about validation, see https://sequelize.org/docs/v7/core-concepts/validations-and-constraints/

    Type Parameters

    Parameters

    • modelName: string

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

    • Optional attributes: ModelAttributes<M, TAttributes>

      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: ModelOptions<M>

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

    Returns ModelStatic<M>

  • Checks whether a model with the given name is defined

    Parameters

    • modelName: string

      The name of a model defined with Sequelize.define

    Returns boolean

    Deprecated

    use Sequelize#models instead.

  • 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<unknown>

  • We highly recommend using Sequelize#transaction instead. If you really want to use the manual solution, don't forget to commit or rollback your transaction once you are done with it.

    Transactions started by this method are not automatically passed to queries. You must pass the transaction object manually, even if the Sequelize "disableClsTransactions" option is false.

    Parameters

    Returns Promise<Transaction>

    Example

    try {
    const transaction = await sequelize.startUnmanagedTransaction();
    const user = await User.findOne(..., { transaction });
    await user.update(..., { transaction });
    await transaction.commit();
    } catch(err) {
    await transaction.rollback();
    }
  • Start a managed transaction: Sequelize will create a transaction, pass it to your callback, and commit it once the promise returned by your callback resolved, or execute a rollback if the promise rejects.

    try {
    await sequelize.transaction(() => {
    const user = await User.findOne(...);
    await user.update(...);
    });

    // By now, the transaction has been committed
    } catch {
    // If the transaction callback threw an error, the transaction has been rolled back
    }

    By default, Sequelize uses AsyncLocalStorage to automatically pass the transaction to all queries executed inside the callback (unless you already pass one or set the transaction option to null). This can be disabled by setting the Sequelize "disableClsTransactions" option to true. You will then need to pass transactions to your queries manually.

    const sequelize = new Sequelize({
    // ...
    disableClsTransactions: true,
    })

    await sequelize.transaction(transaction => {
    // transactions are not automatically passed around anymore, you need to do it yourself:
    const user = await User.findOne(..., { transaction });
    await user.update(..., { transaction });
    });

    If you want to manage your transaction yourself, use startUnmanagedTransaction.

    Type Parameters

    • T

    Parameters

    Returns Promise<T>

  • Type Parameters

    • T

    Parameters

    Returns Promise<T>