@nest-omni/core 4.1.3-15 → 4.1.3-18

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.
Files changed (55) hide show
  1. package/audit/audit.module.js +7 -0
  2. package/audit/services/entity-audit.service.js +2 -1
  3. package/audit/services/manual-audit-log.service.js +2 -2
  4. package/audit/services/multi-database.service.d.ts +0 -5
  5. package/audit/services/multi-database.service.js +0 -24
  6. package/audit/services/transaction-audit.service.js +3 -2
  7. package/cache/dependencies/db.dependency.d.ts +2 -7
  8. package/cache/dependencies/db.dependency.js +4 -11
  9. package/http-client/examples/proxy-from-environment.example.d.ts +1 -1
  10. package/http-client/examples/proxy-from-environment.example.js +18 -19
  11. package/http-client/services/logging.service.js +2 -3
  12. package/index.d.ts +1 -1
  13. package/index.js +1 -1
  14. package/package.json +3 -2
  15. package/setup/bootstrap.setup.d.ts +1 -1
  16. package/shared/service-registry.module.js +2 -15
  17. package/shared/services/api-config.service.js +1 -0
  18. package/validators/is-exists.validator.d.ts +2 -7
  19. package/validators/is-exists.validator.js +2 -24
  20. package/validators/is-unique.validator.d.ts +2 -7
  21. package/validators/is-unique.validator.js +2 -24
  22. package/transaction/__tests__/mocks.d.ts +0 -9
  23. package/transaction/__tests__/mocks.js +0 -33
  24. package/transaction/base-service-transaction.d.ts +0 -106
  25. package/transaction/base-service-transaction.js +0 -317
  26. package/transaction/cls-compatibility.service.d.ts +0 -55
  27. package/transaction/cls-compatibility.service.js +0 -127
  28. package/transaction/data-source-registry.d.ts +0 -91
  29. package/transaction/data-source-registry.js +0 -349
  30. package/transaction/data-source.util.d.ts +0 -142
  31. package/transaction/data-source.util.js +0 -330
  32. package/transaction/database-adapter.d.ts +0 -44
  33. package/transaction/database-adapter.js +0 -240
  34. package/transaction/decorators/entity-datasource.decorator.d.ts +0 -62
  35. package/transaction/decorators/entity-datasource.decorator.js +0 -105
  36. package/transaction/index.d.ts +0 -16
  37. package/transaction/index.js +0 -71
  38. package/transaction/logging-transactional.interceptor.d.ts +0 -18
  39. package/transaction/logging-transactional.interceptor.js +0 -163
  40. package/transaction/transaction-context.service.d.ts +0 -137
  41. package/transaction/transaction-context.service.js +0 -411
  42. package/transaction/transaction-manager.d.ts +0 -230
  43. package/transaction/transaction-manager.holder.d.ts +0 -31
  44. package/transaction/transaction-manager.holder.js +0 -42
  45. package/transaction/transaction-manager.js +0 -1001
  46. package/transaction/transaction-synchronization.d.ts +0 -171
  47. package/transaction/transaction-synchronization.js +0 -380
  48. package/transaction/transaction.errors.d.ts +0 -91
  49. package/transaction/transaction.errors.js +0 -206
  50. package/transaction/transaction.module.d.ts +0 -30
  51. package/transaction/transaction.module.js +0 -126
  52. package/transaction/transactional.decorator.d.ts +0 -82
  53. package/transaction/transactional.decorator.js +0 -320
  54. package/transaction/typeorm-module-wrapper.d.ts +0 -96
  55. package/transaction/typeorm-module-wrapper.js +0 -197
@@ -1,163 +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 LoggingTransactionalInterceptor_1;
21
- Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.LoggingTransactionalInterceptor = void 0;
23
- const common_1 = require("@nestjs/common");
24
- const rxjs_1 = require("rxjs");
25
- const transaction_manager_1 = require("./transaction-manager");
26
- const nestjs_cls_1 = require("nestjs-cls");
27
- /**
28
- * 带日志的事务拦截器
29
- * 提供详细的事务执行日志
30
- */
31
- let LoggingTransactionalInterceptor = LoggingTransactionalInterceptor_1 = class LoggingTransactionalInterceptor {
32
- constructor(transactionManager) {
33
- this.transactionManager = transactionManager;
34
- this.logger = new common_1.Logger(LoggingTransactionalInterceptor_1.name);
35
- }
36
- intercept(context, next) {
37
- // 检查是否有事务元数据
38
- const options = this.getTransactionOptions(context);
39
- if (!options) {
40
- return next.handle();
41
- }
42
- // 异步执行事务处理
43
- return (0, rxjs_1.from)(this.handleTransaction(context, next, options));
44
- }
45
- handleTransaction(context, next, options) {
46
- return __awaiter(this, void 0, void 0, function* () {
47
- const startTime = Date.now();
48
- let status;
49
- try {
50
- // 获取事务定义
51
- const definition = {
52
- propagation: options.propagation || transaction_manager_1.Propagation.REQUIRED,
53
- isolationLevel: options.isolation,
54
- readOnly: options.readOnly || false,
55
- timeout: options.timeout || 30,
56
- name: this.getTransactionName(context, options),
57
- dataSourceName: options.dataSource || 'default',
58
- };
59
- // 记录事务开始
60
- this.logger.log(`Starting transaction: ${definition.name} ` +
61
- `(propagation: ${transaction_manager_1.Propagation[definition.propagation]}, ` +
62
- `dataSource: ${definition.dataSourceName}, ` +
63
- `timeout: ${definition.timeout}s)`);
64
- // 获取事务
65
- status = yield this.transactionManager.getTransaction(definition);
66
- // 记录事务状态
67
- if (status.isNewTransaction) {
68
- this.logger.debug(`New transaction created on ${definition.dataSourceName}`);
69
- }
70
- else {
71
- this.logger.debug(`Joined existing transaction on ${definition.dataSourceName} ` +
72
- `(level: ${status.nestingLevel})`);
73
- }
74
- // 设置 CLS 上下文
75
- yield this.setClsContext(status);
76
- // 执行方法
77
- const result = yield next.handle().toPromise();
78
- // 记录成功
79
- const duration = Date.now() - startTime;
80
- this.logger.log(`Transaction ${definition.name} completed successfully ` +
81
- `in ${duration}ms`);
82
- // 提交事务
83
- yield this.transactionManager.commit(status);
84
- this.logger.debug(`Transaction ${definition.name} committed`);
85
- return result;
86
- }
87
- catch (error) {
88
- // 记录错误
89
- const duration = Date.now() - startTime;
90
- this.logger.error(`Transaction failed: ${error.message} ` +
91
- `(duration: ${duration}ms)`, error.stack);
92
- // 回滚事务
93
- if (status) {
94
- try {
95
- yield this.transactionManager.rollback(status);
96
- this.logger.debug(`Transaction rolled back successfully`);
97
- }
98
- catch (rollbackError) {
99
- this.logger.error(`Failed to rollback transaction: ${rollbackError.message}`, rollbackError.stack);
100
- }
101
- }
102
- throw error;
103
- }
104
- finally {
105
- // 清理 CLS 上下文
106
- this.clearClsContext();
107
- }
108
- });
109
- }
110
- getTransactionOptions(context) {
111
- const handler = context.getHandler();
112
- const controller = context.getClass();
113
- // 检查方法级注解
114
- const methodOptions = Reflect.getMetadata('transactional', handler);
115
- if (methodOptions && !methodOptions.isClassLevel) {
116
- return methodOptions;
117
- }
118
- // 检查类级注解
119
- const classOptions = Reflect.getMetadata('transactional', controller);
120
- if (classOptions && (classOptions.isClassLevel || !methodOptions)) {
121
- return classOptions;
122
- }
123
- return null;
124
- }
125
- getTransactionName(context, options) {
126
- if (options.name) {
127
- return options.name;
128
- }
129
- const handler = context.getHandler();
130
- const controller = context.getClass();
131
- return `${controller.name}.${handler.name}`;
132
- }
133
- setClsContext(status) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- var _a;
136
- const clsService = (_a = nestjs_cls_1.ClsServiceManager.getClsService) === null || _a === void 0 ? void 0 : _a.call(nestjs_cls_1.ClsServiceManager);
137
- if (clsService) {
138
- const em = this.transactionManager.getCurrentEntityManager(status.dataSourceName);
139
- if (em) {
140
- clsService.set('entityManager', em);
141
- }
142
- clsService.set('transactionId', `${status.dataSourceName}_${Date.now()}`);
143
- clsService.set('dataSource', status.dataSourceName);
144
- clsService.set('startTime', status.startTime);
145
- }
146
- });
147
- }
148
- clearClsContext() {
149
- var _a;
150
- const clsService = (_a = nestjs_cls_1.ClsServiceManager.getClsService) === null || _a === void 0 ? void 0 : _a.call(nestjs_cls_1.ClsServiceManager);
151
- if (clsService) {
152
- clsService.set('entityManager', null);
153
- clsService.set('transactionId', null);
154
- clsService.set('dataSource', null);
155
- clsService.set('startTime', null);
156
- }
157
- }
158
- };
159
- exports.LoggingTransactionalInterceptor = LoggingTransactionalInterceptor;
160
- exports.LoggingTransactionalInterceptor = LoggingTransactionalInterceptor = LoggingTransactionalInterceptor_1 = __decorate([
161
- (0, common_1.Injectable)(),
162
- __metadata("design:paramtypes", [transaction_manager_1.TransactionManager])
163
- ], LoggingTransactionalInterceptor);
@@ -1,137 +0,0 @@
1
- import { OnModuleInit } from '@nestjs/common';
2
- import { DataSource, EntityManager, DataSourceOptions } from 'typeorm';
3
- import { ModuleRef } from '@nestjs/core';
4
- /**
5
- * 事务上下文信息
6
- */
7
- interface TransactionContext {
8
- dataSourceName: string;
9
- entityManager: EntityManager;
10
- dataSource: DataSource;
11
- }
12
- /**
13
- * 动态注册的数据源配置
14
- */
15
- export interface DynamicDataSourceConfig {
16
- name: string;
17
- dataSource: DataSource;
18
- options?: DataSourceOptions;
19
- metadata?: Record<string, any>;
20
- createdAt: Date;
21
- }
22
- /**
23
- * 事务上下文管理器
24
- * 使用 RequestScope 确保每个请求都有独立的事务上下文
25
- */
26
- export declare class TransactionContextService implements OnModuleInit {
27
- private readonly moduleRef;
28
- private readonly config?;
29
- private static readonly logger;
30
- private readonly contextMap;
31
- constructor(moduleRef: ModuleRef, config?: {
32
- defaultDataSource?: string;
33
- enableDynamicRegistration?: boolean;
34
- logDataSourceRegistration?: boolean;
35
- });
36
- onModuleInit(): Promise<void>;
37
- /**
38
- * 获取当前请求的事务上下文服务
39
- */
40
- static getCurrent(): TransactionContextService | undefined;
41
- /**
42
- * 获取当前请求的数据源
43
- * 方便其他模块快速访问数据源
44
- * @param dataSourceName 数据源名称,默认为 'default'
45
- * @returns DataSource 实例
46
- */
47
- static getDataSource(dataSourceName?: string): DataSource;
48
- /**
49
- * 设置事务上下文
50
- */
51
- setContext(dataSourceName: string, entityManager: EntityManager): void;
52
- /**
53
- * 获取事务上下文中的 EntityManager
54
- */
55
- getEntityManager(dataSourceName?: string): EntityManager | null;
56
- /**
57
- * 获取事务上下文中的 DataSource
58
- * 如果上下文中没有,则尝试从 TypeORM 模块或全局注册表获取
59
- */
60
- getDataSource(dataSourceName?: string): DataSource | null;
61
- /**
62
- * 检查是否在事务中
63
- */
64
- isInTransaction(dataSourceName?: string): boolean;
65
- /**
66
- * 获取所有活跃的事务上下文
67
- */
68
- getAllActiveContexts(): Map<string, TransactionContext>;
69
- /**
70
- * 清理所有事务上下文
71
- */
72
- clearContext(): void;
73
- /**
74
- * 清理指定数据源的事务上下文
75
- */
76
- clearContextForDataSource(dataSourceName: string): void;
77
- /**
78
- * 执行带事务的操作
79
- * 如果已在事务中,使用现有的事务管理器
80
- * 否则创建新事务
81
- */
82
- executeInTransaction<T>(dataSourceName: string, operation: (entityManager: EntityManager) => Promise<T>, isolationLevel?: any): Promise<T>;
83
- /**
84
- * 获取 DataSource 实例
85
- */
86
- private getDataSourceInstance;
87
- /**
88
- * 动态注册数据源
89
- */
90
- static addDataSource(name: string, dataSource: DataSource, options?: {
91
- options?: DataSourceOptions;
92
- metadata?: Record<string, any>;
93
- replaceExisting?: boolean;
94
- }): Promise<void>;
95
- /**
96
- * 移除动态注册的数据源
97
- */
98
- static removeDataSource(name: string): Promise<void>;
99
- /**
100
- * 获取所有已注册的数据源名称
101
- */
102
- static getRegisteredDataSources(): string[];
103
- /**
104
- * 获取动态数据源的配置
105
- */
106
- static getDataSourceConfig(name: string): DynamicDataSourceConfig | undefined;
107
- /**
108
- * 检查数据源是否已注册
109
- */
110
- static isDataSourceRegistered(name: string): boolean;
111
- /**
112
- * 批量注册数据源
113
- */
114
- static addDataSources(dataSources: Array<{
115
- name: string;
116
- dataSource: DataSource;
117
- options?: DataSourceOptions;
118
- metadata?: Record<string, any>;
119
- }>, options?: {
120
- replaceExisting?: boolean;
121
- continueOnError?: boolean;
122
- }): Promise<void>;
123
- /**
124
- * 清理全局注册表(用于测试)
125
- */
126
- static clearGlobalRegistry(): void;
127
- /**
128
- * 获取默认数据源名称
129
- */
130
- getDefaultDataSourceName(): string;
131
- /**
132
- * 跨多个数据源执行分布式事务
133
- * 使用两阶段提交模式
134
- */
135
- executeDistributedTransaction<T>(dataSourceNames: string[], operation: (entityManagers: Map<string, EntityManager>) => Promise<T>): Promise<T>;
136
- }
137
- export {};