@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
39 lines • 1.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const async_hooks_1 = require("async_hooks");
class LocalStorage {
constructor() {
this.als = new async_hooks_1.AsyncLocalStorage();
if (!this.als) {
throw new Error(`Cannot create transaction storage because no AsyncLocalStorage from async_hooks.`);
}
}
run(store, callback) {
return this.als.run(store, callback);
}
get() {
var _a;
return (_a = this.als.getStore()) !== null && _a !== void 0 ? _a : null;
}
set(key, value) {
const store = this.get();
if (store !== null)
store[key] = value;
}
}
class TransactionStorage extends LocalStorage {
initTx(options, callback) {
return this.run({ manualTx: null, options }, callback);
}
getTx() {
return this.get() || null;
}
setTx(value) {
const store = this.get();
if (store !== null)
store.manualTx = value;
throw new Error('TransactionStorage is not initialized.');
}
}
exports.default = new TransactionStorage();
//# sourceMappingURL=transaction.storage.js.map