Documentation
    Preparing search index...

    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.

    Transaction

    Index
    id: string
    parent: Transaction | null
    sequelize: Sequelize
    • get finished(): "rollback" | "commit" | undefined

      Returns "rollback" | "commit" | undefined

    • 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

      use the Lock export

    • get rootTransaction(): Transaction

      Get the root transaction if nested, or self if this is a root transaction

      Returns Transaction

    • get ISOLATION_LEVELS(): typeof IsolationLevel

      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:

      Returns typeof IsolationLevel

      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.
      }

      use the IsolationLevel export

    • get LOCK(): typeof Lock

      Possible options for row locking. Used in conjunction with find calls:

      Returns typeof Lock

      possible options for row locking

      // t1 is a transaction
      Model.findAll({
      where: ...,
      transaction: t1,
      lock: t1.LOCK...
      });
      UserModel.findAll({
      where: ...,
      include: [TaskModel, ...],
      transaction: t1,
      lock: {
      level: t1.LOCK...,
      of: UserModel
      }
      });

      UserModel will be locked but TaskModel won't!

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

      use the Lock export

    • get TYPES(): typeof TransactionType

      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:

      Returns typeof TransactionType

      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.
      }

      use the TransactionType export

    • Adds a hook that is run after a transaction is committed.

      Parameters

      Returns this

    • Adds a hook that is run after a transaction is rolled back.

      Parameters

      Returns this

    • Adds a hook that is run after a transaction completes, no matter if it was committed or rolled back.

      Parameters

      Returns this

    • Commit the transaction.

      Returns Promise<void>

    • Called to acquire a connection to use and set the correct options on the connection. We should ensure all the environment that's set up is cleaned up in cleanup() below.

      Returns Promise<void>

    • Rollback (abort) the transaction

      Returns Promise<void>

    • Changes the isolation level of the transaction.

      Parameters

      Returns Promise<void>