@tsrt/typeorm-transactions
Version:
TypeORM convenient transactions management
56 lines • 3.2 kB
JavaScript
;
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.TransactionManager = void 0;
const utils_1 = require("./utils");
const Transaction_1 = require("./Transaction");
class TransactionManager {
constructor(_options = {}) {
this._options = _options;
}
createTransaction(options) {
return new Transaction_1.Transaction(this._getDefaultOptions(options));
}
transaction(cbOrOptions, options) {
return __awaiter(this, void 0, void 0, function* () {
const opts = typeof cbOrOptions === 'function' ? options : cbOrOptions;
const t = this.createTransaction(opts);
yield t.begin();
return typeof cbOrOptions === 'function'
? cbOrOptions(t)
.then((result) => __awaiter(this, void 0, void 0, function* () { yield t.rollback(); return result; }))
.catch((err) => __awaiter(this, void 0, void 0, function* () { return yield t.rollback(err); }))
: t;
});
}
autoTransaction(cb, options) {
return __awaiter(this, void 0, void 0, function* () {
const t = this.createTransaction(options);
yield t.begin();
return cb(t)
.then((result) => __awaiter(this, void 0, void 0, function* () { yield t.commit(); return result; }))
.catch((err) => __awaiter(this, void 0, void 0, function* () { return yield t.rollback(err); }));
});
}
get connection() {
var _a;
const { manager, connection, connectionName } = this._options;
if (!this._connection)
this._connection = (_a = manager === null || manager === void 0 ? void 0 : manager.connection) !== null && _a !== void 0 ? _a : utils_1.getConnection(connection, connectionName);
return this._connection;
}
_getDefaultOptions(options) {
var _a, _b, _c;
return Object.assign(Object.assign({}, options), { connection: this.connection, connectionName: this.connection.name, isolationLevel: (_a = options === null || options === void 0 ? void 0 : options.isolationLevel) !== null && _a !== void 0 ? _a : (_b = this._options) === null || _b === void 0 ? void 0 : _b.isolationLevel, propagation: (_c = options === null || options === void 0 ? void 0 : options.propagation) !== null && _c !== void 0 ? _c : this._options.propagation });
}
}
exports.TransactionManager = TransactionManager;
//# sourceMappingURL=TransactionManager.js.map