transaction.js
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
class Transaction {
constructor(sequelize, options) {
this.sequelize = sequelize;
this.savepoints = [];
this._afterCommitHooks = [];
const generateTransactionId = this.sequelize.dialect.queryGenerator.generateTransactionId;
this.options = __spreadValues({
type: sequelize.options.transactionType,
isolationLevel: sequelize.options.isolationLevel,
readOnly: false
}, options);
this.parent = this.options.transaction;
if (this.parent) {
this.id = this.parent.id;
this.parent.savepoints.push(this);
this.name = `${this.id}-sp-${this.parent.savepoints.length}`;
} else {
this.id = this.name = generateTransactionId();
}
delete this.options.transaction;
}
async commit() {
if (this.finished) {
throw new Error(`Transaction cannot be committed because it has been finished with state: ${this.finished}`);
}
try {
return await this.sequelize.getQueryInterface().commitTransaction(this, this.options);
} finally {
this.finished = "commit";
this.cleanup();
for (const hook of this._afterCommitHooks) {
await hook.apply(this, [this]);
}
}
}
async rollback() {
if (this.finished) {
throw new Error(`Transaction cannot be rolled back because it has been finished with state: ${this.finished}`);
}
if (!this.connection) {
throw new Error("Transaction cannot be rolled back because it never started");
}
try {
return await this.sequelize.getQueryInterface().rollbackTransaction(this, this.options);
} finally {
this.cleanup();
}
}
async prepareEnvironment(useCLS) {
let connectionPromise;
if (useCLS === void 0) {
useCLS = true;
}
if (this.parent) {
connectionPromise = Promise.resolve(this.parent.connection);
} else {
const acquireOptions = { uuid: this.id };
if (this.options.readOnly) {
acquireOptions.type = "SELECT";
}
connectionPromise = this.sequelize.connectionManager.getConnection(acquireOptions);
}
let result;
const connection = await connectionPromise;
this.connection = connection;
this.connection.uuid = this.id;
try {
await this.begin();
result = await this.setDeferrable();
} catch (setupErr) {
try {
result = await this.rollback();
} finally {
throw setupErr;
}
}
if (useCLS && this.sequelize.constructor._cls) {
this.sequelize.constructor._cls.set("transaction", this);
}
return result;
}
async setDeferrable() {
if (this.options.deferrable) {
return await this.sequelize.getQueryInterface().deferConstraints(this, this.options);
}
}
async begin() {
const queryInterface = this.sequelize.getQueryInterface();
if (this.sequelize.dialect.supports.settingIsolationLevelDuringTransaction) {
await queryInterface.startTransaction(this, this.options);
return queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options);
}
await queryInterface.setIsolationLevel(this, this.options.isolationLevel, this.options);
return queryInterface.startTransaction(this, this.options);
}
cleanup() {
if (this.parent || this.connection.uuid === void 0)
return;
this._clearCls();
const res = this.sequelize.connectionManager.releaseConnection(this.connection);
this.connection.uuid = void 0;
return res;
}
_clearCls() {
const cls = this.sequelize.constructor._cls;
if (cls) {
if (cls.get("transaction") === this) {
cls.set("transaction", null);
}
}
}
afterCommit(fn) {
if (!fn || typeof fn !== "function") {
throw new Error('"fn" must be a function');
}
this._afterCommitHooks.push(fn);
}
static get TYPES() {
return {
DEFERRED: "DEFERRED",
IMMEDIATE: "IMMEDIATE",
EXCLUSIVE: "EXCLUSIVE"
};
}
static get ISOLATION_LEVELS() {
return {
READ_UNCOMMITTED: "READ UNCOMMITTED",
READ_COMMITTED: "READ COMMITTED",
REPEATABLE_READ: "REPEATABLE READ",
SERIALIZABLE: "SERIALIZABLE"
};
}
static get LOCK() {
return {
UPDATE: "UPDATE",
SHARE: "SHARE",
KEY_SHARE: "KEY SHARE",
NO_KEY_UPDATE: "NO KEY UPDATE"
};
}
get LOCK() {
return Transaction.LOCK;
}
}
module.exports = Transaction;
module.exports.Transaction = Transaction;
module.exports.default = Transaction;
//# sourceMappingURL=transaction.js.map