1 | import { Deferrable } from './deferrable';
|
2 | import { Logging } from './model';
|
3 | import { Promise } from './promise';
|
4 | import { 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 | */
|
12 | export 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 | * Returns possible options for row locking
|
32 | */
|
33 | static get LOCK(): LOCK;
|
34 |
|
35 | /**
|
36 | * Same as its static version, but can also be called on instances of
|
37 | * transactions to get possible options for row locking directly from the
|
38 | * instance.
|
39 | */
|
40 | get LOCK(): LOCK;
|
41 | }
|
42 |
|
43 | // tslint:disable-next-line no-namespace
|
44 | export namespace Transaction {
|
45 | /**
|
46 | * Isolations levels can be set per-transaction by passing `options.isolationLevel` to `sequelize.transaction`.
|
47 | * Default to `REPEATABLE_READ` but you can override the default isolation level by passing `options.isolationLevel` in `new Sequelize`.
|
48 | *
|
49 | * The possible isolations levels to use when starting a transaction:
|
50 | *
|
51 | * ```js
|
52 | * {
|
53 | * READ_UNCOMMITTED: "READ UNCOMMITTED",
|
54 | * READ_COMMITTED: "READ COMMITTED",
|
55 | * REPEATABLE_READ: "REPEATABLE READ",
|
56 | * SERIALIZABLE: "SERIALIZABLE"
|
57 | * }
|
58 | * ```
|
59 | *
|
60 | * Pass in the desired level as the first argument:
|
61 | *
|
62 | * ```js
|
63 | * return sequelize.transaction({isolationLevel: Sequelize.Transaction.SERIALIZABLE}, transaction => {
|
64 | *
|
65 | * // your transactions
|
66 | *
|
67 | * }).then(result => {
|
68 | * // transaction has been committed. Do something after the commit if required.
|
69 | * }).catch(err => {
|
70 | * // do something with the err.
|
71 | * });
|
72 | * ```
|
73 | */
|
74 | enum ISOLATION_LEVELS {
|
75 | READ_UNCOMMITTED = 'READ UNCOMMITTED',
|
76 | READ_COMMITTED = 'READ COMMITTED',
|
77 | REPEATABLE_READ = 'REPEATABLE READ',
|
78 | SERIALIZABLE = 'SERIALIZABLE',
|
79 | }
|
80 |
|
81 | enum TYPES {
|
82 | DEFERRED = 'DEFERRED',
|
83 | IMMEDIATE = 'IMMEDIATE',
|
84 | EXCLUSIVE = 'EXCLUSIVE',
|
85 | }
|
86 | }
|
87 |
|
88 | /**
|
89 | * Possible options for row locking. Used in conjunction with `find` calls:
|
90 | *
|
91 | * ```js
|
92 | * t1 // is a transaction
|
93 | * t1.LOCK.UPDATE,
|
94 | * t1.LOCK.SHARE,
|
95 | * t1.LOCK.KEY_SHARE, // Postgres 9.3+ only
|
96 | * t1.LOCK.NO_KEY_UPDATE // Postgres 9.3+ only
|
97 | * ```
|
98 | *
|
99 | * Usage:
|
100 | * ```js
|
101 | * t1 // is a transaction
|
102 | * Model.findAll({
|
103 | * where: ...,
|
104 | * transaction: t1,
|
105 | * lock: t1.LOCK...
|
106 | * });
|
107 | * ```
|
108 | *
|
109 | * Postgres also supports specific locks while eager loading by using OF:
|
110 | * ```js
|
111 | * UserModel.findAll({
|
112 | * where: ...,
|
113 | * include: [TaskModel, ...],
|
114 | * transaction: t1,
|
115 | * lock: {
|
116 | * level: t1.LOCK...,
|
117 | * of: UserModel
|
118 | * }
|
119 | * });
|
120 | * ```
|
121 | * UserModel will be locked but TaskModel won't!
|
122 | */
|
123 | export enum LOCK {
|
124 | UPDATE = 'UPDATE',
|
125 | SHARE = 'SHARE',
|
126 | /**
|
127 | * Postgres 9.3+ only
|
128 | */
|
129 | KEY_SHARE = 'KEY SHARE',
|
130 | /**
|
131 | * Postgres 9.3+ only
|
132 | */
|
133 | NO_KEY_UPDATE = 'NO KEY UPDATE',
|
134 | }
|
135 |
|
136 | interface LOCK {
|
137 | UPDATE: LOCK.UPDATE;
|
138 | SHARE: LOCK.SHARE;
|
139 | KEY_SHARE: LOCK.KEY_SHARE;
|
140 | NO_KEY_UPDATE: LOCK.NO_KEY_UPDATE;
|
141 | }
|
142 |
|
143 | /**
|
144 | * Options provided when the transaction is created
|
145 | */
|
146 | export interface TransactionOptions extends Logging {
|
147 | autocommit?: boolean;
|
148 | isolationLevel?: Transaction.ISOLATION_LEVELS;
|
149 | type?: Transaction.TYPES;
|
150 | deferrable?: string | Deferrable;
|
151 | /**
|
152 | * Parent transaction.
|
153 | */
|
154 | transaction?: Transaction;
|
155 | }
|
156 |
|
157 | export default Transaction;
|