UNPKG

1.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Transaction = exports.IsolationLevel = void 0;
4var IsolationLevel;
5(function (IsolationLevel) {
6 IsolationLevel["READ_UNCOMMITTED"] = "READ UNCOMMITTED";
7 IsolationLevel["READ_COMMITTED"] = "READ COMMITTED";
8 IsolationLevel["REPEATABLE_READ"] = "REPEATABLE READ";
9 IsolationLevel["SERIALIZABLE"] = "SERIALIZABLE";
10})(IsolationLevel = exports.IsolationLevel || (exports.IsolationLevel = {}));
11class Transaction {
12 /** @internal */
13 constructor(connection) {
14 this._connection = connection;
15 this._status = 'initial';
16 }
17 /** @internal */
18 async setup(isolation_level) {
19 if (this._status !== 'initial') {
20 throw new Error('can not start used transaction again');
21 }
22 this._adapter_connection = await this._connection._adapter.getConnection();
23 await this._connection._adapter.startTransaction(this._adapter_connection, isolation_level);
24 this._status = 'started';
25 }
26 async commit() {
27 if (this._status !== 'started') {
28 throw new Error('not an active transaction');
29 }
30 await this._connection._adapter.commitTransaction(this._adapter_connection);
31 await this._connection._adapter.releaseConnection(this._adapter_connection);
32 this._status = 'committed';
33 }
34 async rollback() {
35 if (this._status !== 'started') {
36 throw new Error('not an active transaction');
37 }
38 await this._connection._adapter.rollbackTransaction(this._adapter_connection);
39 await this._connection._adapter.releaseConnection(this._adapter_connection);
40 this._status = 'rollbacked';
41 }
42 /** @internal */
43 checkFinished() {
44 if (this._status === 'committed' || this._status === 'rollbacked') {
45 throw new Error('transaction finished');
46 }
47 }
48}
49exports.Transaction = Transaction;