Class SequelizeTypeScript<Dialect>Abstract

This is a temporary class used to progressively migrate the Sequelize class to TypeScript by slowly moving its functions here. Always use Sequelize instead.

Type Parameters

Hierarchy (View Summary)

Constructors

  • Instantiates sequelize.

    The options to connect to the database are specific to your dialect. Please refer to the documentation of your dialect on https://sequelize.org to learn about the options you can use.

    Type Parameters

    Parameters

    Returns SequelizeTypeScript<Dialect>

    import { PostgresDialect } from '@sequelize/postgres';

    // with database, username, and password in the options object
    const sequelize = new Sequelize({ database, user, password, dialect: PostgresDialect });
    // with url
    import { MySqlDialect } from '@sequelize/mysql';

    const sequelize = new Sequelize({
    dialect: MySqlDialect,
    url: 'mysql://localhost:3306/database',
    })
    // option examples
    import { MsSqlDialect } from '@sequelize/mssql';

    const sequelize = new Sequelize('database', 'username', 'password', {
    // the dialect of the database
    // It is a Dialect class exported from the dialect package
    dialect: MsSqlDialect,

    // custom host;
    host: 'my.server.tld',
    // for postgres, you can also specify an absolute path to a directory
    // containing a UNIX socket to connect over
    // host: '/sockets/psql_sockets'.

    // custom port;
    port: 12345,

    // disable logging or provide a custom logging function; default: console.log
    logging: false,

    // This option is specific to MySQL and MariaDB
    socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock',

    // the storage engine for sqlite
    // - default ':memory:'
    storage: 'path/to/database.sqlite',

    // disable inserting undefined values as NULL
    // - default: false
    omitNull: true,

    // A flag that defines if connection should be over ssl or not
    // Dialect-dependent, check the dialect documentation
    ssl: true,

    // Specify options, which are used when sequelize.define is called.
    // The following example:
    // define: { timestamps: false }
    // is basically the same as:
    // Model.init(attributes, { timestamps: false });
    // sequelize.define(name, attributes, { timestamps: false });
    // so defining the timestamps for each model will be not necessary
    define: {
    underscored: false,
    freezeTableName: false,
    charset: 'utf8',
    collate: 'utf8_general_ci'
    timestamps: true
    },

    // similar for sync: you can define this to always force sync for models
    sync: { force: true },

    // pool configuration used to pool database connections
    pool: {
    max: 5,
    idle: 30000,
    acquire: 60000,
    },

    // isolation level of each transaction
    // defaults to dialect default
    isolationLevel: IsolationLevel.REPEATABLE_READ
    })

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: WritableObjectDeep<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: WritableObjectDeep<FindOptions<any>>) => AsyncHookReturn,
> = ...
beforeFindAfterExpandIncludeAll: LegacyAddHookFunction<
    (options: WritableObjectDeep<FindOptions<any>>) => AsyncHookReturn,
> = ...
beforeFindAfterOptions: LegacyAddHookFunction<
    (options: WritableObjectDeep<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,
> = ...
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>

  • A slower alternative to truncate that uses DELETE FROM instead of TRUNCATE, but which works with foreign key constraints in dialects that don't support TRUNCATE CASCADE (postgres), or temporarily disabling foreign key constraints (mysql, mariadb, sqlite).

    Parameters

    Returns Promise<void>

  • Returns the transaction that is associated to the current asynchronous operation. This method returns undefined if no transaction is active in the current asynchronous operation, or if the Sequelize "disableClsTransactions" option is true.

    Returns undefined | Transaction

  • Throws if the database version hasn't been loaded yet. It is automatically loaded the first time Sequelize connects to your database.

    You can use Sequelize#authenticate to cause a first connection.

    Returns string

    current version of the dialect that is internally loaded

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

    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>