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
117 lines • 5.25 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var TransactionModule_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TransactionModule = void 0;
const common_1 = require("@nestjs/common");
const core_1 = require("@nestjs/core");
const nestjs_aop_1 = require("@toss/nestjs-aop");
const typeorm_1 = require("typeorm");
const common_2 = require("../common");
const errors_1 = require("../errors");
const providers_1 = require("../providers");
let TransactionModule = TransactionModule_1 = class TransactionModule {
constructor(discoveryService, transactionModuleOption) {
this.discoveryService = discoveryService;
this.transactionModuleOption = transactionModuleOption;
/**
* Check if the instanceWrapper is a DataSource instance
* @param value InstanceWrapper
* @returns true if the instanceWrapper is a DataSource instance, false otherwise
*/
this.isDataSourceInstanceWrapper = (value) => {
var _a;
const isDataSourceInstance = (value) => value instanceof typeorm_1.DataSource;
if (isDataSourceInstance(value.instance)) {
if (((_a = this === null || this === void 0 ? void 0 : this.transactionModuleOption) === null || _a === void 0 ? void 0 : _a.dataSourceNames) !== undefined &&
this.transactionModuleOption.dataSourceNames.find((name) => name === value.instance.name) === undefined) {
return false;
}
return true;
}
return false;
};
(0, common_2.initializeTransactionalContext)();
}
onModuleInit() {
// Register the data source that is not registered in the transaction module
this.discoveryService
.getProviders()
.filter(this.isDataSourceInstanceWrapper)
.forEach((instanceWrapper) => {
try {
(0, common_2.getDataSource)(instanceWrapper.instance.name);
}
catch (e) {
if (e instanceof errors_1.NoRegistedDataSourceError) {
return (0, common_2.addTransactionalDataSource)(instanceWrapper.instance);
}
else {
throw e;
}
}
});
}
/**
* Register the Transaction Module
* @param option optional, Transaction Module option
* @returns DynamicModule
*/
static forRoot(option) {
const moduleOptionProviders = this.getTransacionModuleOptionProviders(option);
const serviceProviders = this.getServiceProividers();
if ((option === null || option === void 0 ? void 0 : option.maxEventListeners) !== undefined) {
common_2.storeOption.maxEventListeners = option.maxEventListeners;
}
return {
module: TransactionModule_1,
providers: [...moduleOptionProviders, ...serviceProviders],
exports: [
...moduleOptionProviders.map((provider) => provider.provide),
...serviceProviders.map((provider) => 'provide' in provider ? provider.provide : provider),
],
};
}
/**
* Register the Aspect provider used by the transaction module
* @param option optional, Transaction Module option
* @returns ValueProvider[]
*/
static getServiceProividers() {
return [providers_1.ALSTransactionAspect, providers_1.ALSTransactionEventListenerAspect];
}
/**
* Register the option provider used by the transaction module
* @param option optional, Transaction Module option
* @returns ValueProvider[]
*/
static getTransacionModuleOptionProviders(option) {
return [
{
provide: common_2.TRANSACTION_MODULE_OPTION_TOKEN,
useValue: option || {},
},
];
}
};
TransactionModule = TransactionModule_1 = __decorate([
(0, common_1.Module)({
imports: [nestjs_aop_1.AopModule, core_1.DiscoveryModule],
}),
__param(1, (0, common_1.Optional)()),
__param(1, (0, common_1.Inject)(common_2.TRANSACTION_MODULE_OPTION_TOKEN)),
__metadata("design:paramtypes", [core_1.DiscoveryService, Object])
], TransactionModule);
exports.TransactionModule = TransactionModule;
//# sourceMappingURL=index.js.map