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