@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
43 lines • 2.33 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.createTransactionExtension = void 0;
const client_1 = require("@prisma/client");
const utils_1 = require("./utils");
const transaction_storage_1 = __importDefault(require("./utils/transaction.storage"));
const createTransactionExtension = (options) => client_1.Prisma.defineExtension((prisma) => {
return prisma.$extends({
query: {
$allOperations: (_a) => __awaiter(void 0, [_a], void 0, function* ({ args, model, operation, query, __internalParams }) {
const store = transaction_storage_1.default.getTx();
if (store === null || (__internalParams === null || __internalParams === void 0 ? void 0 : __internalParams.transaction)) {
return query(args);
}
if (store.manualTx === null) {
const manualTx = yield (0, utils_1.extractTransaction)(prisma, store.options || options);
if (store.manualTx === null) {
store.manualTx = manualTx;
}
}
if (model) {
const result = yield store.manualTx.client[model][operation](args);
return result;
}
return store.manualTx.client[operation](args);
}),
},
});
});
exports.createTransactionExtension = createTransactionExtension;
//# sourceMappingURL=transaction.extension.js.map