@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
100 lines (96 loc) • 5.3 kB
JavaScript
;
var node_async_hooks = require('node:async_hooks');
require('../../../enum/decorator/api/action.enum.js');
require('../../../enum/decorator/api/authentication-type.enum.js');
require('../../../enum/decorator/api/controller/get-list/query/filter/missing-behavior.enum.js');
require('../../../enum/decorator/api/controller/get-list/query/unlisted-fields.enum.js');
require('../../../enum/decorator/api/controller/relation-reference-shape.enum.js');
require('../../../enum/decorator/api/controller/request/target.enum.js');
require('../../../enum/decorator/api/controller/request/transformer-type.enum.js');
require('../../../enum/decorator/api/controller/response-target.enum.js');
require('../../../enum/decorator/api/dto-type.enum.js');
var contextStorageKind_enum = require('../../../enum/decorator/api/function/context-storage-kind.enum.js');
require('../../../enum/decorator/api/function/subscriber-transaction-expectation.enum.js');
require('../../../enum/decorator/api/function/transaction/event-status.enum.js');
require('../../../enum/decorator/api/function/transaction/failure-stage.enum.js');
require('../../../enum/decorator/api/function/transaction/mode.enum.js');
require('../../../enum/decorator/api/function/transaction/outcome.enum.js');
require('../../../enum/decorator/api/function/transaction/owner-kind.enum.js');
require('../../../enum/decorator/api/function/transaction/trace-type.enum.js');
require('../../../enum/decorator/api/function/type.enum.js');
require('../../../enum/decorator/api/on-type.enum.js');
require('../../../enum/decorator/api/property/data-type.enum.js');
require('../../../enum/decorator/api/property/date/identifier.enum.js');
require('../../../enum/decorator/api/property/date/type.enum.js');
require('../../../enum/decorator/api/property/desribe-type.enum.js');
require('../../../enum/decorator/api/property/number-type.enum.js');
require('../../../enum/decorator/api/property/string-type.enum.js');
require('../../../enum/decorator/api/route/subscriber-authorization-expectation.enum.js');
require('../../../enum/decorator/api/route/type.enum.js');
class ApiFunctionContextStorage {
static STORAGE = new node_async_hooks.AsyncLocalStorage();
static TRANSACTION_REGISTRIES = new WeakMap();
static get() {
const entry = ApiFunctionContextStorage.STORAGE.getStore();
return entry?.kind === contextStorageKind_enum.EApiFunctionContextStorageKind.FUNCTION ? entry.context : undefined;
}
static getEventManager() {
const entry = ApiFunctionContextStorage.STORAGE.getStore();
return entry?.transaction?.entityManager ?? entry?.context.eventManager;
}
static getStep() {
const entry = ApiFunctionContextStorage.STORAGE.getStore();
return entry?.kind === contextStorageKind_enum.EApiFunctionContextStorageKind.STEP ? entry.context : undefined;
}
static getTransactionQueryRunner() {
return ApiFunctionContextStorage.STORAGE.getStore()?.transaction?.queryRunner;
}
static getTransactionRegistry() {
return ApiFunctionContextStorage.STORAGE.getStore()?.transaction?.registry;
}
static getTransactionRegistryForEntityManager(entityManager) {
return ApiFunctionContextStorage.TRANSACTION_REGISTRIES.get(entityManager);
}
static run(context, callback) {
return ApiFunctionContextStorage.STORAGE.run({
context: context,
kind: contextStorageKind_enum.EApiFunctionContextStorageKind.FUNCTION,
transaction: ApiFunctionContextStorage.STORAGE.getStore()?.transaction,
}, callback);
}
static runStep(context, callback) {
return ApiFunctionContextStorage.STORAGE.run({
context,
kind: contextStorageKind_enum.EApiFunctionContextStorageKind.STEP,
transaction: ApiFunctionContextStorage.STORAGE.getStore()?.transaction,
}, callback);
}
static async runTransaction(entityManager, queryRunner, registry, callback) {
ApiFunctionContextStorage.TRANSACTION_REGISTRIES.set(entityManager, registry);
try {
return await ApiFunctionContextStorage.STORAGE.run({
context: { eventManager: entityManager },
kind: contextStorageKind_enum.EApiFunctionContextStorageKind.TRANSACTION,
transaction: { entityManager, queryRunner, registry },
}, callback);
}
finally {
if (ApiFunctionContextStorage.TRANSACTION_REGISTRIES.get(entityManager) === registry) {
ApiFunctionContextStorage.TRANSACTION_REGISTRIES.delete(entityManager);
}
}
}
static runWithoutStepContext(callback) {
const entry = ApiFunctionContextStorage.STORAGE.getStore();
if (entry?.kind !== contextStorageKind_enum.EApiFunctionContextStorageKind.STEP) {
return callback();
}
return ApiFunctionContextStorage.STORAGE.run({
context: { eventManager: entry.context.eventManager },
kind: contextStorageKind_enum.EApiFunctionContextStorageKind.TRANSACTION,
transaction: entry.transaction,
}, callback);
}
}
exports.ApiFunctionContextStorage = ApiFunctionContextStorage;
//# sourceMappingURL=context-storage.class.js.map