1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.TransactionClient = void 0;
|
12 | const events_1 = require("events");
|
13 | const hooks_1 = require("@poppinss/hooks");
|
14 | const QueryBuilder_1 = require("../Orm/QueryBuilder");
|
15 | const Raw_1 = require("../Database/StaticBuilder/Raw");
|
16 | const Raw_2 = require("../Database/QueryBuilder/Raw");
|
17 | const Insert_1 = require("../Database/QueryBuilder/Insert");
|
18 | const Reference_1 = require("../Database/StaticBuilder/Reference");
|
19 | const Database_1 = require("../Database/QueryBuilder/Database");
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | class TransactionClient extends events_1.EventEmitter {
|
25 | constructor(knexClient, dialect, connectionName, debug, emitter) {
|
26 | super();
|
27 | Object.defineProperty(this, "knexClient", {
|
28 | enumerable: true,
|
29 | configurable: true,
|
30 | writable: true,
|
31 | value: knexClient
|
32 | });
|
33 | Object.defineProperty(this, "dialect", {
|
34 | enumerable: true,
|
35 | configurable: true,
|
36 | writable: true,
|
37 | value: dialect
|
38 | });
|
39 | Object.defineProperty(this, "connectionName", {
|
40 | enumerable: true,
|
41 | configurable: true,
|
42 | writable: true,
|
43 | value: connectionName
|
44 | });
|
45 | Object.defineProperty(this, "debug", {
|
46 | enumerable: true,
|
47 | configurable: true,
|
48 | writable: true,
|
49 | value: debug
|
50 | });
|
51 | Object.defineProperty(this, "emitter", {
|
52 | enumerable: true,
|
53 | configurable: true,
|
54 | writable: true,
|
55 | value: emitter
|
56 | });
|
57 | |
58 |
|
59 |
|
60 | Object.defineProperty(this, "isTransaction", {
|
61 | enumerable: true,
|
62 | configurable: true,
|
63 | writable: true,
|
64 | value: true
|
65 | });
|
66 | |
67 |
|
68 |
|
69 |
|
70 | Object.defineProperty(this, "mode", {
|
71 | enumerable: true,
|
72 | configurable: true,
|
73 | writable: true,
|
74 | value: 'dual'
|
75 | });
|
76 | |
77 |
|
78 |
|
79 | Object.defineProperty(this, "profiler", {
|
80 | enumerable: true,
|
81 | configurable: true,
|
82 | writable: true,
|
83 | value: void 0
|
84 | });
|
85 | Object.defineProperty(this, "hooks", {
|
86 | enumerable: true,
|
87 | configurable: true,
|
88 | writable: true,
|
89 | value: new hooks_1.Hooks()
|
90 | });
|
91 | |
92 |
|
93 |
|
94 |
|
95 |
|
96 | this.setMaxListeners(Infinity);
|
97 | }
|
98 | |
99 |
|
100 |
|
101 | get isCompleted() {
|
102 | return this.knexClient.isCompleted();
|
103 | }
|
104 | |
105 |
|
106 |
|
107 | get schema() {
|
108 | return this.getWriteClient().schema;
|
109 | }
|
110 | |
111 |
|
112 |
|
113 |
|
114 | getReadClient() {
|
115 | return this.knexClient;
|
116 | }
|
117 | |
118 |
|
119 |
|
120 |
|
121 | getWriteClient() {
|
122 | return this.knexClient;
|
123 | }
|
124 | |
125 |
|
126 |
|
127 | async truncate(table, cascade = false) {
|
128 | await this.dialect.truncate(table, cascade);
|
129 | }
|
130 | |
131 |
|
132 |
|
133 | async getAllTables(schemas) {
|
134 | return this.dialect.getAllTables(schemas);
|
135 | }
|
136 | |
137 |
|
138 |
|
139 |
|
140 | async columnsInfo(table, column) {
|
141 | const query = this.knexClient.select(table);
|
142 | const result = await (column ? query.columnInfo(column) : query.columnInfo());
|
143 | return result;
|
144 | }
|
145 | |
146 |
|
147 |
|
148 | knexQuery() {
|
149 | return this.knexClient.queryBuilder();
|
150 | }
|
151 | |
152 |
|
153 |
|
154 |
|
155 |
|
156 | knexRawQuery(sql, bindings) {
|
157 | return bindings ? this.knexClient.raw(sql, bindings) : this.knexClient.raw(sql);
|
158 | }
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 | modelQuery(model) {
|
165 | return new QueryBuilder_1.ModelQueryBuilder(this.knexQuery(), model, this);
|
166 | }
|
167 | |
168 |
|
169 |
|
170 | query() {
|
171 | return new Database_1.DatabaseQueryBuilder(this.knexQuery(), this);
|
172 | }
|
173 | |
174 |
|
175 |
|
176 | insertQuery() {
|
177 | return new Insert_1.InsertQueryBuilder(this.knexQuery(), this);
|
178 | }
|
179 | |
180 |
|
181 |
|
182 | rawQuery(sql, bindings) {
|
183 | return new Raw_2.RawQueryBuilder(this.knexClient.raw(sql, bindings), this);
|
184 | }
|
185 | |
186 |
|
187 |
|
188 |
|
189 |
|
190 | raw(sql, bindings) {
|
191 | return new Raw_1.RawBuilder(sql, bindings);
|
192 | }
|
193 | |
194 |
|
195 |
|
196 | ref(reference) {
|
197 | return new Reference_1.ReferenceBuilder(reference, this.knexClient.client);
|
198 | }
|
199 | |
200 |
|
201 |
|
202 | async transaction(callback, options) {
|
203 | const trx = await this.knexClient.transaction(options);
|
204 | const transaction = new TransactionClient(trx, this.dialect, this.connectionName, this.debug, this.emitter);
|
205 | |
206 |
|
207 |
|
208 | transaction.profiler = this.profiler?.create('trx:begin', { state: 'begin' });
|
209 | |
210 |
|
211 |
|
212 | if (typeof callback === 'function') {
|
213 | try {
|
214 | const response = await callback(transaction);
|
215 | !transaction.isCompleted && (await transaction.commit());
|
216 | return response;
|
217 | }
|
218 | catch (error) {
|
219 | await transaction.rollback();
|
220 | throw error;
|
221 | }
|
222 | }
|
223 | return transaction;
|
224 | }
|
225 | |
226 |
|
227 |
|
228 | from(table) {
|
229 | return this.query().from(table);
|
230 | }
|
231 | |
232 |
|
233 |
|
234 | table(table) {
|
235 | return this.insertQuery().table(table);
|
236 | }
|
237 | |
238 |
|
239 |
|
240 | after(event, handler) {
|
241 | this.hooks.add('after', event, handler);
|
242 | return this;
|
243 | }
|
244 | |
245 |
|
246 |
|
247 | async commit() {
|
248 | try {
|
249 | await this.knexClient.commit();
|
250 | this.profiler?.end({ state: 'commit' });
|
251 | this.emit('commit', this);
|
252 | this.removeAllListeners();
|
253 | }
|
254 | catch (error) {
|
255 | this.profiler?.end({ state: 'commit' });
|
256 | this.removeAllListeners();
|
257 | throw error;
|
258 | }
|
259 | try {
|
260 | await this.hooks.exec('after', 'commit');
|
261 | this.hooks.clear('after');
|
262 | }
|
263 | catch { }
|
264 | }
|
265 | |
266 |
|
267 |
|
268 | async rollback() {
|
269 | try {
|
270 | await this.knexClient.rollback();
|
271 | this.profiler?.end({ state: 'rollback' });
|
272 | this.emit('rollback', this);
|
273 | this.removeAllListeners();
|
274 | }
|
275 | catch (error) {
|
276 | this.profiler?.end({ state: 'rollback' });
|
277 | this.removeAllListeners();
|
278 | throw error;
|
279 | }
|
280 | try {
|
281 | await this.hooks.exec('after', 'rollback');
|
282 | this.hooks.clear('after');
|
283 | }
|
284 | catch { }
|
285 | }
|
286 | |
287 |
|
288 |
|
289 | getAdvisoryLock(key, timeout) {
|
290 | return this.dialect.getAdvisoryLock(key, timeout);
|
291 | }
|
292 | |
293 |
|
294 |
|
295 | releaseAdvisoryLock(key) {
|
296 | return this.dialect.releaseAdvisoryLock(key);
|
297 | }
|
298 | }
|
299 | exports.TransactionClient = TransactionClient;
|