Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Transaction

The transaction object is used to identify a running transaction. It is created by calling Sequelize.transaction(). To run a query under a transaction, you should pass the transaction in the options object.

see

{Sequelize.transaction}

Hierarchy

  • Transaction

Index

Constructors

  • Creates a new transaction instance

    Parameters

    Returns Transaction

Properties

_afterCommitHooks: any
connection: any
finished: any
id: any
name: any
options: any
parent: any
savepoints: any
sequelize: Sequelize

Accessors

  • get LOCK(): typeof LOCK
  • Same as Transaction.LOCK, but can also be called on instances of transactions to get possible options for row locking directly from the instance.

    Returns typeof LOCK

  • Isolation levels can be set per-transaction by passing options.isolationLevel to sequelize.transaction. Sequelize uses the default isolation level of the database, you can override this by passing options.isolationLevel in Sequelize constructor options.

    Pass in the desired level as the first argument:

    example

    try { const result = await sequelize.transaction({isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE}, transaction => { // your transactions }); // transaction has been committed. Do something after the commit if required. } catch(err) { // do something with the err. }

    property

    READ_UNCOMMITTED

    property

    READ_COMMITTED

    property

    REPEATABLE_READ

    property

    SERIALIZABLE

    Returns typeof ISOLATION_LEVELS

  • get LOCK(): typeof LOCK
  • Possible options for row locking. Used in conjunction with find calls:

    example

    // t1 is a transaction Model.findAll({ where: ..., transaction: t1, lock: t1.LOCK... });

    example

    Postgres also supports specific locks while eager loading by using OF: UserModel.findAll({ where: ..., include: [TaskModel, ...], transaction: t1, lock: { level: t1.LOCK..., of: UserModel } });

    UserModel will be locked but TaskModel won't!

    example

    You can also skip locked rows: // t1 is a transaction Model.findAll({ where: ..., transaction: t1, lock: true, skipLocked: true });

    The query will now return any rows that aren't locked by another transaction

    property

    UPDATE

    property

    SHARE

    property

    KEY_SHARE Postgres 9.3+ only

    property

    NO_KEY_UPDATE Postgres 9.3+ only

    Returns typeof LOCK

    possible options for row locking

  • Types can be set per-transaction by passing options.type to sequelize.transaction. Default to DEFERRED but you can override the default type by passing options.transactionType in new Sequelize. Sqlite only.

    Pass in the desired level as the first argument:

    example

    try { await sequelize.transaction({ type: Sequelize.Transaction.TYPES.EXCLUSIVE }, transaction => { // your transactions }); // transaction has been committed. Do something after the commit if required. } catch(err) { // do something with the err. }

    property

    DEFERRED

    property

    IMMEDIATE

    property

    EXCLUSIVE

    Returns typeof TRANSACTION_TYPES

Methods

  • _clearCls(): void
  • Returns void

  • afterCommit(fn: AfterTransactionCommitCallback): Transaction
  • Adds a hook that is run after a transaction is committed.

    name

    afterCommit

    memberof

    Sequelize.Transaction

    Parameters

    • fn: AfterTransactionCommitCallback

      A callback function that is called with the committed transaction

    Returns Transaction

  • begin(): Promise<void>
  • Returns Promise<void>

  • cleanup(): Promise<void>
  • Returns Promise<void>

  • commit(): Promise<void>
  • Commit the transaction.

    Returns Promise<void>

  • prepareEnvironment(useCLS?: boolean): Promise<void>
  • Called to acquire a connection to use and set the correct options on the connection. We should ensure all of the environment that's set up is cleaned up in cleanup() below.

    Parameters

    • Optional useCLS: boolean

      Defaults to true: Use CLS (Continuation Local Storage) with Sequelize. With CLS, all queries within the transaction callback will automatically receive the transaction object.

    Returns Promise<void>

  • rollback(): Promise<void>
  • Rollback (abort) the transaction

    Returns Promise<void>

  • setDeferrable(): Promise<void>
  • Returns Promise<void>

Generated using TypeDoc