UNPKG

3.45 kBTypeScriptView Raw
1import { Deferrable } from './deferrable';
2import { Logging } from './model';
3import { Promise } from './promise';
4import { Sequelize } from './sequelize';
5
6/**
7 * The transaction object is used to identify a running transaction. It is created by calling
8 * `Sequelize.transaction()`.
9 *
10 * To run a query under a transaction, you should pass the transaction in the options object.
11 */
12export class Transaction {
13 constructor(sequelize: Sequelize, options: TransactionOptions);
14
15 /**
16 * Commit the transaction
17 */
18 public commit(): Promise<void>;
19
20 /**
21 * Rollback (abort) the transaction
22 */
23 public rollback(): Promise<void>;
24
25 /**
26 * Adds hook that is run after a transaction is committed
27 */
28 public afterCommit(fn: (transaction: this) => void | Promise<void>): void;
29}
30
31// tslint:disable-next-line no-namespace
32export namespace Transaction {
33 /**
34 * Isolations levels can be set per-transaction by passing `options.isolationLevel` to `sequelize.transaction`.
35 * Default to `REPEATABLE_READ` but you can override the default isolation level by passing `options.isolationLevel` in `new Sequelize`.
36 *
37 * The possible isolations levels to use when starting a transaction:
38 *
39 * ```js
40 * {
41 * READ_UNCOMMITTED: "READ UNCOMMITTED",
42 * READ_COMMITTED: "READ COMMITTED",
43 * REPEATABLE_READ: "REPEATABLE READ",
44 * SERIALIZABLE: "SERIALIZABLE"
45 * }
46 * ```
47 *
48 * Pass in the desired level as the first argument:
49 *
50 * ```js
51 * return sequelize.transaction({isolationLevel: Sequelize.Transaction.SERIALIZABLE}, transaction => {
52 *
53 * // your transactions
54 *
55 * }).then(result => {
56 * // transaction has been committed. Do something after the commit if required.
57 * }).catch(err => {
58 * // do something with the err.
59 * });
60 * ```
61 */
62 enum ISOLATION_LEVELS {
63 READ_UNCOMMITTED = 'READ UNCOMMITTED',
64 READ_COMMITTED = 'READ COMMITTED',
65 REPEATABLE_READ = 'REPEATABLE READ',
66 SERIALIZABLE = 'SERIALIZABLE',
67 }
68
69 enum TYPES {
70 DEFERRED = 'DEFERRED',
71 IMMEDIATE = 'IMMEDIATE',
72 EXCLUSIVE = 'EXCLUSIVE',
73 }
74
75 /**
76 * Possible options for row locking. Used in conjunction with `find` calls:
77 *
78 * ```js
79 * t1 // is a transaction
80 * t1.LOCK.UPDATE,
81 * t1.LOCK.SHARE,
82 * t1.LOCK.KEY_SHARE, // Postgres 9.3+ only
83 * t1.LOCK.NO_KEY_UPDATE // Postgres 9.3+ only
84 * ```
85 *
86 * Usage:
87 * ```js
88 * t1 // is a transaction
89 * Model.findAll({
90 * where: ...,
91 * transaction: t1,
92 * lock: t1.LOCK...
93 * });
94 * ```
95 *
96 * Postgres also supports specific locks while eager loading by using OF:
97 * ```js
98 * UserModel.findAll({
99 * where: ...,
100 * include: [TaskModel, ...],
101 * transaction: t1,
102 * lock: {
103 * level: t1.LOCK...,
104 * of: UserModel
105 * }
106 * });
107 * ```
108 * UserModel will be locked but TaskModel won't!
109 */
110 enum LOCK {
111 UPDATE = 'UPDATE',
112 SHARE = 'SHARE',
113 /**
114 * Postgres 9.3+ only
115 */
116 KEY_SHARE = 'KEY SHARE',
117 /**
118 * Postgres 9.3+ only
119 */
120 NO_KEY_UPDATE = 'NO KEY UPDATE',
121 }
122}
123
124/**
125 * Options provided when the transaction is created
126 */
127export interface TransactionOptions extends Logging {
128 autocommit?: boolean;
129 isolationLevel?: Transaction.ISOLATION_LEVELS;
130 type?: Transaction.TYPES;
131 deferrable?: string | Deferrable;
132 /**
133 * Parent transaction.
134 */
135 transaction?: Transaction;
136}
137
138export default Transaction;