Back to Sequelize

Transaction

static/v4/class/lib/transaction.js~Transaction.html

latest6.5 KB
Original Source

publicclass| source

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:

Static Member Summary

| Static Public Members | | publicstaticget |

ISOLATION_LEVELS: {"READ_UNCOMMITTED": string, "READ_COMMITTED": string, "REPEATABLE_READ": string, "SERIALIZABLE": string}

Isolations levels can be set per-transaction by passing options.isolationLevel to sequelize.transaction.

| | | publicstaticget |

LOCK: Object: {"UPDATE": string, "SHARE": string, "KEY_SHARE": string, "NO_KEY_UPDATE": string}

Possible options for row locking.

| | | publicstaticget |

TYPES: {"DEFERRED": string, "IMMEDIATE": string, "EXCLUSIVE": string}

Types can be set per-transaction by passing options.type to sequelize.transaction.

| |

Constructor Summary

| Public Constructor | | public |

constructor(sequelize: Sequelize, options: Object)

| |

Member Summary

| Public Members | | publicget |

LOCK: *

| |

Method Summary

| Public Methods | | public |

afterCommit(fn: Function)

A hook that is run after a transaction is committed

| | | public |

commit(): Promise

Commit the transaction

| | | public |

rollback(): Promise

Rollback (abort) the transaction

| |

Static Public Members

publicstaticgetISOLATION_LEVELS: {"READ_UNCOMMITTED": string, "READ_COMMITTED": string, "REPEATABLE_READ": string, "SERIALIZABLE": string} source

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.

Pass in the desired level as the first argument:

return sequelize.transaction({isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE}, transaction => {

 // your transactions

}).then(result => {
  // transaction has been committed. Do something after the commit if required.
}).catch(err => {
  // do something with the err.
});

Properties:

| Name | Type | Attribute | Description | | READ_UNCOMMITTED | * | | | | READ_COMMITTED | * | | | | REPEATABLE_READ | * | | | | SERIALIZABLE | * | | |

publicstaticgetLOCK: Object: {"UPDATE": string, "SHARE": string, "KEY_SHARE": string, "NO_KEY_UPDATE": string} source

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

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

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!

Properties:

| Name | Type | Attribute | Description | | UPDATE | * | | | | SHARE | * | | | | KEY_SHARE | * | |

Postgres 9.3+ only

| | NO_KEY_UPDATE | * | |

Postgres 9.3+ only

|

Return:

| Object |

Return Properties:

| Name | Type | Attribute | Description | | UPDATE | * | | | | SHARE | * | | | | KEY_SHARE | * | |

Postgres 9.3+ only

| | NO_KEY_UPDATE | * | |

Postgres 9.3+ only

|

publicstaticgetTYPES: {"DEFERRED": string, "IMMEDIATE": string, "EXCLUSIVE": string} source

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:

return sequelize.transaction({type: Sequelize.Transaction.TYPES.EXCLUSIVE}, transaction => {

 // your transactions

}).then(result => {
  // transaction has been committed. Do something after the commit if required.
}).catch(err => {
  // do something with the err.
});

Properties:

| Name | Type | Attribute | Description | | DEFERRED | * | | | | IMMEDIATE | * | | | | EXCLUSIVE | * | | |

Public Constructors

publicconstructor(sequelize: Sequelize, options: Object) source

Params:

| Name | Type | Attribute | Description | | sequelize | Sequelize | |

A configured sequelize Instance

| | options | Object | |

An object with options

| | options.autocommit | Boolean | |

Sets the autocommit property of the transaction.

| | options.type | String |

  • default: true

|

Sets the type of the transaction.

| | options.isolationLevel | String |

  • default: true

|

Sets the isolation level of the transaction.

| | options.deferrable | String | |

Sets the constraints to be deferred or immediately checked.

|

Public Members

publicgetLOCK: * source

See:

Public Methods

publicafterCommit(fn: Function) source

A hook that is run after a transaction is committed

Params:

| Name | Type | Attribute | Description | | fn | Function | |

A callback function that is called with the committed transaction

|

publiccommit(): Promise source

Commit the transaction

Return:

| Promise |

publicrollback(): Promise source

Rollback (abort) the transaction

Return:

| Promise |