Class Transaction

View code

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.

Params:

NameTypeDescription
sequelizeSequelizeA configured sequelize Instance
optionsObjectAn object with options
options.autocommit=trueBooleanSets the autocommit property of the transaction.
options.type=trueStringSets the type of the transaction.
options.isolationLevel=trueStringSets the isolation level of the transaction.
options.deferrableStringSets the constraints to be deferred or immediately checked.
options.readOnly=falseStringSets the read-only property of the transaction. Such transactions will use read replicas when available

TYPES

View code

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.

The possible types to use when starting a transaction:

  1. {
  2. DEFERRED: "DEFERRED",
  3. IMMEDIATE: "IMMEDIATE",
  4. EXCLUSIVE: "EXCLUSIVE"
  5. }

Pass in the desired level as the first argument:

  1. return sequelize.transaction({
  2. type: Sequelize.Transaction.EXCLUSIVE
  3. }, function (t) {
  4. // your transactions
  5. }).then(function(result) {
  6. // transaction has been committed. Do something after the commit if required.
  7. }).catch(function(err) {
  8. // do something with the err.
  9. });

ISOLATION_LEVELS

View code

Isolations levels can be set per-transaction by passing options.isolationLevel to sequelize.transaction.Default to REPEATABLE_READ but you can override the default isolation level by passing options.isolationLevel in new Sequelize.

The possible isolations levels to use when starting a transaction:

  1. {
  2. READ_UNCOMMITTED: "READ UNCOMMITTED",
  3. READ_COMMITTED: "READ COMMITTED",
  4. REPEATABLE_READ: "REPEATABLE READ",
  5. SERIALIZABLE: "SERIALIZABLE"
  6. }

Pass in the desired level as the first argument:

  1. return sequelize.transaction({
  2. isolationLevel: Sequelize.Transaction.SERIALIZABLE
  3. }, function (t) {
  4. // your transactions
  5. }).then(function(result) {
  6. // transaction has been committed. Do something after the commit if required.
  7. }).catch(function(err) {
  8. // do something with the err.
  9. });

LOCK

View code

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

  1. t1 // is a transaction
  2. t1.LOCK.UPDATE,
  3. t1.LOCK.SHARE,
  4. t1.LOCK.KEY_SHARE, // Postgres 9.3+ only
  5. t1.LOCK.NO_KEY_UPDATE // Postgres 9.3+ only

Usage:

  1. t1 // is a transaction
  2. Model.findAll({
  3. where: ...,
  4. transaction: t1,
  5. lock: t1.LOCK...
  6. });

Postgres also supports specific locks while eager loading by using OF:

  1. UserModel.findAll({
  2. where: ...,
  3. include: [TaskModel, ...],
  4. transaction: t1,
  5. lock: {
  6. level: t1.LOCK...,
  7. of: UserModel
  8. }
  9. });

UserModel will be locked but TaskModel won't!


commit() -> Promise

View code

Commit the transaction


rollback() -> Promise

View code

Rollback (abort) the transaction


This document is automatically generated based on source code comments. Please do not edit it directly, as your changes will be ignored. Please write on IRC, open an issue or a create a pull request if you feel something can be improved. For help on how to write source code documentation see JSDoc and dox