1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Transaction = exports.IsolationLevel = void 0;
|
4 | var 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 = {}));
|
11 | class Transaction {
|
12 |
|
13 | constructor(connection) {
|
14 | this._connection = connection;
|
15 | this._status = 'initial';
|
16 | }
|
17 |
|
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 |
|
43 | checkFinished() {
|
44 | if (this._status === 'committed' || this._status === 'rollbacked') {
|
45 | throw new Error('transaction finished');
|
46 | }
|
47 | }
|
48 | }
|
49 | exports.Transaction = Transaction;
|