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 Summary)

Constructors

Properties

addHook: LegacyAddAnyHookFunction<
    SequelizeHooks<AbstractDialect<object, object>>,
> = ...
afterAssociate: LegacyAddHookFunction<
    (
        data: AfterAssociateEventData,
        options: AssociationOptions<any>,
    ) => AsyncHookReturn,
> = ...
afterBulkCreate: LegacyAddHookFunction<
    (
        instances: readonly Model<any, any>[],
        options: BulkCreateOptions<any>,
    ) => AsyncHookReturn,
> = ...
afterBulkDestroy: LegacyAddHookFunction<
    (options: DestroyOptions<any>) => AsyncHookReturn,
> = ...
afterBulkRestore: LegacyAddHookFunction<
    (options: RestoreOptions<any>) => AsyncHookReturn,
> = ...
afterBulkSync: LegacyAddHookFunction<(options: SyncOptions) => AsyncHookReturn> = ...
afterBulkUpdate: LegacyAddHookFunction<
    (options: UpdateOptions<any>) => AsyncHookReturn,
> = ...
afterConnect: LegacyAddHookFunction<
    (connection: AbstractConnection, config: object) => AsyncHookReturn,
> = ...
afterCreate: LegacyAddHookFunction<
    (attributes: Model, options: CreateOptions<any>) => AsyncHookReturn,
> = ...
afterDefine: LegacyAddHookFunction<(model: ModelStatic) => void> = ...
afterDestroy: LegacyAddHookFunction<
    (instance: Model, options: InstanceDestroyOptions) => AsyncHookReturn,
> = ...
afterDisconnect: LegacyAddHookFunction<(connection: unknown) => AsyncHookReturn> = ...
afterFind: LegacyAddHookFunction<
    (
        instancesOrInstance:
            | null
            | Model<any, any>
            | readonly Model<any, any>[],
        options: FindOptions<any>,
    ) => AsyncHookReturn,
> = ...
afterPoolAcquire: LegacyAddHookFunction<
    (
        connection: AbstractConnection,
        options?: AcquireConnectionOptions,
    ) => AsyncHookReturn,
> = ...
afterQuery: LegacyAddHookFunction<
    (options: QueryOptions, query: AbstractQuery) => AsyncHookReturn,
> = ...
afterRestore: LegacyAddHookFunction<
    (instance: Model, options: InstanceRestoreOptions) => AsyncHookReturn,
> = ...
afterSave: LegacyAddHookFunction<
    (
        instance: Model,
        options: InstanceUpdateOptions<any> | CreateOptions<any>,
    ) => AsyncHookReturn,
> = ...
afterSync: LegacyAddHookFunction<(options: SyncOptions) => AsyncHookReturn> = ...
afterUpdate: LegacyAddHookFunction<
    (
        instance: Model,
        options: InstanceUpdateOptions<any>,
    ) => AsyncHookReturn,
> = ...
afterUpsert: LegacyAddHookFunction<
    (
        attributes: [Model<any, any>, null | boolean],
        options: UpsertOptions<any>,
    ) => AsyncHookReturn,
> = ...
afterValidate: LegacyAddHookFunction<
    (instance: Model, options: ValidationOptions) => AsyncHookReturn,
> = ...
beforeAssociate: LegacyAddHookFunction<
    (
        data: BeforeAssociateEventData,
        options: AssociationOptions<any>,
    ) => AsyncHookReturn,
> = ...
beforeBulkCreate: LegacyAddHookFunction<
    (
        instances: Model<any, any>[],
        options: BulkCreateOptions<any>,
    ) => AsyncHookReturn,
> = ...
beforeBulkDestroy: LegacyAddHookFunction<
    (options: DestroyOptions<any>) => AsyncHookReturn,
> = ...
beforeBulkRestore: LegacyAddHookFunction<
    (options: RestoreOptions<any>) => AsyncHookReturn,
> = ...
beforeBulkSync: LegacyAddHookFunction<(options: SyncOptions) => AsyncHookReturn> = ...
beforeBulkUpdate: LegacyAddHookFunction<
    (options: UpdateOptions<any>) => AsyncHookReturn,
> = ...
beforeConnect: LegacyAddHookFunction<(config: object) => AsyncHookReturn> = ...
beforeCount: LegacyAddHookFunction<
    (options: CountOptions<any>) => AsyncHookReturn,
> = ...
beforeCreate: LegacyAddHookFunction<
    (attributes: Model, options: CreateOptions<any>) => AsyncHookReturn,
> = ...
beforeDefine: LegacyAddHookFunction<
    (attributes: ModelAttributes<any>, options: ModelOptions) => void,
> = ...
beforeDestroy: LegacyAddHookFunction<
    (instance: Model, options: InstanceDestroyOptions) => AsyncHookReturn,
> = ...
beforeDisconnect: LegacyAddHookFunction<
    (connection: AbstractConnection) => AsyncHookReturn,
> = ...
beforeFind: LegacyAddHookFunction<
    (options: FindOptions<any>) => AsyncHookReturn,
> = ...
beforeFindAfterExpandIncludeAll: LegacyAddHookFunction<
    (options: FindOptions<any>) => AsyncHookReturn,
> = ...
beforeFindAfterOptions: LegacyAddHookFunction<
    (options: FindOptions<any>) => AsyncHookReturn,
> = ...
beforePoolAcquire: LegacyAddHookFunction<
    (options?: AcquireConnectionOptions) => AsyncHookReturn,
> = ...
beforeQuery: LegacyAddHookFunction<
    (options: QueryOptions, query: AbstractQuery) => AsyncHookReturn,
> = ...
beforeRestore: LegacyAddHookFunction<
    (instance: Model, options: InstanceRestoreOptions) => AsyncHookReturn,
> = ...
beforeSave: LegacyAddHookFunction<
    (
        instance: Model,
        options: InstanceUpdateOptions<any> | CreateOptions<any>,
    ) => AsyncHookReturn,
> = ...
beforeSync: LegacyAddHookFunction<(options: SyncOptions) => AsyncHookReturn> = ...
beforeUpdate: LegacyAddHookFunction<
    (
        instance: Model,
        options: InstanceUpdateOptions<any>,
    ) => AsyncHookReturn,
> = ...
beforeUpsert: LegacyAddHookFunction<
    (attributes: Model, options: UpsertOptions<any>) => AsyncHookReturn,
> = ...
beforeValidate: LegacyAddHookFunction<
    (instance: Model, options: ValidationOptions) => AsyncHookReturn,
> = ...
dialect: Dialect
hasHook: <
    HookName extends keyof SequelizeHooks<AbstractDialect<object, object>>,
>(
    this: {
        hooks: HookHandler<SequelizeHooks<AbstractDialect<object, object>>>;
    },
    hookName: HookName,
) => boolean = ...
hasHooks: <
    HookName extends keyof SequelizeHooks<AbstractDialect<object, object>>,
>(
    this: {
        hooks: HookHandler<SequelizeHooks<AbstractDialect<object, object>>>;
    },
    hookName: HookName,
) => boolean = ...
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 extends keyof SequelizeHooks<AbstractDialect<object, object>>,
>(
    this: {
        hooks: HookHandler<SequelizeHooks<AbstractDialect<object, object>>>;
    },
    hookName: HookName,
    listenerNameOrListener:
        | string
        | SequelizeHooks<AbstractDialect<object, object>>[HookName],
) => void = ...
runHooks: LegacyRunHookFunction<
    SequelizeHooks<AbstractDialect<object, object>>,
    void,
> = ...
Sequelize: typeof Sequelize

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

validationFailed: LegacyAddHookFunction<
    (
        instance: Model,
        options: ValidationOptions,
        error: unknown,
    ) => AsyncHookReturn,
> = ...
afterInit: LegacyAddHookFunction<(sequelize: Sequelize) => void> = ...
beforeInit: LegacyAddHookFunction<
    (options: Options<AbstractDialect<object, object>>) => void,
> = ...
hasHook: <HookName extends keyof StaticSequelizeHooks>(
    this: { hooks: HookHandler<StaticSequelizeHooks> },
    hookName: HookName,
) => boolean = ...
hasHooks: <HookName extends keyof StaticSequelizeHooks>(
    this: { hooks: HookHandler<StaticSequelizeHooks> },
    hookName: HookName,
) => boolean = ...
removeHook: <HookName extends keyof StaticSequelizeHooks>(
    this: { hooks: HookHandler<StaticSequelizeHooks> },
    hookName: HookName,
    listenerNameOrListener: string | StaticSequelizeHooks[HookName],
) => void = ...

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>

  • Checks whether a model with the given name is defined

    Parameters

    • modelName: string

      The name of a model defined with Sequelize.define

    Returns boolean

    use Sequelize#models instead.

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Parameters

    Returns Promise<[undefined, number]>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Parameters

    Returns Promise<number>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Parameters

    Returns Promise<[number, number]>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Parameters

    Returns Promise<number>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Parameters

    Returns Promise<number>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Parameters

    Returns Promise<ColumnsDescription>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Parameters

    Returns Promise<RawConstraintDescription[]>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Type Parameters

    • M extends Model<any, any>

    Parameters

    Returns Promise<null | M>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Type Parameters

    • M extends Model<any, any>

    Parameters

    Returns Promise<M[]>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Type Parameters

    • T extends object

    Parameters

    Returns Promise<null | T>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Type Parameters

    • T extends object

    Parameters

    Returns Promise<T[]>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Parameters

    Returns Promise<null | { [key: string]: unknown }>

  • Execute a query on the DB, optionally bypassing 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 const [results, meta] = await ... 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:

    const [results, metadata] = await sequelize.query('SELECT...'); // Raw query - use array destructuring

    const results = await sequelize.query('SELECT...', { type: sequelize.QueryTypes.SELECT }); // SELECT query - no destructuring

    Parameters

    Returns Promise<[unknown[], unknown]>

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