@tsrt/typeorm-transactions
Version:
TypeORM convenient transactions management
110 lines • 5.82 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transaction = void 0;
const utils_1 = require("./utils");
class Transaction {
constructor(_options) {
var _a, _b, _c, _d, _e;
this._options = _options;
const { connection, connectionName, manager, propagation } = _options;
this._connection = (_a = manager === null || manager === void 0 ? void 0 : manager.connection) !== null && _a !== void 0 ? _a : utils_1.getConnection(connection, connectionName); // Necessary for _transactionId
this._parentManager = utils_1.getManagerFromNs(this._transactionId);
this._manager = (_b = manager !== null && manager !== void 0 ? manager : this._parentManager) !== null && _b !== void 0 ? _b : this._connection.manager;
switch (propagation) {
case 'SUPPORT': {
this._shouldUseParentManager = true;
this._queryRunner = (_c = this._manager) === null || _c === void 0 ? void 0 : _c.queryRunner;
break;
}
case 'SEPARATE': {
this._connection = utils_1.getConnection(connection, connectionName); // Necessary for _transactionId
this._parentManager = utils_1.getManagerFromNs(this._transactionId); // To retrieve it back later
this._shouldUseParentManager = false;
this._queryRunner = this._connection.createQueryRunner();
this._saveManager(this.manager);
break;
}
case 'REQUIRED':
default: {
this._shouldUseParentManager = !!(manager === null || manager === void 0 ? void 0 : manager.queryRunner) || !!this._parentManager;
this._queryRunner = (_e = (_d = this._manager) === null || _d === void 0 ? void 0 : _d.queryRunner) !== null && _e !== void 0 ? _e : this._manager.connection.createQueryRunner();
if (!this._shouldUseParentManager)
this._saveManager(this.manager);
break;
}
}
}
get manager() {
var _a;
return (_a = this._queryRunner.manager) !== null && _a !== void 0 ? _a : this._manager;
}
begin() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
if (this._shouldUseParentManager)
return;
if (!this._queryRunner.isTransactionActive) {
yield this._queryRunner.connect();
yield this._queryRunner.startTransaction((_a = this._options) === null || _a === void 0 ? void 0 : _a.isolationLevel);
}
else
throw new Error('Transaction is already started');
});
}
commit() {
return __awaiter(this, void 0, void 0, function* () {
if (this._shouldUseParentManager)
return;
if (this._isTransactionActive)
yield this._queryRunner.commitTransaction();
yield this._endTransaction();
});
}
rollback(error) {
return __awaiter(this, void 0, void 0, function* () {
if (!this._shouldUseParentManager) {
if (this._isTransactionActive)
yield this._queryRunner.rollbackTransaction();
yield this._endTransaction();
}
if (error && typeof error === 'string')
throw new Error(error);
else if (error)
throw error;
});
}
get _transactionId() { return this._connection.name; }
get _isTransactionActive() { var _a, _b, _c; return ((_a = this._queryRunner) === null || _a === void 0 ? void 0 : _a.isTransactionActive) && !((_c = (_b = this._options) === null || _b === void 0 ? void 0 : _b.manager) === null || _c === void 0 ? void 0 : _c.queryRunner); }
get _isTransactionNotReleased() { var _a, _b, _c; return !((_a = this._queryRunner) === null || _a === void 0 ? void 0 : _a.isReleased) && !((_c = (_b = this._options) === null || _b === void 0 ? void 0 : _b.manager) === null || _c === void 0 ? void 0 : _c.queryRunner); }
_saveManager(manager) {
this._validateNsAvailability();
utils_1.setManagerIntoNs(this._transactionId, manager);
}
_endTransaction() {
return __awaiter(this, void 0, void 0, function* () {
if (this._isTransactionNotReleased)
yield this._queryRunner.release();
utils_1.removeManagerFromNs(this._transactionId);
if (this._parentManager && !this._shouldUseParentManager)
utils_1.setManagerIntoNs(this._transactionId, this._parentManager);
});
}
_validateNsAvailability() {
const ns = utils_1.getTransactionsNamespace();
if (!ns)
throw new Error('Please, create Namespace using `createTransactionsNamespace` method.');
if (!ns.active)
throw new Error('Please, bind Namespace using `bindTransactionsNamespace` or `execInTransactionsNamespace` method.');
}
}
exports.Transaction = Transaction;
//# sourceMappingURL=Transaction.js.map