@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.
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 -2
  8. package/cache/dependencies/db.dependency.js +4 -4
  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,206 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.TransactionErrorHandler = exports.TransactionError = void 0;
13
- /**
14
- * 事务相关错误类
15
- * 提供统一的错误处理机制
16
- */
17
- class TransactionError extends Error {
18
- constructor(message, code, dataSource, transactionId, cause) {
19
- super(message);
20
- this.code = code;
21
- this.dataSource = dataSource;
22
- this.transactionId = transactionId;
23
- this.cause = cause;
24
- this.name = 'TransactionError';
25
- // 保持堆栈跟踪
26
- if (Error.captureStackTrace) {
27
- Error.captureStackTrace(this, TransactionError);
28
- }
29
- }
30
- /**
31
- * 创建事务开始失败错误
32
- */
33
- static beginFailed(message, dataSource, cause) {
34
- return new TransactionError(`Transaction begin failed: ${message}`, 'TRANSACTION_BEGIN_FAILED', dataSource, undefined, cause);
35
- }
36
- /**
37
- * 创建事务提交失败错误
38
- */
39
- static commitFailed(message, dataSource, transactionId, cause) {
40
- return new TransactionError(`Transaction commit failed: ${message}`, 'TRANSACTION_COMMIT_FAILED', dataSource, transactionId, cause);
41
- }
42
- /**
43
- * 创建事务回滚失败错误
44
- */
45
- static rollbackFailed(message, dataSource, transactionId, cause) {
46
- return new TransactionError(`Transaction rollback failed: ${message}`, 'TRANSACTION_ROLLBACK_FAILED', dataSource, transactionId, cause);
47
- }
48
- /**
49
- * 创建事务超时错误
50
- */
51
- static timeout(message, dataSource, transactionId) {
52
- return new TransactionError(`Transaction timeout: ${message}`, 'TRANSACTION_TIMEOUT', dataSource, transactionId);
53
- }
54
- /**
55
- * 创建事务状态无效错误
56
- */
57
- static invalidState(message, dataSource, transactionId) {
58
- return new TransactionError(`Invalid transaction state: ${message}`, 'TRANSACTION_INVALID_STATE', dataSource, transactionId);
59
- }
60
- /**
61
- * 创建数据源连接错误
62
- */
63
- static dataSourceNotFound(dataSource) {
64
- return new TransactionError(`DataSource not found: ${dataSource}`, 'DATA_SOURCE_NOT_FOUND', dataSource);
65
- }
66
- /**
67
- * 创建传播行为不支持错误
68
- */
69
- static unsupportedPropagation(propagation) {
70
- return new TransactionError(`Unsupported propagation: ${propagation}`, 'UNSUPPORTED_PROPAGATION');
71
- }
72
- /**
73
- * 创建嵌套事务错误
74
- */
75
- static nestedTransactionFailed(message, dataSource, cause) {
76
- return new TransactionError(`Nested transaction failed: ${message}`, 'NESTED_TRANSACTION_FAILED', dataSource, undefined, cause);
77
- }
78
- /**
79
- * 转换为JSON
80
- */
81
- toJSON() {
82
- var _a;
83
- return {
84
- name: this.name,
85
- message: this.message,
86
- code: this.code,
87
- dataSource: this.dataSource,
88
- transactionId: this.transactionId,
89
- stack: this.stack,
90
- cause: (_a = this.cause) === null || _a === void 0 ? void 0 : _a.message,
91
- };
92
- }
93
- /**
94
- * 检查是否为特定错误代码
95
- */
96
- isErrorCode(code) {
97
- return this.code === code;
98
- }
99
- /**
100
- * 检查是否为连接相关错误
101
- */
102
- isConnectionError() {
103
- return [
104
- 'DATA_SOURCE_NOT_FOUND',
105
- 'TRANSACTION_BEGIN_FAILED',
106
- ].includes(this.code);
107
- }
108
- /**
109
- * 检查是否为可重试错误
110
- */
111
- isRetryable() {
112
- return [
113
- 'TRANSACTION_BEGIN_FAILED',
114
- 'TRANSACTION_COMMIT_FAILED',
115
- 'TRANSACTION_ROLLBACK_FAILED',
116
- ].includes(this.code);
117
- }
118
- }
119
- exports.TransactionError = TransactionError;
120
- /**
121
- * 事务错误处理工具类
122
- */
123
- class TransactionErrorHandler {
124
- /**
125
- * 设置日志记录器
126
- */
127
- static setLogger(logger) {
128
- this.logger = logger;
129
- }
130
- /**
131
- * 安全地执行操作,捕获并转换错误
132
- */
133
- static safeExecute(operation, errorFactory, context) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- try {
136
- return yield operation();
137
- }
138
- catch (error) {
139
- const transactionError = errorFactory(error);
140
- // 记录错误上下文
141
- if (context) {
142
- this.logger.error(`[TransactionErrorHandler] Error in context "${context}":`, transactionError.toJSON());
143
- }
144
- throw transactionError;
145
- }
146
- });
147
- }
148
- /**
149
- * 处理事务超时
150
- */
151
- static handleTimeout(timeoutMs, dataSource, transactionId) {
152
- return TransactionError.timeout(`Transaction exceeded timeout of ${timeoutMs}ms`, dataSource, transactionId);
153
- }
154
- /**
155
- * 处理回滚失败
156
- */
157
- static safeRollback(rollbackFn, dataSource, transactionId) {
158
- return __awaiter(this, void 0, void 0, function* () {
159
- try {
160
- yield rollbackFn();
161
- }
162
- catch (error) {
163
- const rollbackError = TransactionError.rollbackFailed(error.message, dataSource, transactionId, error);
164
- // 记录回滚失败,但不抛出(避免掩盖原始错误)
165
- this.logger.error('[TransactionErrorHandler] Rollback failed:', rollbackError.toJSON());
166
- }
167
- });
168
- }
169
- /**
170
- * 格式化错误消息
171
- */
172
- static formatMessage(error) {
173
- let message = `[${error.code}] ${error.message}`;
174
- if (error.dataSource) {
175
- message += ` (DataSource: ${error.dataSource})`;
176
- }
177
- if (error.transactionId) {
178
- message += ` (TxId: ${error.transactionId})`;
179
- }
180
- return message;
181
- }
182
- /**
183
- * 判断是否应该重试
184
- */
185
- static shouldRetry(error, attempt, maxAttempts = 3) {
186
- if (attempt >= maxAttempts) {
187
- return false;
188
- }
189
- // 只对 TransactionError 且可重试的错误进行重试
190
- if (error instanceof TransactionError) {
191
- return error.isRetryable();
192
- }
193
- // 对于其他错误,如果是网络相关或临时性错误,也可以重试
194
- const retryablePatterns = [
195
- /connection/i,
196
- /timeout/i,
197
- /temporary/i,
198
- /network/i,
199
- /ECONNRESET/,
200
- /ETIMEDOUT/,
201
- ];
202
- return retryablePatterns.some(pattern => pattern.test(error.message) || pattern.test(error.name));
203
- }
204
- }
205
- exports.TransactionErrorHandler = TransactionErrorHandler;
206
- TransactionErrorHandler.logger = console; // 在实际使用中应该注入 Logger
@@ -1,30 +0,0 @@
1
- import { DynamicModule } from '@nestjs/common';
2
- import { DataSourceRegistryConfig } from './data-source-registry';
3
- /**
4
- * 事务模块配置
5
- */
6
- export interface TransactionModuleOptions {
7
- defaultDataSource?: string;
8
- globalTransactionOptions?: {
9
- dataSources?: string[];
10
- isolationLevel?: any;
11
- rollbackOnError?: boolean;
12
- timeout?: number;
13
- logDetails?: boolean;
14
- };
15
- enableDynamicRegistration?: boolean;
16
- logDataSourceRegistration?: boolean;
17
- dataSourceRegistryConfig?: DataSourceRegistryConfig;
18
- useLoggingInterceptor?: boolean;
19
- }
20
- /**
21
- * 事务模块
22
- */
23
- export declare class TransactionModule {
24
- static forRoot(options?: TransactionModuleOptions): DynamicModule;
25
- static forFeature(dataSourceName?: string, interceptorOptions?: {
26
- dataSources?: string[];
27
- isolationLevel?: any;
28
- rollbackOnError?: boolean;
29
- }): DynamicModule;
30
- }
@@ -1,126 +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 __param = (this && this.__param) || function (paramIndex, decorator) {
12
- return function (target, key) { decorator(target, key, paramIndex); }
13
- };
14
- var TransactionModuleInitService_1, TransactionModule_1;
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- exports.TransactionModule = void 0;
17
- const common_1 = require("@nestjs/common");
18
- const transaction_context_service_1 = require("./transaction-context.service");
19
- const transactional_decorator_1 = require("./transactional.decorator");
20
- const transaction_manager_1 = require("./transaction-manager");
21
- const transaction_manager_2 = require("./transaction-manager");
22
- const data_source_registry_1 = require("./data-source-registry");
23
- const logging_transactional_interceptor_1 = require("./logging-transactional.interceptor");
24
- const transaction_manager_holder_1 = require("./transaction-manager.holder");
25
- /**
26
- * 事务模块初始化服务
27
- * 用于在模块启动时设置全局 TransactionManager 持有者
28
- */
29
- let TransactionModuleInitService = TransactionModuleInitService_1 = class TransactionModuleInitService {
30
- constructor(transactionManager) {
31
- this.transactionManager = transactionManager;
32
- this.logger = new common_1.Logger(TransactionModuleInitService_1.name);
33
- }
34
- onModuleInit() {
35
- // 设置全局事务管理器持有者
36
- transaction_manager_holder_1.TransactionManagerHolder.setManager(this.transactionManager);
37
- this.logger.log('TransactionManagerHolder initialized');
38
- }
39
- };
40
- TransactionModuleInitService = TransactionModuleInitService_1 = __decorate([
41
- (0, common_1.Injectable)(),
42
- __param(0, (0, common_1.Inject)((0, common_1.forwardRef)(() => transaction_manager_1.TransactionManager))),
43
- __metadata("design:paramtypes", [transaction_manager_1.TransactionManager])
44
- ], TransactionModuleInitService);
45
- /**
46
- * 事务模块
47
- */
48
- let TransactionModule = TransactionModule_1 = class TransactionModule {
49
- static forRoot(options) {
50
- var _a, _b, _c, _d, _e;
51
- const providers = [
52
- transaction_context_service_1.TransactionContextService,
53
- transaction_manager_2.TransactionContextManager,
54
- transaction_manager_1.TransactionManager,
55
- transactional_decorator_1.TransactionalInterceptor,
56
- TransactionModuleInitService,
57
- {
58
- provide: 'TRANSACTION_CONFIG',
59
- useValue: {
60
- defaultDataSource: (options === null || options === void 0 ? void 0 : options.defaultDataSource) || 'default',
61
- enableDynamicRegistration: (_a = options === null || options === void 0 ? void 0 : options.enableDynamicRegistration) !== null && _a !== void 0 ? _a : true,
62
- logDataSourceRegistration: (_b = options === null || options === void 0 ? void 0 : options.logDataSourceRegistration) !== null && _b !== void 0 ? _b : true,
63
- defaultTimeout: (_c = options === null || options === void 0 ? void 0 : options.globalTransactionOptions) === null || _c === void 0 ? void 0 : _c.timeout,
64
- logTransactionDetails: (_e = (_d = options === null || options === void 0 ? void 0 : options.globalTransactionOptions) === null || _d === void 0 ? void 0 : _d.logDetails) !== null && _e !== void 0 ? _e : false,
65
- },
66
- },
67
- {
68
- provide: 'DATA_SOURCE_NAME',
69
- useValue: (options === null || options === void 0 ? void 0 : options.defaultDataSource) || 'default',
70
- },
71
- {
72
- provide: 'TRANSACTION_OPTIONS',
73
- useValue: (options === null || options === void 0 ? void 0 : options.globalTransactionOptions) || {},
74
- },
75
- data_source_registry_1.DataSourceRegistryService,
76
- {
77
- provide: 'DATA_SOURCE_REGISTRY_CONFIG',
78
- useValue: (options === null || options === void 0 ? void 0 : options.dataSourceRegistryConfig) || {
79
- enableHealthCheck: false,
80
- logLifecycleEvents: true,
81
- },
82
- },
83
- ];
84
- // 添加日志增强拦截器
85
- if (options === null || options === void 0 ? void 0 : options.useLoggingInterceptor) {
86
- providers.push(logging_transactional_interceptor_1.LoggingTransactionalInterceptor);
87
- }
88
- const exports = [
89
- transaction_context_service_1.TransactionContextService,
90
- transaction_manager_2.TransactionContextManager,
91
- transaction_manager_1.TransactionManager,
92
- data_source_registry_1.DataSourceRegistryService,
93
- transactional_decorator_1.TransactionalInterceptor,
94
- ];
95
- return {
96
- module: TransactionModule_1,
97
- providers,
98
- exports,
99
- global: true,
100
- };
101
- }
102
- static forFeature(dataSourceName, interceptorOptions) {
103
- const providers = [];
104
- if (interceptorOptions) {
105
- providers.push({
106
- provide: 'TRANSACTION_OPTIONS',
107
- useValue: interceptorOptions,
108
- });
109
- }
110
- if (dataSourceName) {
111
- providers.push({
112
- provide: 'DATA_SOURCE_NAME',
113
- useValue: dataSourceName,
114
- });
115
- }
116
- return {
117
- module: TransactionModule_1,
118
- providers,
119
- exports: [transaction_context_service_1.TransactionContextService],
120
- };
121
- }
122
- };
123
- exports.TransactionModule = TransactionModule;
124
- exports.TransactionModule = TransactionModule = TransactionModule_1 = __decorate([
125
- (0, common_1.Module)({})
126
- ], TransactionModule);
@@ -1,82 +0,0 @@
1
- import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
2
- import { Observable } from 'rxjs';
3
- import { TransactionManager, Propagation, IsolationLevel, TransactionTimeoutCallback } from './transaction-manager';
4
- /**
5
- * 事务元数据键
6
- */
7
- export declare const TRANSACTION_METADATA_KEY = "transactional";
8
- /**
9
- * 事务配置选项
10
- */
11
- export interface TransactionalOptions {
12
- propagation?: Propagation;
13
- isolation?: IsolationLevel;
14
- readOnly?: boolean;
15
- timeout?: number;
16
- name?: string;
17
- dataSource?: string;
18
- onTimeout?: TransactionTimeoutCallback;
19
- isolationLevel?: IsolationLevel;
20
- }
21
- /**
22
- * @Transactional 装饰器
23
- * 统一的事务装饰器,自动处理嵌套和数据源
24
- */
25
- export declare function Transactional(options?: TransactionalOptions): MethodDecorator;
26
- /**
27
- * 类级别事务装饰器
28
- */
29
- export declare function ClassTransactional(options?: TransactionalOptions): ClassDecorator;
30
- /**
31
- * 事务拦截器
32
- * 支持类级别和方法级别的事务管理
33
- */
34
- export declare class TransactionalInterceptor implements NestInterceptor {
35
- private readonly transactionManager;
36
- private readonly logger;
37
- constructor(transactionManager: TransactionManager);
38
- intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>>;
39
- private executeWithTransaction;
40
- private getTransactionOptions;
41
- }
42
- /**
43
- * 便捷装饰器工厂
44
- */
45
- export declare const Tx: {
46
- /**
47
- * 必须在事务中执行(默认)
48
- */
49
- Required: (dataSource?: string) => MethodDecorator;
50
- /**
51
- * 支持当前事务,非事务执行也行
52
- */
53
- Supports: (dataSource?: string) => MethodDecorator;
54
- /**
55
- * 必须在事务中执行,否则抛异常
56
- */
57
- Mandatory: (dataSource?: string) => MethodDecorator;
58
- /**
59
- * 创建新事务
60
- */
61
- RequiresNew: (dataSource?: string) => MethodDecorator;
62
- /**
63
- * 不支持事务,挂起当前事务
64
- */
65
- NotSupported: (dataSource?: string) => MethodDecorator;
66
- /**
67
- * 永不使用事务
68
- */
69
- Never: (dataSource?: string) => MethodDecorator;
70
- /**
71
- * 嵌套事务
72
- */
73
- Nested: (dataSource?: string) => MethodDecorator;
74
- /**
75
- * 只读事务
76
- */
77
- ReadOnly: (dataSource?: string) => MethodDecorator;
78
- };
79
- /**
80
- * 组合装饰器:使用拦截器
81
- */
82
- export declare function UseTransactional(options?: TransactionalOptions): ClassDecorator & MethodDecorator;