@nicheeeer/prisma-transaction
Version:
## Introduction `prisma-transaction` is a library that provides a simple way to use transactions in Prisma within NestJS using decorators. \ It simplifies handling transactions and ensures consistency across services by utilizing `AsyncLocalStorage` to m
51 lines • 2.72 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Transaction = Transaction;
const utils_1 = require("./utils");
const transaction_storage_1 = __importDefault(require("./utils/transaction.storage"));
function Transaction(options) {
return ((_, propertyKey, descriptor) => {
const originalFn = descriptor.value;
if (typeof originalFn !== 'function') {
throw new Error(`The @Transaction decorator can be only used on functions, but ${propertyKey.toString()} is not a function.`);
}
descriptor.value = function (...args) {
return __awaiter(this, void 0, void 0, function* () {
const outerThis = this;
return transaction_storage_1.default.initTx(options, function () {
return __awaiter(this, void 0, void 0, function* () {
try {
const result = yield originalFn.apply(outerThis, args);
const store = transaction_storage_1.default.getTx();
if (store === null || store === void 0 ? void 0 : store.manualTx) {
yield store.manualTx.commit();
}
return result;
}
catch (error) {
const store = transaction_storage_1.default.getTx();
if (store === null || store === void 0 ? void 0 : store.manualTx) {
yield store.manualTx.rollback(error);
}
throw error;
}
});
});
});
};
(0, utils_1.copyMethodMetadata)(originalFn, descriptor.value);
});
}
//# sourceMappingURL=transaction.decorator.js.map