nestjs-transaction
Version:
A library that extracts and provides only some of the functions of the 'typeorm-transactional' npm module that are needed to operate in the Nestjs + TypeORM environment
51 lines • 3 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.emitAsyncOnRollBackEvent = exports.emitAsyncOnCommitEvent = exports.runOnTransactionRollback = exports.runOnTransactionCommit = void 0;
const eventemitter2_1 = require("eventemitter2");
const common_1 = require("../common");
const storage_1 = require("../storage");
const runOnTransactionCommit = (cb) => {
var _a;
runWithNewEventEmitter();
(_a = storage_1.storage.get(common_1.TRANSACTION_EVENT_EMIMTTER)) === null || _a === void 0 ? void 0 : _a.once(common_1.ON_COMMIT_EVENT_NAME, cb);
};
exports.runOnTransactionCommit = runOnTransactionCommit;
const runOnTransactionRollback = (cb) => {
var _a;
runWithNewEventEmitter();
(_a = storage_1.storage.get(common_1.TRANSACTION_EVENT_EMIMTTER)) === null || _a === void 0 ? void 0 : _a.once(common_1.ON_ROLLBACK_EVENT_NAME, cb);
};
exports.runOnTransactionRollback = runOnTransactionRollback;
const emitAsyncOnCommitEvent = () => __awaiter(void 0, void 0, void 0, function* () {
const storedEventEmitter = storage_1.storage.get(common_1.TRANSACTION_EVENT_EMIMTTER);
yield (storedEventEmitter === null || storedEventEmitter === void 0 ? void 0 : storedEventEmitter.emitAsync(common_1.ON_COMMIT_EVENT_NAME));
});
exports.emitAsyncOnCommitEvent = emitAsyncOnCommitEvent;
const emitAsyncOnRollBackEvent = (e) => __awaiter(void 0, void 0, void 0, function* () {
const storedEventEmitter = storage_1.storage.get(common_1.TRANSACTION_EVENT_EMIMTTER);
yield (storedEventEmitter === null || storedEventEmitter === void 0 ? void 0 : storedEventEmitter.emitAsync(common_1.ON_ROLLBACK_EVENT_NAME, e));
});
exports.emitAsyncOnRollBackEvent = emitAsyncOnRollBackEvent;
/**
* If event emitter is not set, create new event emitter and set to storage
* after that, other event emitter will not be created
*/
const runWithNewEventEmitter = () => {
const storedEventEmitter = storage_1.storage.get(common_1.TRANSACTION_EVENT_EMIMTTER);
if (storedEventEmitter === undefined) {
const eventEmitter = new eventemitter2_1.EventEmitter2({
maxListeners: common_1.storeOption.maxEventListeners,
});
storage_1.storage.set(common_1.TRANSACTION_EVENT_EMIMTTER, eventEmitter);
}
};
//# sourceMappingURL=transaciton-hooks.js.map