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

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>

    Example

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

    // with database, username, and password in the options object
    const sequelize = new Sequelize({ database, user, password, dialect: PostgresDialect });

    Example

    // with url
    import { MySqlDialect } from '@sequelize/mysql';

    const sequelize = new Sequelize({
    dialect: MySqlDialect,
    url: 'mysql://localhost:3306/database',
    })

    Example

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

#databaseVersion: undefined | string
#databaseVersionPromise: null | Promise<void> = null
#isClosed: boolean = false
#models: Set<ModelStatic> = ...
#transactionCls: undefined | AsyncLocalStorage<Transaction>
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>

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

  • Escape value to be used in raw SQL.

    If you are using this to use the value in a literal, consider using sql instead, which automatically escapes interpolated values.

    Parameters

    • value: unknown

      The value to escape

    • Optional options: EscapeOptions

    Returns string

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

    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>