@nest-omni/core 4.1.3-14 → 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/decorators/field.decorators.d.ts +1 -1
- 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 -99
- package/transaction/base-service-transaction.js +0 -286
- 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 -15
- package/transaction/index.js +0 -68
- 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.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 -98
- package/transaction/transactional.decorator.d.ts +0 -82
- package/transaction/transactional.decorator.js +0 -319
- package/transaction/typeorm-module-wrapper.d.ts +0 -96
- package/transaction/typeorm-module-wrapper.js +0 -197
|
@@ -1,330 +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.addDataSources = exports.addDataSource = exports.withTransaction = exports.withDataSource = exports.isDataSourceAvailable = exports.getDefaultDataSource = exports.getDataSourceSafe = exports.getDataSource = exports.DataSourceUtil = void 0;
|
|
13
|
-
const transaction_context_service_1 = require("./transaction-context.service");
|
|
14
|
-
/**
|
|
15
|
-
* 全局数据源存储
|
|
16
|
-
* 用于在非请求上下文中存储和获取数据源
|
|
17
|
-
*/
|
|
18
|
-
class GlobalDataSourceStorage {
|
|
19
|
-
/**
|
|
20
|
-
* 注册数据源
|
|
21
|
-
*/
|
|
22
|
-
static register(name, dataSource) {
|
|
23
|
-
this.dataSources.set(name, dataSource);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* 批量注册数据源
|
|
27
|
-
*/
|
|
28
|
-
static registerAll(dataSources) {
|
|
29
|
-
for (const [name, dataSource] of dataSources.entries()) {
|
|
30
|
-
this.register(name, dataSource);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* 获取数据源
|
|
35
|
-
*/
|
|
36
|
-
static get(name) {
|
|
37
|
-
return this.dataSources.get(name);
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* 获取默认数据源
|
|
41
|
-
*/
|
|
42
|
-
static getDefault() {
|
|
43
|
-
return this.dataSources.get(this.defaultDataSourceName);
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* 设置默认数据源名称
|
|
47
|
-
*/
|
|
48
|
-
static setDefaultName(name) {
|
|
49
|
-
this.defaultDataSourceName = name;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* 检查数据源是否存在
|
|
53
|
-
*/
|
|
54
|
-
static has(name) {
|
|
55
|
-
return this.dataSources.has(name);
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* 获取所有已注册的数据源名称
|
|
59
|
-
*/
|
|
60
|
-
static getAllNames() {
|
|
61
|
-
return Array.from(this.dataSources.keys());
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* 移除数据源
|
|
65
|
-
*/
|
|
66
|
-
static remove(name) {
|
|
67
|
-
this.dataSources.delete(name);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* 清空所有数据源
|
|
71
|
-
*/
|
|
72
|
-
static clear() {
|
|
73
|
-
this.dataSources.clear();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
GlobalDataSourceStorage.dataSources = new Map();
|
|
77
|
-
GlobalDataSourceStorage.defaultDataSourceName = 'default';
|
|
78
|
-
/**
|
|
79
|
-
* 数据源工具类
|
|
80
|
-
* 提供静态方法在任意位置获取数据源
|
|
81
|
-
*/
|
|
82
|
-
class DataSourceUtil {
|
|
83
|
-
/**
|
|
84
|
-
* 获取数据源
|
|
85
|
-
* 按优先级从以下位置查找:
|
|
86
|
-
* 1. 当前请求上下文 (TransactionContextService)
|
|
87
|
-
* 2. 全局数据源存储 (GlobalDataSourceStorage)
|
|
88
|
-
*
|
|
89
|
-
* @param name 数据源名称,默认为 'default'
|
|
90
|
-
* @returns DataSource 实例
|
|
91
|
-
* @throws Error 如果数据源不存在
|
|
92
|
-
*/
|
|
93
|
-
static getDataSource(name = 'default') {
|
|
94
|
-
// 1. 尝试从请求上下文获取
|
|
95
|
-
try {
|
|
96
|
-
return transaction_context_service_1.TransactionContextService.getDataSource(name);
|
|
97
|
-
}
|
|
98
|
-
catch (_a) {
|
|
99
|
-
// 忽略错误,继续尝试其他方式
|
|
100
|
-
}
|
|
101
|
-
// 2. 从全局存储获取
|
|
102
|
-
const dataSource = GlobalDataSourceStorage.get(name);
|
|
103
|
-
if (dataSource) {
|
|
104
|
-
return dataSource;
|
|
105
|
-
}
|
|
106
|
-
throw new Error(`DataSource '${name}' not found. ` +
|
|
107
|
-
`Please ensure it is registered via TransactionModule.registerDataSource() or TypeOrmModule.`);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* 安全获取数据源,不存在时返回 null
|
|
111
|
-
*
|
|
112
|
-
* @param name 数据源名称
|
|
113
|
-
* @returns DataSource 实例或 null
|
|
114
|
-
*/
|
|
115
|
-
static getDataSourceSafe(name = 'default') {
|
|
116
|
-
try {
|
|
117
|
-
return this.getDataSource(name);
|
|
118
|
-
}
|
|
119
|
-
catch (_a) {
|
|
120
|
-
return null;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* 获取默认数据源
|
|
125
|
-
*/
|
|
126
|
-
static getDefaultDataSource() {
|
|
127
|
-
return this.getDataSource('default');
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* 安全获取默认数据源
|
|
131
|
-
*/
|
|
132
|
-
static getDefaultDataSourceSafe() {
|
|
133
|
-
return this.getDataSourceSafe('default');
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* 检查数据源是否可用
|
|
137
|
-
*
|
|
138
|
-
* @param name 数据源名称
|
|
139
|
-
* @returns 是否可用
|
|
140
|
-
*/
|
|
141
|
-
static isAvailable(name = 'default') {
|
|
142
|
-
var _a;
|
|
143
|
-
try {
|
|
144
|
-
const ds = this.getDataSource(name);
|
|
145
|
-
return (_a = ds === null || ds === void 0 ? void 0 : ds.isInitialized) !== null && _a !== void 0 ? _a : false;
|
|
146
|
-
}
|
|
147
|
-
catch (_b) {
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* 获取所有可用数据源名称
|
|
153
|
-
*/
|
|
154
|
-
static getAvailableDataSourceNames() {
|
|
155
|
-
const names = new Set();
|
|
156
|
-
// 从请求上下文获取
|
|
157
|
-
try {
|
|
158
|
-
const contextNames = transaction_context_service_1.TransactionContextService.getRegisteredDataSources();
|
|
159
|
-
contextNames.forEach((name) => names.add(name));
|
|
160
|
-
}
|
|
161
|
-
catch (_a) {
|
|
162
|
-
// 忽略错误
|
|
163
|
-
}
|
|
164
|
-
// 从全局存储获取
|
|
165
|
-
GlobalDataSourceStorage.getAllNames().forEach((name) => names.add(name));
|
|
166
|
-
return Array.from(names);
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* 批量获取数据源
|
|
170
|
-
*
|
|
171
|
-
* @param names 数据源名称数组
|
|
172
|
-
* @returns 数据源 Map
|
|
173
|
-
*/
|
|
174
|
-
static getDataSources(names) {
|
|
175
|
-
const result = new Map();
|
|
176
|
-
for (const name of names) {
|
|
177
|
-
try {
|
|
178
|
-
const ds = this.getDataSource(name);
|
|
179
|
-
if (ds) {
|
|
180
|
-
result.set(name, ds);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
catch (_a) {
|
|
184
|
-
// 跳过不存在的数据源
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return result;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* 获取所有已注册的数据源
|
|
191
|
-
*/
|
|
192
|
-
static getAllDataSources() {
|
|
193
|
-
const names = this.getAvailableDataSourceNames();
|
|
194
|
-
return this.getDataSources(names);
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* 手动注册数据源(用于非模块场景)
|
|
198
|
-
*
|
|
199
|
-
* @param name 数据源名称
|
|
200
|
-
* @param dataSource DataSource 实例
|
|
201
|
-
* @param isDefault 是否设为默认数据源
|
|
202
|
-
*/
|
|
203
|
-
static registerDataSource(name, dataSource, isDefault = false) {
|
|
204
|
-
GlobalDataSourceStorage.register(name, dataSource);
|
|
205
|
-
if (isDefault) {
|
|
206
|
-
GlobalDataSourceStorage.setDefaultName(name);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
/**
|
|
210
|
-
* 批量注册数据源
|
|
211
|
-
*
|
|
212
|
-
* @param dataSources 数据源 Map
|
|
213
|
-
* @param defaultName 默认数据源名称
|
|
214
|
-
*/
|
|
215
|
-
static registerDataSources(dataSources, defaultName) {
|
|
216
|
-
GlobalDataSourceStorage.registerAll(dataSources);
|
|
217
|
-
if (defaultName) {
|
|
218
|
-
GlobalDataSourceStorage.setDefaultName(defaultName);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
/**
|
|
222
|
-
* 使用数据源执行操作
|
|
223
|
-
*
|
|
224
|
-
* @param name 数据源名称
|
|
225
|
-
* @param operation 要执行的操作
|
|
226
|
-
* @returns 操作结果
|
|
227
|
-
*/
|
|
228
|
-
static withDataSource(name, operation) {
|
|
229
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
230
|
-
const dataSource = this.getDataSource(name);
|
|
231
|
-
return operation(dataSource);
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* 使用默认数据源执行操作
|
|
236
|
-
*
|
|
237
|
-
* @param operation 要执行的操作
|
|
238
|
-
* @returns 操作结果
|
|
239
|
-
*/
|
|
240
|
-
static withDefaultDataSource(operation) {
|
|
241
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
242
|
-
return this.withDataSource('default', operation);
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
/**
|
|
246
|
-
* 在事务中执行操作
|
|
247
|
-
*
|
|
248
|
-
* @param name 数据源名称
|
|
249
|
-
* @param operation 要执行的操作
|
|
250
|
-
* @returns 操作结果
|
|
251
|
-
*/
|
|
252
|
-
static withTransaction(name, operation) {
|
|
253
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
254
|
-
const dataSource = this.getDataSource(name);
|
|
255
|
-
return dataSource.transaction((entityManager) => __awaiter(this, void 0, void 0, function* () {
|
|
256
|
-
// 通过 EntityManager 获取 DataSource
|
|
257
|
-
const ds = entityManager.connection;
|
|
258
|
-
return operation(ds);
|
|
259
|
-
}));
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* 在默认数据源的事务中执行操作
|
|
264
|
-
*/
|
|
265
|
-
static withDefaultTransaction(operation) {
|
|
266
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
267
|
-
return this.withTransaction('default', operation);
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* 测试数据源连接
|
|
272
|
-
*
|
|
273
|
-
* @param name 数据源名称
|
|
274
|
-
* @returns 连接是否正常
|
|
275
|
-
*/
|
|
276
|
-
static testConnection() {
|
|
277
|
-
return __awaiter(this, arguments, void 0, function* (name = 'default') {
|
|
278
|
-
try {
|
|
279
|
-
const dataSource = this.getDataSource(name);
|
|
280
|
-
if (dataSource.isInitialized) {
|
|
281
|
-
yield dataSource.query('SELECT 1');
|
|
282
|
-
return true;
|
|
283
|
-
}
|
|
284
|
-
return false;
|
|
285
|
-
}
|
|
286
|
-
catch (_a) {
|
|
287
|
-
return false;
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
exports.DataSourceUtil = DataSourceUtil;
|
|
293
|
-
/**
|
|
294
|
-
* 便捷的别名
|
|
295
|
-
*/
|
|
296
|
-
exports.getDataSource = DataSourceUtil.getDataSource.bind(DataSourceUtil);
|
|
297
|
-
exports.getDataSourceSafe = DataSourceUtil.getDataSourceSafe.bind(DataSourceUtil);
|
|
298
|
-
exports.getDefaultDataSource = DataSourceUtil.getDefaultDataSource.bind(DataSourceUtil);
|
|
299
|
-
exports.isDataSourceAvailable = DataSourceUtil.isAvailable.bind(DataSourceUtil);
|
|
300
|
-
exports.withDataSource = DataSourceUtil.withDataSource.bind(DataSourceUtil);
|
|
301
|
-
exports.withTransaction = DataSourceUtil.withTransaction.bind(DataSourceUtil);
|
|
302
|
-
/**
|
|
303
|
-
* 添加数据源到全局注册表
|
|
304
|
-
* 用于在 TypeOrmModule.forRootAsync 的 useFactory 中手动注册数据源
|
|
305
|
-
*
|
|
306
|
-
* @param name 数据源名称
|
|
307
|
-
* @param dataSource DataSource 实例
|
|
308
|
-
* @param isDefault 是否设为默认数据源
|
|
309
|
-
*
|
|
310
|
-
* @example
|
|
311
|
-
* ```typescript
|
|
312
|
-
* TypeOrmModule.forRootAsync({
|
|
313
|
-
* inject: [ApiConfigService],
|
|
314
|
-
* useFactory: (config: ApiConfigService) => {
|
|
315
|
-
* const dsConfig = config.typeormConfig;
|
|
316
|
-
* // 在这里注册,确保数据源初始化后立即可用
|
|
317
|
-
* addDataSource('default', dataSource, true);
|
|
318
|
-
* return dsConfig;
|
|
319
|
-
* },
|
|
320
|
-
* }),
|
|
321
|
-
* ```
|
|
322
|
-
*/
|
|
323
|
-
exports.addDataSource = DataSourceUtil.registerDataSource.bind(DataSourceUtil);
|
|
324
|
-
/**
|
|
325
|
-
* 批量添加数据源
|
|
326
|
-
*
|
|
327
|
-
* @param dataSources 数据源 Map
|
|
328
|
-
* @param defaultName 默认数据源名称
|
|
329
|
-
*/
|
|
330
|
-
exports.addDataSources = DataSourceUtil.registerDataSources.bind(DataSourceUtil);
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { DataSource, QueryRunner } from 'typeorm';
|
|
2
|
-
/**
|
|
3
|
-
* 数据库类型
|
|
4
|
-
*/
|
|
5
|
-
export type DatabaseType = 'mysql' | 'mariadb' | 'postgres' | 'cockroachdb' | 'sqlite' | 'better-sqlite3' | 'mssql' | 'oracle' | 'mongodb' | 'cordova' | 'react-native' | 'nativescript' | 'sqljs' | 'expo' | 'capacitor';
|
|
6
|
-
/**
|
|
7
|
-
* 数据库适配器
|
|
8
|
-
* 处理不同数据库的 SQL 语法差异
|
|
9
|
-
*/
|
|
10
|
-
export declare class DatabaseAdapter {
|
|
11
|
-
/**
|
|
12
|
-
* 获取数据库类型
|
|
13
|
-
*/
|
|
14
|
-
static getDatabaseType(dataSource: DataSource): DatabaseType;
|
|
15
|
-
/**
|
|
16
|
-
* 检查数据库是否支持 SAVEPOINT
|
|
17
|
-
*/
|
|
18
|
-
static supportsSavepoint(dataSource: DataSource): boolean;
|
|
19
|
-
/**
|
|
20
|
-
* 创建保存点
|
|
21
|
-
*/
|
|
22
|
-
static createSavepoint(queryRunner: QueryRunner, savepointName: string): Promise<void>;
|
|
23
|
-
/**
|
|
24
|
-
* 回滚到保存点
|
|
25
|
-
*/
|
|
26
|
-
static rollbackToSavepoint(queryRunner: QueryRunner, savepointName: string): Promise<void>;
|
|
27
|
-
/**
|
|
28
|
-
* 释放保存点
|
|
29
|
-
*/
|
|
30
|
-
static releaseSavepoint(queryRunner: QueryRunner, savepointName: string): Promise<void>;
|
|
31
|
-
/**
|
|
32
|
-
* 执行健康检查查询
|
|
33
|
-
*/
|
|
34
|
-
static healthCheck(dataSource: DataSource): Promise<boolean>;
|
|
35
|
-
/**
|
|
36
|
-
* 获取数据库特定的信息
|
|
37
|
-
*/
|
|
38
|
-
static getDatabaseInfo(dataSource: DataSource): {
|
|
39
|
-
type: DatabaseType;
|
|
40
|
-
supportsSavepoint: boolean;
|
|
41
|
-
supportsNestedTransactions: boolean;
|
|
42
|
-
requiresReleaseSavepoint: boolean;
|
|
43
|
-
};
|
|
44
|
-
}
|
|
@@ -1,240 +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.DatabaseAdapter = void 0;
|
|
13
|
-
/**
|
|
14
|
-
* 数据库适配器
|
|
15
|
-
* 处理不同数据库的 SQL 语法差异
|
|
16
|
-
*/
|
|
17
|
-
class DatabaseAdapter {
|
|
18
|
-
/**
|
|
19
|
-
* 获取数据库类型
|
|
20
|
-
*/
|
|
21
|
-
static getDatabaseType(dataSource) {
|
|
22
|
-
return dataSource.options.type;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* 检查数据库是否支持 SAVEPOINT
|
|
26
|
-
*/
|
|
27
|
-
static supportsSavepoint(dataSource) {
|
|
28
|
-
const type = this.getDatabaseType(dataSource);
|
|
29
|
-
// MongoDB 不支持传统事务和 SAVEPOINT
|
|
30
|
-
if (type === 'mongodb') {
|
|
31
|
-
return false;
|
|
32
|
-
}
|
|
33
|
-
// 其他关系型数据库都支持 SAVEPOINT
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* 创建保存点
|
|
38
|
-
*/
|
|
39
|
-
static createSavepoint(queryRunner, savepointName) {
|
|
40
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
41
|
-
// 检查 queryRunner.connection 是否存在
|
|
42
|
-
if (!queryRunner.connection) {
|
|
43
|
-
// 如果没有 connection,使用标准 SQL 语法(最常见的情况)
|
|
44
|
-
yield queryRunner.query(`SAVEPOINT ${savepointName}`);
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
const type = this.getDatabaseType(queryRunner.connection);
|
|
48
|
-
switch (type) {
|
|
49
|
-
case 'mysql':
|
|
50
|
-
case 'mariadb':
|
|
51
|
-
case 'postgres':
|
|
52
|
-
case 'cockroachdb':
|
|
53
|
-
case 'sqlite':
|
|
54
|
-
case 'better-sqlite3':
|
|
55
|
-
case 'sqljs':
|
|
56
|
-
case 'cordova':
|
|
57
|
-
case 'react-native':
|
|
58
|
-
case 'nativescript':
|
|
59
|
-
case 'expo':
|
|
60
|
-
case 'capacitor':
|
|
61
|
-
// 标准 SQL 语法
|
|
62
|
-
yield queryRunner.query(`SAVEPOINT ${savepointName}`);
|
|
63
|
-
break;
|
|
64
|
-
case 'mssql':
|
|
65
|
-
// SQL Server 使用 SAVE TRANSACTION
|
|
66
|
-
yield queryRunner.query(`SAVE TRANSACTION ${savepointName}`);
|
|
67
|
-
break;
|
|
68
|
-
case 'oracle':
|
|
69
|
-
// Oracle 使用标准 SAVEPOINT 语法
|
|
70
|
-
yield queryRunner.query(`SAVEPOINT ${savepointName}`);
|
|
71
|
-
break;
|
|
72
|
-
case 'mongodb':
|
|
73
|
-
throw new Error('MongoDB does not support SAVEPOINT. Use transactions instead.');
|
|
74
|
-
default:
|
|
75
|
-
// 对于未知的数据库类型,尝试使用标准 SQL 语法
|
|
76
|
-
yield queryRunner.query(`SAVEPOINT ${savepointName}`);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* 回滚到保存点
|
|
82
|
-
*/
|
|
83
|
-
static rollbackToSavepoint(queryRunner, savepointName) {
|
|
84
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
-
// 检查 queryRunner.connection 是否存在
|
|
86
|
-
if (!queryRunner.connection) {
|
|
87
|
-
// 如果没有 connection,使用标准 SQL 语法(最常见的情况)
|
|
88
|
-
yield queryRunner.query(`ROLLBACK TO SAVEPOINT ${savepointName}`);
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
const type = this.getDatabaseType(queryRunner.connection);
|
|
92
|
-
switch (type) {
|
|
93
|
-
case 'mysql':
|
|
94
|
-
case 'mariadb':
|
|
95
|
-
case 'postgres':
|
|
96
|
-
case 'cockroachdb':
|
|
97
|
-
case 'sqlite':
|
|
98
|
-
case 'better-sqlite3':
|
|
99
|
-
case 'sqljs':
|
|
100
|
-
case 'cordova':
|
|
101
|
-
case 'react-native':
|
|
102
|
-
case 'nativescript':
|
|
103
|
-
case 'expo':
|
|
104
|
-
case 'capacitor':
|
|
105
|
-
case 'oracle':
|
|
106
|
-
// 标准 SQL 语法
|
|
107
|
-
yield queryRunner.query(`ROLLBACK TO SAVEPOINT ${savepointName}`);
|
|
108
|
-
break;
|
|
109
|
-
case 'mssql':
|
|
110
|
-
// SQL Server 使用 ROLLBACK TRANSACTION
|
|
111
|
-
yield queryRunner.query(`ROLLBACK TRANSACTION ${savepointName}`);
|
|
112
|
-
break;
|
|
113
|
-
case 'mongodb':
|
|
114
|
-
throw new Error('MongoDB does not support SAVEPOINT. Use transactions instead.');
|
|
115
|
-
default:
|
|
116
|
-
// 对于未知的数据库类型,尝试使用标准 SQL 语法
|
|
117
|
-
yield queryRunner.query(`ROLLBACK TO SAVEPOINT ${savepointName}`);
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* 释放保存点
|
|
123
|
-
*/
|
|
124
|
-
static releaseSavepoint(queryRunner, savepointName) {
|
|
125
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
-
// 检查 queryRunner.connection 是否存在
|
|
127
|
-
if (!queryRunner.connection) {
|
|
128
|
-
// 如果没有 connection,尝试使用标准 SQL 语法
|
|
129
|
-
try {
|
|
130
|
-
yield queryRunner.query(`RELEASE SAVEPOINT ${savepointName}`);
|
|
131
|
-
}
|
|
132
|
-
catch (error) {
|
|
133
|
-
// 静默失败,因为某些数据库不支持 RELEASE
|
|
134
|
-
}
|
|
135
|
-
return;
|
|
136
|
-
}
|
|
137
|
-
const type = this.getDatabaseType(queryRunner.connection);
|
|
138
|
-
switch (type) {
|
|
139
|
-
case 'mysql':
|
|
140
|
-
case 'mariadb':
|
|
141
|
-
case 'postgres':
|
|
142
|
-
case 'cockroachdb':
|
|
143
|
-
case 'sqlite':
|
|
144
|
-
case 'better-sqlite3':
|
|
145
|
-
case 'sqljs':
|
|
146
|
-
case 'cordova':
|
|
147
|
-
case 'react-native':
|
|
148
|
-
case 'nativescript':
|
|
149
|
-
case 'expo':
|
|
150
|
-
case 'capacitor':
|
|
151
|
-
// 标准 SQL 语法
|
|
152
|
-
yield queryRunner.query(`RELEASE SAVEPOINT ${savepointName}`);
|
|
153
|
-
break;
|
|
154
|
-
case 'oracle':
|
|
155
|
-
// Oracle 不支持 RELEASE SAVEPOINT,保存点会在提交时自动释放
|
|
156
|
-
// 不执行任何操作
|
|
157
|
-
break;
|
|
158
|
-
case 'mssql':
|
|
159
|
-
// SQL Server 不支持 RELEASE,保存点会在提交或回滚时自动清理
|
|
160
|
-
// 不执行任何操作
|
|
161
|
-
break;
|
|
162
|
-
case 'mongodb':
|
|
163
|
-
// MongoDB 不支持
|
|
164
|
-
break;
|
|
165
|
-
default:
|
|
166
|
-
// 对于未知的数据库类型,尝试使用标准 SQL 语法
|
|
167
|
-
// 如果失败也不抛出错误,因为某些数据库不需要显式释放
|
|
168
|
-
try {
|
|
169
|
-
yield queryRunner.query(`RELEASE SAVEPOINT ${savepointName}`);
|
|
170
|
-
}
|
|
171
|
-
catch (error) {
|
|
172
|
-
// 静默失败,因为某些数据库不支持 RELEASE
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* 执行健康检查查询
|
|
179
|
-
*/
|
|
180
|
-
static healthCheck(dataSource) {
|
|
181
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
-
const type = this.getDatabaseType(dataSource);
|
|
183
|
-
try {
|
|
184
|
-
switch (type) {
|
|
185
|
-
case 'mysql':
|
|
186
|
-
case 'mariadb':
|
|
187
|
-
yield dataSource.query('SELECT 1');
|
|
188
|
-
break;
|
|
189
|
-
case 'postgres':
|
|
190
|
-
case 'cockroachdb':
|
|
191
|
-
yield dataSource.query('SELECT 1');
|
|
192
|
-
break;
|
|
193
|
-
case 'mssql':
|
|
194
|
-
yield dataSource.query('SELECT 1');
|
|
195
|
-
break;
|
|
196
|
-
case 'oracle':
|
|
197
|
-
yield dataSource.query('SELECT 1 FROM DUAL');
|
|
198
|
-
break;
|
|
199
|
-
case 'sqlite':
|
|
200
|
-
case 'better-sqlite3':
|
|
201
|
-
case 'sqljs':
|
|
202
|
-
case 'cordova':
|
|
203
|
-
case 'react-native':
|
|
204
|
-
case 'nativescript':
|
|
205
|
-
case 'expo':
|
|
206
|
-
case 'capacitor':
|
|
207
|
-
yield dataSource.query('SELECT 1');
|
|
208
|
-
break;
|
|
209
|
-
case 'mongodb':
|
|
210
|
-
// MongoDB 使用不同的健康检查方式
|
|
211
|
-
yield dataSource.query('{ ping: 1 }');
|
|
212
|
-
break;
|
|
213
|
-
default:
|
|
214
|
-
// 默认使用 SELECT 1
|
|
215
|
-
yield dataSource.query('SELECT 1');
|
|
216
|
-
}
|
|
217
|
-
return true;
|
|
218
|
-
}
|
|
219
|
-
catch (error) {
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
/**
|
|
225
|
-
* 获取数据库特定的信息
|
|
226
|
-
*/
|
|
227
|
-
static getDatabaseInfo(dataSource) {
|
|
228
|
-
const type = this.getDatabaseType(dataSource);
|
|
229
|
-
const supportsSavepoint = this.supportsSavepoint(dataSource);
|
|
230
|
-
// 检查是否需要显式释放 SAVEPOINT
|
|
231
|
-
const requiresReleaseSavepoint = !['oracle', 'mssql', 'mongodb'].includes(type);
|
|
232
|
-
return {
|
|
233
|
-
type,
|
|
234
|
-
supportsSavepoint,
|
|
235
|
-
supportsNestedTransactions: supportsSavepoint,
|
|
236
|
-
requiresReleaseSavepoint,
|
|
237
|
-
};
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
exports.DatabaseAdapter = DatabaseAdapter;
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
/**
|
|
3
|
-
* Entity 数据源装饰器
|
|
4
|
-
* 用于指定 Entity 应该使用哪个数据源
|
|
5
|
-
*/
|
|
6
|
-
export declare const ENTITY_DATASOURCE_METADATA_KEY = "entity:datasource";
|
|
7
|
-
/**
|
|
8
|
-
* 数据源装饰器 - 为 Entity 指定数据源
|
|
9
|
-
*
|
|
10
|
-
* @param dataSourceName 数据源名称
|
|
11
|
-
*
|
|
12
|
-
* 使用示例:
|
|
13
|
-
* ```typescript
|
|
14
|
-
* @Entity('users')
|
|
15
|
-
* @DataSource('user_db')
|
|
16
|
-
* export class User {
|
|
17
|
-
* @PrimaryGeneratedColumn()
|
|
18
|
-
* id: number;
|
|
19
|
-
*
|
|
20
|
-
* @Column()
|
|
21
|
-
* name: string;
|
|
22
|
-
* }
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export declare function DataSource(dataSourceName: string): ClassDecorator;
|
|
26
|
-
/**
|
|
27
|
-
* 获取 Entity 的数据源名称
|
|
28
|
-
* @param entityClass Entity 类或构造函数
|
|
29
|
-
* @returns 数据源名称,如果未指定则返回 null
|
|
30
|
-
*/
|
|
31
|
-
export declare function getEntityDataSource(entityClass: any): string | null;
|
|
32
|
-
/**
|
|
33
|
-
* 检查 Entity 是否指定了数据源
|
|
34
|
-
* @param entityClass Entity 类或构造函数
|
|
35
|
-
* @returns 是否指定了数据源
|
|
36
|
-
*/
|
|
37
|
-
export declare function hasEntityDataSource(entityClass: any): boolean;
|
|
38
|
-
/**
|
|
39
|
-
* 带数据源的 Entity 基类
|
|
40
|
-
* 继承这个基类的 Entity 可以通过实现 useDataSource 方法动态指定数据源
|
|
41
|
-
*/
|
|
42
|
-
export declare abstract class BaseEntityWithDataSource {
|
|
43
|
-
/**
|
|
44
|
-
* 子类可以重写此方法来动态指定数据源
|
|
45
|
-
* @returns 数据源名称
|
|
46
|
-
*/
|
|
47
|
-
static useDataSource?(): string;
|
|
48
|
-
/**
|
|
49
|
-
* 实例方法版本,用于动态获取数据源
|
|
50
|
-
* @returns 数据源名称
|
|
51
|
-
*/
|
|
52
|
-
getDataSource?(): string;
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* 动态数据源装饰器
|
|
56
|
-
* 标记 Entity 使用动态数据源(通过 useDataSource 方法指定)
|
|
57
|
-
*/
|
|
58
|
-
export declare function DynamicDataSource(): ClassDecorator;
|
|
59
|
-
/**
|
|
60
|
-
* 检查 Entity 是否使用动态数据源
|
|
61
|
-
*/
|
|
62
|
-
export declare function isDynamicDataSourceEntity(entityClass: any): boolean;
|