@nest-omni/core 4.1.3-15 → 4.1.3-17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/audit/audit.module.js +7 -0
- package/audit/services/entity-audit.service.js +2 -1
- package/audit/services/manual-audit-log.service.js +2 -2
- package/audit/services/multi-database.service.d.ts +0 -5
- package/audit/services/multi-database.service.js +0 -24
- package/audit/services/transaction-audit.service.js +3 -2
- package/cache/dependencies/db.dependency.d.ts +2 -2
- package/cache/dependencies/db.dependency.js +4 -4
- package/http-client/examples/proxy-from-environment.example.d.ts +1 -1
- package/http-client/examples/proxy-from-environment.example.js +18 -19
- package/http-client/services/logging.service.js +2 -3
- package/index.d.ts +1 -1
- package/index.js +1 -1
- package/package.json +3 -2
- package/setup/bootstrap.setup.d.ts +1 -1
- package/shared/service-registry.module.js +2 -15
- package/shared/services/api-config.service.js +1 -0
- package/validators/is-exists.validator.d.ts +2 -7
- package/validators/is-exists.validator.js +2 -24
- package/validators/is-unique.validator.d.ts +2 -7
- package/validators/is-unique.validator.js +2 -24
- package/transaction/__tests__/mocks.d.ts +0 -9
- package/transaction/__tests__/mocks.js +0 -33
- package/transaction/base-service-transaction.d.ts +0 -106
- package/transaction/base-service-transaction.js +0 -317
- package/transaction/cls-compatibility.service.d.ts +0 -55
- package/transaction/cls-compatibility.service.js +0 -127
- package/transaction/data-source-registry.d.ts +0 -91
- package/transaction/data-source-registry.js +0 -349
- package/transaction/data-source.util.d.ts +0 -142
- package/transaction/data-source.util.js +0 -330
- package/transaction/database-adapter.d.ts +0 -44
- package/transaction/database-adapter.js +0 -240
- package/transaction/decorators/entity-datasource.decorator.d.ts +0 -62
- package/transaction/decorators/entity-datasource.decorator.js +0 -105
- package/transaction/index.d.ts +0 -16
- package/transaction/index.js +0 -71
- package/transaction/logging-transactional.interceptor.d.ts +0 -18
- package/transaction/logging-transactional.interceptor.js +0 -163
- package/transaction/transaction-context.service.d.ts +0 -137
- package/transaction/transaction-context.service.js +0 -411
- package/transaction/transaction-manager.d.ts +0 -230
- package/transaction/transaction-manager.holder.d.ts +0 -31
- package/transaction/transaction-manager.holder.js +0 -42
- package/transaction/transaction-manager.js +0 -1001
- package/transaction/transaction-synchronization.d.ts +0 -171
- package/transaction/transaction-synchronization.js +0 -380
- package/transaction/transaction.errors.d.ts +0 -91
- package/transaction/transaction.errors.js +0 -206
- package/transaction/transaction.module.d.ts +0 -30
- package/transaction/transaction.module.js +0 -126
- package/transaction/transactional.decorator.d.ts +0 -82
- package/transaction/transactional.decorator.js +0 -320
- package/transaction/typeorm-module-wrapper.d.ts +0 -96
- package/transaction/typeorm-module-wrapper.js +0 -197
|
@@ -1,320 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
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;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
-
};
|
|
11
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
12
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
13
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
14
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
15
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
16
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
17
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
18
|
-
});
|
|
19
|
-
};
|
|
20
|
-
var TransactionalInterceptor_1;
|
|
21
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.Tx = exports.TransactionalInterceptor = exports.TRANSACTION_METADATA_KEY = void 0;
|
|
23
|
-
exports.Transactional = Transactional;
|
|
24
|
-
exports.ClassTransactional = ClassTransactional;
|
|
25
|
-
exports.UseTransactional = UseTransactional;
|
|
26
|
-
const common_1 = require("@nestjs/common");
|
|
27
|
-
const rxjs_1 = require("rxjs");
|
|
28
|
-
const transaction_manager_1 = require("./transaction-manager");
|
|
29
|
-
const nestjs_cls_1 = require("nestjs-cls");
|
|
30
|
-
const transaction_manager_holder_1 = require("./transaction-manager.holder");
|
|
31
|
-
/**
|
|
32
|
-
* 事务元数据键
|
|
33
|
-
*/
|
|
34
|
-
exports.TRANSACTION_METADATA_KEY = 'transactional';
|
|
35
|
-
/**
|
|
36
|
-
* @Transactional 装饰器
|
|
37
|
-
* 统一的事务装饰器,自动处理嵌套和数据源
|
|
38
|
-
*/
|
|
39
|
-
function Transactional(options) {
|
|
40
|
-
const defaultOptions = {
|
|
41
|
-
propagation: transaction_manager_1.Propagation.REQUIRED,
|
|
42
|
-
readOnly: false,
|
|
43
|
-
timeout: 30,
|
|
44
|
-
};
|
|
45
|
-
const finalOptions = Object.assign(Object.assign({}, defaultOptions), options);
|
|
46
|
-
return function (target, propertyKey, descriptor) {
|
|
47
|
-
// 设置元数据
|
|
48
|
-
(0, common_1.SetMetadata)(exports.TRANSACTION_METADATA_KEY, finalOptions)(target, propertyKey, descriptor);
|
|
49
|
-
// 包装原始方法
|
|
50
|
-
const originalMethod = descriptor.value;
|
|
51
|
-
descriptor.value = function (...args) {
|
|
52
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
-
var _a;
|
|
54
|
-
// 获取事务管理器 - 优先使用全局持有者,兼容手动注入的方式
|
|
55
|
-
let transactionManager = transaction_manager_holder_1.TransactionManagerHolder.getManager() || this.transactionManager;
|
|
56
|
-
const logger = new common_1.Logger('Transactional');
|
|
57
|
-
if (!transactionManager) {
|
|
58
|
-
logger.warn('TransactionManager not found. Make sure TransactionModule is imported. Executing without transaction.');
|
|
59
|
-
return yield originalMethod.apply(this, args);
|
|
60
|
-
}
|
|
61
|
-
// 获取事务定义
|
|
62
|
-
const isolation = (options === null || options === void 0 ? void 0 : options.isolation) || (options === null || options === void 0 ? void 0 : options.isolationLevel);
|
|
63
|
-
const definition = {
|
|
64
|
-
propagation: finalOptions.propagation || transaction_manager_1.Propagation.REQUIRED,
|
|
65
|
-
isolationLevel: isolation,
|
|
66
|
-
readOnly: finalOptions.readOnly,
|
|
67
|
-
timeout: finalOptions.timeout,
|
|
68
|
-
name: finalOptions.name || `${target.constructor.name}.${String(propertyKey)}`,
|
|
69
|
-
dataSourceName: (options === null || options === void 0 ? void 0 : options.dataSource) || 'default',
|
|
70
|
-
onTimeout: options === null || options === void 0 ? void 0 : options.onTimeout,
|
|
71
|
-
};
|
|
72
|
-
let clsService = null;
|
|
73
|
-
let clsAvailable = false;
|
|
74
|
-
try {
|
|
75
|
-
clsService = (_a = nestjs_cls_1.ClsServiceManager.getClsService) === null || _a === void 0 ? void 0 : _a.call(nestjs_cls_1.ClsServiceManager);
|
|
76
|
-
if (clsService && typeof clsService.set === 'function') {
|
|
77
|
-
clsAvailable = true;
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
logger.warn('[Transactional] ClsService found but not fully compatible. ' +
|
|
81
|
-
'EntityManager will not be stored in CLS context.');
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
catch (clsError) {
|
|
85
|
-
logger.warn('[Transactional] CLS is not available or failed to initialize. ' +
|
|
86
|
-
`Reason: ${clsError.message}. ` +
|
|
87
|
-
'Transaction will proceed without CLS integration.');
|
|
88
|
-
}
|
|
89
|
-
// 开始事务
|
|
90
|
-
const status = yield transactionManager.getTransaction(definition);
|
|
91
|
-
try {
|
|
92
|
-
// 安全地设置 CLS 中的 EntityManager
|
|
93
|
-
if (clsAvailable && clsService) {
|
|
94
|
-
try {
|
|
95
|
-
const em = transactionManager.getCurrentEntityManager(definition.dataSourceName);
|
|
96
|
-
if (em) {
|
|
97
|
-
clsService.set('entityManager', em);
|
|
98
|
-
logger.debug(`[Transactional] EntityManager set in CLS for ${definition.dataSourceName}`);
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
logger.warn(`[Transactional] No EntityManager available for ${definition.dataSourceName}`);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
catch (error) {
|
|
105
|
-
logger.error('[Transactional] Failed to set EntityManager in CLS. ' +
|
|
106
|
-
`DataSource: ${definition.dataSourceName}, ` +
|
|
107
|
-
`Error: ${error.message}`, error.stack);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
// 执行方法
|
|
111
|
-
const result = yield originalMethod.apply(this, args);
|
|
112
|
-
// 提交事务
|
|
113
|
-
yield transactionManager.commit(status);
|
|
114
|
-
return result;
|
|
115
|
-
}
|
|
116
|
-
catch (error) {
|
|
117
|
-
// 确保回滚事务
|
|
118
|
-
let rollbackError = null;
|
|
119
|
-
try {
|
|
120
|
-
yield transactionManager.rollback(status);
|
|
121
|
-
}
|
|
122
|
-
catch (rbError) {
|
|
123
|
-
rollbackError = rbError;
|
|
124
|
-
logger.error('[Transactional] Failed to rollback transaction:', rbError);
|
|
125
|
-
}
|
|
126
|
-
// 如果回滚也失败,抛出聚合错误
|
|
127
|
-
if (rollbackError) {
|
|
128
|
-
const aggregateError = new Error(`Transaction failed and rollback also failed. ` +
|
|
129
|
-
`Original error: ${error.message}. ` +
|
|
130
|
-
`Rollback error: ${rollbackError.message}`);
|
|
131
|
-
aggregateError.originalError = error;
|
|
132
|
-
aggregateError.rollbackError = rollbackError;
|
|
133
|
-
throw aggregateError;
|
|
134
|
-
}
|
|
135
|
-
throw error;
|
|
136
|
-
}
|
|
137
|
-
finally {
|
|
138
|
-
// Clean up CLS context
|
|
139
|
-
if (clsAvailable && clsService) {
|
|
140
|
-
try {
|
|
141
|
-
clsService.set('entityManager', null);
|
|
142
|
-
logger.debug('[Transactional] CLS context cleaned up');
|
|
143
|
-
}
|
|
144
|
-
catch (cleanupError) {
|
|
145
|
-
logger.error('[Transactional] Failed to clean up CLS context. ' +
|
|
146
|
-
`This may lead to context leakage. Error: ${cleanupError.message}`, cleanupError.stack);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
});
|
|
151
|
-
};
|
|
152
|
-
return descriptor;
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* 类级别事务装饰器
|
|
157
|
-
*/
|
|
158
|
-
function ClassTransactional(options) {
|
|
159
|
-
return function (target) {
|
|
160
|
-
(0, common_1.SetMetadata)(exports.TRANSACTION_METADATA_KEY, Object.assign(Object.assign({}, options), { isClassLevel: true }))(target);
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* 事务拦截器
|
|
165
|
-
* 支持类级别和方法级别的事务管理
|
|
166
|
-
*/
|
|
167
|
-
let TransactionalInterceptor = TransactionalInterceptor_1 = class TransactionalInterceptor {
|
|
168
|
-
constructor(transactionManager) {
|
|
169
|
-
this.transactionManager = transactionManager;
|
|
170
|
-
this.logger = new common_1.Logger(TransactionalInterceptor_1.name);
|
|
171
|
-
}
|
|
172
|
-
intercept(context, next) {
|
|
173
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
174
|
-
// 获取处理器
|
|
175
|
-
const handler = context.getHandler();
|
|
176
|
-
const controller = context.getClass();
|
|
177
|
-
// 检查方法或类是否有事务元数据
|
|
178
|
-
const options = this.getTransactionOptions(handler, controller);
|
|
179
|
-
if (!options) {
|
|
180
|
-
// 没有事务注解,直接执行
|
|
181
|
-
return next.handle();
|
|
182
|
-
}
|
|
183
|
-
// 获取事务定义
|
|
184
|
-
const definition = {
|
|
185
|
-
propagation: options.propagation || transaction_manager_1.Propagation.REQUIRED,
|
|
186
|
-
isolationLevel: options.isolation,
|
|
187
|
-
readOnly: options.readOnly,
|
|
188
|
-
timeout: options.timeout || 30,
|
|
189
|
-
name: options.name || `${controller.name}.${handler.name}`,
|
|
190
|
-
dataSourceName: options.dataSource || 'default',
|
|
191
|
-
onTimeout: options.onTimeout,
|
|
192
|
-
};
|
|
193
|
-
// 开始事务
|
|
194
|
-
const status = yield this.transactionManager.getTransaction(definition);
|
|
195
|
-
// 包装执行
|
|
196
|
-
return new rxjs_1.Observable((subscriber) => {
|
|
197
|
-
this.executeWithTransaction(status, next, subscriber)
|
|
198
|
-
.catch((error) => {
|
|
199
|
-
subscriber.error(error);
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
executeWithTransaction(status, next, subscriber) {
|
|
205
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
206
|
-
try {
|
|
207
|
-
// 执行处理器
|
|
208
|
-
const result = yield next.handle().toPromise();
|
|
209
|
-
// 提交事务
|
|
210
|
-
yield this.transactionManager.commit(status);
|
|
211
|
-
subscriber.next(result);
|
|
212
|
-
subscriber.complete();
|
|
213
|
-
}
|
|
214
|
-
catch (error) {
|
|
215
|
-
// 回滚事务
|
|
216
|
-
try {
|
|
217
|
-
yield this.transactionManager.rollback(status);
|
|
218
|
-
}
|
|
219
|
-
catch (rollbackError) {
|
|
220
|
-
this.logger.error('Failed to rollback transaction:', rollbackError);
|
|
221
|
-
}
|
|
222
|
-
throw error;
|
|
223
|
-
}
|
|
224
|
-
});
|
|
225
|
-
}
|
|
226
|
-
getTransactionOptions(handler, controller) {
|
|
227
|
-
// 检查方法级注解
|
|
228
|
-
const methodOptions = Reflect.getMetadata(exports.TRANSACTION_METADATA_KEY, handler);
|
|
229
|
-
if (methodOptions && !methodOptions.isClassLevel) {
|
|
230
|
-
return methodOptions;
|
|
231
|
-
}
|
|
232
|
-
// 检查类级注解
|
|
233
|
-
const classOptions = Reflect.getMetadata(exports.TRANSACTION_METADATA_KEY, controller);
|
|
234
|
-
if (classOptions && (classOptions.isClassLevel || !methodOptions)) {
|
|
235
|
-
return classOptions;
|
|
236
|
-
}
|
|
237
|
-
return null;
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
exports.TransactionalInterceptor = TransactionalInterceptor;
|
|
241
|
-
exports.TransactionalInterceptor = TransactionalInterceptor = TransactionalInterceptor_1 = __decorate([
|
|
242
|
-
(0, common_1.Injectable)(),
|
|
243
|
-
__metadata("design:paramtypes", [transaction_manager_1.TransactionManager])
|
|
244
|
-
], TransactionalInterceptor);
|
|
245
|
-
/**
|
|
246
|
-
* 便捷装饰器工厂
|
|
247
|
-
*/
|
|
248
|
-
exports.Tx = {
|
|
249
|
-
/**
|
|
250
|
-
* 必须在事务中执行(默认)
|
|
251
|
-
*/
|
|
252
|
-
Required: (dataSource) => Transactional({
|
|
253
|
-
propagation: transaction_manager_1.Propagation.REQUIRED,
|
|
254
|
-
dataSource,
|
|
255
|
-
}),
|
|
256
|
-
/**
|
|
257
|
-
* 支持当前事务,非事务执行也行
|
|
258
|
-
*/
|
|
259
|
-
Supports: (dataSource) => Transactional({
|
|
260
|
-
propagation: transaction_manager_1.Propagation.SUPPORTS,
|
|
261
|
-
dataSource,
|
|
262
|
-
}),
|
|
263
|
-
/**
|
|
264
|
-
* 必须在事务中执行,否则抛异常
|
|
265
|
-
*/
|
|
266
|
-
Mandatory: (dataSource) => Transactional({
|
|
267
|
-
propagation: transaction_manager_1.Propagation.MANDATORY,
|
|
268
|
-
dataSource,
|
|
269
|
-
}),
|
|
270
|
-
/**
|
|
271
|
-
* 创建新事务
|
|
272
|
-
*/
|
|
273
|
-
RequiresNew: (dataSource) => Transactional({
|
|
274
|
-
propagation: transaction_manager_1.Propagation.REQUIRES_NEW,
|
|
275
|
-
dataSource,
|
|
276
|
-
}),
|
|
277
|
-
/**
|
|
278
|
-
* 不支持事务,挂起当前事务
|
|
279
|
-
*/
|
|
280
|
-
NotSupported: (dataSource) => Transactional({
|
|
281
|
-
propagation: transaction_manager_1.Propagation.NOT_SUPPORTED,
|
|
282
|
-
dataSource,
|
|
283
|
-
}),
|
|
284
|
-
/**
|
|
285
|
-
* 永不使用事务
|
|
286
|
-
*/
|
|
287
|
-
Never: (dataSource) => Transactional({
|
|
288
|
-
propagation: transaction_manager_1.Propagation.NEVER,
|
|
289
|
-
dataSource,
|
|
290
|
-
}),
|
|
291
|
-
/**
|
|
292
|
-
* 嵌套事务
|
|
293
|
-
*/
|
|
294
|
-
Nested: (dataSource) => Transactional({
|
|
295
|
-
propagation: transaction_manager_1.Propagation.NESTED,
|
|
296
|
-
dataSource,
|
|
297
|
-
}),
|
|
298
|
-
/**
|
|
299
|
-
* 只读事务
|
|
300
|
-
*/
|
|
301
|
-
ReadOnly: (dataSource) => Transactional({
|
|
302
|
-
readOnly: true,
|
|
303
|
-
dataSource,
|
|
304
|
-
}),
|
|
305
|
-
};
|
|
306
|
-
/**
|
|
307
|
-
* 组合装饰器:使用拦截器
|
|
308
|
-
*/
|
|
309
|
-
function UseTransactional(options) {
|
|
310
|
-
return function (target, propertyKey, descriptor) {
|
|
311
|
-
if (descriptor) {
|
|
312
|
-
// 方法装饰器
|
|
313
|
-
return Transactional(options)(target, propertyKey, descriptor);
|
|
314
|
-
}
|
|
315
|
-
else {
|
|
316
|
-
// 类装饰器
|
|
317
|
-
return ClassTransactional(options)(target);
|
|
318
|
-
}
|
|
319
|
-
};
|
|
320
|
-
}
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { DynamicModule } from '@nestjs/common';
|
|
2
|
-
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
|
|
3
|
-
import { DataSource } from 'typeorm';
|
|
4
|
-
import { DataSourceRegistryService } from './data-source-registry';
|
|
5
|
-
/**
|
|
6
|
-
* TypeORM 数据源配置
|
|
7
|
-
*/
|
|
8
|
-
export interface TypeOrmDataSourceConfig {
|
|
9
|
-
name: string;
|
|
10
|
-
options: TypeOrmModuleOptions;
|
|
11
|
-
isDefault?: boolean;
|
|
12
|
-
metadata?: {
|
|
13
|
-
description?: string;
|
|
14
|
-
team?: string;
|
|
15
|
-
environment?: string;
|
|
16
|
-
tags?: string[];
|
|
17
|
-
};
|
|
18
|
-
healthCheck?: {
|
|
19
|
-
enabled?: boolean;
|
|
20
|
-
timeout?: number;
|
|
21
|
-
interval?: number;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* 多数据源 TypeORM 模块配置
|
|
26
|
-
*/
|
|
27
|
-
export interface MultiTypeOrmModuleOptions {
|
|
28
|
-
dataSources: TypeOrmDataSourceConfig[];
|
|
29
|
-
transactionOptions?: {
|
|
30
|
-
defaultDataSource?: string;
|
|
31
|
-
enableDynamicRegistration?: boolean;
|
|
32
|
-
logDataSourceRegistration?: boolean;
|
|
33
|
-
globalTransactionOptions?: {
|
|
34
|
-
dataSources?: string[];
|
|
35
|
-
isolationLevel?: any;
|
|
36
|
-
rollbackOnError?: boolean;
|
|
37
|
-
timeout?: number;
|
|
38
|
-
logDetails?: boolean;
|
|
39
|
-
};
|
|
40
|
-
dataSourceRegistryConfig?: {
|
|
41
|
-
enableHealthCheck?: boolean;
|
|
42
|
-
healthCheckInterval?: number;
|
|
43
|
-
connectionTimeout?: number;
|
|
44
|
-
maxReconnectAttempts?: number;
|
|
45
|
-
logLifecycleEvents?: boolean;
|
|
46
|
-
autoCleanup?: boolean;
|
|
47
|
-
};
|
|
48
|
-
};
|
|
49
|
-
autoInitialize?: boolean;
|
|
50
|
-
global?: boolean;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* TypeORM 模块包装器
|
|
54
|
-
* 提供批量注册和管理多个 TypeORM 数据源的功能
|
|
55
|
-
*/
|
|
56
|
-
export declare class TypeOrmModuleWrapper {
|
|
57
|
-
private static readonly logger;
|
|
58
|
-
/**
|
|
59
|
-
* 批量注册多个 TypeORM 数据源
|
|
60
|
-
*/
|
|
61
|
-
static forRoot(options?: MultiTypeOrmModuleOptions): DynamicModule;
|
|
62
|
-
/**
|
|
63
|
-
* 为特定功能注册数据源
|
|
64
|
-
*/
|
|
65
|
-
static forFeature(dataSourceNames: string[], entities?: any[]): DynamicModule;
|
|
66
|
-
/**
|
|
67
|
-
* 创建数据源提供者
|
|
68
|
-
*/
|
|
69
|
-
static createDataSourceProvider(name: string): {
|
|
70
|
-
provide: string;
|
|
71
|
-
useFactory: (registry: DataSourceRegistryService) => Promise<DataSource>;
|
|
72
|
-
inject: (typeof DataSourceRegistryService)[];
|
|
73
|
-
};
|
|
74
|
-
/**
|
|
75
|
-
* 创建 EntityManager 提供者
|
|
76
|
-
*/
|
|
77
|
-
static createEntityManagerProvider(name: string): {
|
|
78
|
-
provide: string;
|
|
79
|
-
useFactory: (registry: DataSourceRegistryService) => Promise<import("typeorm").EntityManager>;
|
|
80
|
-
inject: (typeof DataSourceRegistryService)[];
|
|
81
|
-
};
|
|
82
|
-
/**
|
|
83
|
-
* 创建 Repository 提供者
|
|
84
|
-
*/
|
|
85
|
-
static createRepositoryProvider(entity: any, dataSourceName?: string): {
|
|
86
|
-
provide: string;
|
|
87
|
-
useFactory: (registry: DataSourceRegistryService) => Promise<import("typeorm").Repository<import("typeorm").ObjectLiteral>>;
|
|
88
|
-
inject: (typeof DataSourceRegistryService)[];
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* 全局多数据源 TypeORM 模块
|
|
93
|
-
*/
|
|
94
|
-
export declare class GlobalMultiTypeOrmModule {
|
|
95
|
-
static forRoot(options: MultiTypeOrmModuleOptions): DynamicModule;
|
|
96
|
-
}
|
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
-
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;
|
|
6
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
-
};
|
|
8
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
-
});
|
|
16
|
-
};
|
|
17
|
-
var TypeOrmModuleWrapper_1;
|
|
18
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
|
-
exports.GlobalMultiTypeOrmModule = exports.TypeOrmModuleWrapper = void 0;
|
|
20
|
-
const common_1 = require("@nestjs/common");
|
|
21
|
-
const typeorm_1 = require("@nestjs/typeorm");
|
|
22
|
-
const transaction_module_1 = require("./transaction.module");
|
|
23
|
-
const data_source_registry_1 = require("./data-source-registry");
|
|
24
|
-
/**
|
|
25
|
-
* TypeORM 模块包装器
|
|
26
|
-
* 提供批量注册和管理多个 TypeORM 数据源的功能
|
|
27
|
-
*/
|
|
28
|
-
let TypeOrmModuleWrapper = TypeOrmModuleWrapper_1 = class TypeOrmModuleWrapper {
|
|
29
|
-
/**
|
|
30
|
-
* 批量注册多个 TypeORM 数据源
|
|
31
|
-
*/
|
|
32
|
-
static forRoot(options) {
|
|
33
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
34
|
-
const imports = [];
|
|
35
|
-
const providers = [];
|
|
36
|
-
const exports = [];
|
|
37
|
-
// 验证数据源配置
|
|
38
|
-
if (options === null || options === void 0 ? void 0 : options.dataSources) {
|
|
39
|
-
for (const config of options.dataSources) {
|
|
40
|
-
if (config.name === 'default') {
|
|
41
|
-
throw new Error('DataSource name "default" is reserved and cannot be used. ' +
|
|
42
|
-
'Please use a different name, such as "main", "primary", or "app".');
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
// 1. 导入 TypeORM 模块
|
|
47
|
-
const typeOrmModules = ((_a = options === null || options === void 0 ? void 0 : options.dataSources) === null || _a === void 0 ? void 0 : _a.map(config => {
|
|
48
|
-
return typeorm_1.TypeOrmModule.forRoot(Object.assign(Object.assign({}, config.options), { name: config.name,
|
|
49
|
-
// 支持事务
|
|
50
|
-
retryAttempts: 3, retryDelay: 3000 }));
|
|
51
|
-
})) || [];
|
|
52
|
-
imports.push(...typeOrmModules);
|
|
53
|
-
// 2. 导入事务模块
|
|
54
|
-
const defaultDataSource = ((_c = (_b = options === null || options === void 0 ? void 0 : options.dataSources) === null || _b === void 0 ? void 0 : _b.find(ds => ds.isDefault)) === null || _c === void 0 ? void 0 : _c.name) ||
|
|
55
|
-
((_d = options === null || options === void 0 ? void 0 : options.transactionOptions) === null || _d === void 0 ? void 0 : _d.defaultDataSource) ||
|
|
56
|
-
((_f = (_e = options === null || options === void 0 ? void 0 : options.dataSources) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.name) ||
|
|
57
|
-
'main';
|
|
58
|
-
imports.push(transaction_module_1.TransactionModule.forRoot({
|
|
59
|
-
defaultDataSource,
|
|
60
|
-
enableDynamicRegistration: (_h = (_g = options === null || options === void 0 ? void 0 : options.transactionOptions) === null || _g === void 0 ? void 0 : _g.enableDynamicRegistration) !== null && _h !== void 0 ? _h : true,
|
|
61
|
-
logDataSourceRegistration: (_k = (_j = options === null || options === void 0 ? void 0 : options.transactionOptions) === null || _j === void 0 ? void 0 : _j.logDataSourceRegistration) !== null && _k !== void 0 ? _k : true,
|
|
62
|
-
globalTransactionOptions: (_l = options === null || options === void 0 ? void 0 : options.transactionOptions) === null || _l === void 0 ? void 0 : _l.globalTransactionOptions,
|
|
63
|
-
dataSourceRegistryConfig: (_m = options === null || options === void 0 ? void 0 : options.transactionOptions) === null || _m === void 0 ? void 0 : _m.dataSourceRegistryConfig,
|
|
64
|
-
}));
|
|
65
|
-
// 3. 创建数据源配置提供者
|
|
66
|
-
providers.push({
|
|
67
|
-
provide: 'TYPEORM_DATASOURCE_CONFIGS',
|
|
68
|
-
useValue: (options === null || options === void 0 ? void 0 : options.dataSources) || [],
|
|
69
|
-
});
|
|
70
|
-
// 4. 创建数据源初始化服务
|
|
71
|
-
if ((options === null || options === void 0 ? void 0 : options.autoInitialize) !== false) {
|
|
72
|
-
providers.push({
|
|
73
|
-
provide: 'DATASOURCE_INITIALIZER',
|
|
74
|
-
useFactory: (registry) => __awaiter(this, void 0, void 0, function* () {
|
|
75
|
-
var _a, _b;
|
|
76
|
-
// 将配置的数据源注册到注册表
|
|
77
|
-
for (const config of (options === null || options === void 0 ? void 0 : options.dataSources) || []) {
|
|
78
|
-
try {
|
|
79
|
-
// 获取已初始化的数据源
|
|
80
|
-
// 注意:TypeORMModule.forRoot 已经初始化了数据源
|
|
81
|
-
// 这里我们只是注册到注册表以便管理
|
|
82
|
-
TypeOrmModuleWrapper_1.logger.log(`Configuring DataSource: ${config.name}`);
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
TypeOrmModuleWrapper_1.logger.error(`Failed to configure DataSource ${config.name}:`, error.stack);
|
|
86
|
-
if (!((_b = (_a = options === null || options === void 0 ? void 0 : options.transactionOptions) === null || _a === void 0 ? void 0 : _a.globalTransactionOptions) === null || _b === void 0 ? void 0 : _b.rollbackOnError)) {
|
|
87
|
-
throw error;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}),
|
|
92
|
-
inject: [data_source_registry_1.DataSourceRegistryService],
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
// 5. 导出所有注册的数据源(通过 token)- 需要先添加到 providers
|
|
96
|
-
for (const config of (options === null || options === void 0 ? void 0 : options.dataSources) || []) {
|
|
97
|
-
const dataSourceProvider = {
|
|
98
|
-
provide: `DATASOURCE_${config.name.toUpperCase()}`,
|
|
99
|
-
useFactory: (registry) => {
|
|
100
|
-
return registry.getDataSource(config.name);
|
|
101
|
-
},
|
|
102
|
-
inject: [data_source_registry_1.DataSourceRegistryService],
|
|
103
|
-
};
|
|
104
|
-
providers.push(dataSourceProvider);
|
|
105
|
-
exports.push(`DATASOURCE_${config.name.toUpperCase()}`);
|
|
106
|
-
}
|
|
107
|
-
// 6. 重新导出 TransactionModule(它会导出 DataSourceRegistryService 等)
|
|
108
|
-
exports.push(transaction_module_1.TransactionModule);
|
|
109
|
-
// 7. 重新导出 TypeORM 模块
|
|
110
|
-
exports.push(...typeOrmModules);
|
|
111
|
-
return {
|
|
112
|
-
module: TypeOrmModuleWrapper_1,
|
|
113
|
-
imports,
|
|
114
|
-
providers,
|
|
115
|
-
exports,
|
|
116
|
-
global: options === null || options === void 0 ? void 0 : options.global,
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* 为特定功能注册数据源
|
|
121
|
-
*/
|
|
122
|
-
static forFeature(dataSourceNames, entities) {
|
|
123
|
-
const imports = dataSourceNames.map(name => typeorm_1.TypeOrmModule.forFeature(entities || [], name));
|
|
124
|
-
return {
|
|
125
|
-
module: TypeOrmModuleWrapper_1,
|
|
126
|
-
imports,
|
|
127
|
-
exports: imports,
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* 创建数据源提供者
|
|
132
|
-
*/
|
|
133
|
-
static createDataSourceProvider(name) {
|
|
134
|
-
return {
|
|
135
|
-
provide: `DATASOURCE_${name.toUpperCase()}`,
|
|
136
|
-
useFactory: (registry) => __awaiter(this, void 0, void 0, function* () {
|
|
137
|
-
const dataSource = registry.getDataSource(name);
|
|
138
|
-
if (!dataSource) {
|
|
139
|
-
throw new Error(`DataSource '${name}' not found`);
|
|
140
|
-
}
|
|
141
|
-
return dataSource;
|
|
142
|
-
}),
|
|
143
|
-
inject: [data_source_registry_1.DataSourceRegistryService],
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* 创建 EntityManager 提供者
|
|
148
|
-
*/
|
|
149
|
-
static createEntityManagerProvider(name) {
|
|
150
|
-
return {
|
|
151
|
-
provide: `ENTITY_MANAGER_${name.toUpperCase()}`,
|
|
152
|
-
useFactory: (registry) => __awaiter(this, void 0, void 0, function* () {
|
|
153
|
-
const dataSource = registry.getDataSource(name);
|
|
154
|
-
if (!dataSource) {
|
|
155
|
-
throw new Error(`DataSource '${name}' not found`);
|
|
156
|
-
}
|
|
157
|
-
return dataSource.manager;
|
|
158
|
-
}),
|
|
159
|
-
inject: [data_source_registry_1.DataSourceRegistryService],
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* 创建 Repository 提供者
|
|
164
|
-
*/
|
|
165
|
-
static createRepositoryProvider(entity, dataSourceName) {
|
|
166
|
-
return {
|
|
167
|
-
provide: `${entity.name}Repository`,
|
|
168
|
-
useFactory: (registry) => __awaiter(this, void 0, void 0, function* () {
|
|
169
|
-
const dsName = dataSourceName || 'default';
|
|
170
|
-
const dataSource = registry.getDataSource(dsName);
|
|
171
|
-
if (!dataSource) {
|
|
172
|
-
throw new Error(`DataSource '${dsName}' not found`);
|
|
173
|
-
}
|
|
174
|
-
return dataSource.getRepository(entity);
|
|
175
|
-
}),
|
|
176
|
-
inject: [data_source_registry_1.DataSourceRegistryService],
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
};
|
|
180
|
-
exports.TypeOrmModuleWrapper = TypeOrmModuleWrapper;
|
|
181
|
-
TypeOrmModuleWrapper.logger = new common_1.Logger(TypeOrmModuleWrapper_1.name);
|
|
182
|
-
exports.TypeOrmModuleWrapper = TypeOrmModuleWrapper = TypeOrmModuleWrapper_1 = __decorate([
|
|
183
|
-
(0, common_1.Module)({})
|
|
184
|
-
], TypeOrmModuleWrapper);
|
|
185
|
-
/**
|
|
186
|
-
* 全局多数据源 TypeORM 模块
|
|
187
|
-
*/
|
|
188
|
-
let GlobalMultiTypeOrmModule = class GlobalMultiTypeOrmModule {
|
|
189
|
-
static forRoot(options) {
|
|
190
|
-
return TypeOrmModuleWrapper.forRoot(Object.assign(Object.assign({}, options), { global: true }));
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
exports.GlobalMultiTypeOrmModule = GlobalMultiTypeOrmModule;
|
|
194
|
-
exports.GlobalMultiTypeOrmModule = GlobalMultiTypeOrmModule = __decorate([
|
|
195
|
-
(0, common_1.Global)(),
|
|
196
|
-
(0, common_1.Module)({})
|
|
197
|
-
], GlobalMultiTypeOrmModule);
|