fdb2 1.0.7 → 1.0.9
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/.dockerignore +21 -21
- package/.editorconfig +11 -11
- package/.eslintrc.cjs +14 -14
- package/.eslintrc.json +7 -7
- package/.prettierrc.js +3 -3
- package/.tpl.env +21 -21
- package/.vscodeignore +45 -45
- package/README.md +312 -312
- package/bin/build.sh +28 -28
- package/bin/deploy.sh +8 -8
- package/bin/dev.sh +10 -10
- package/bin/docker/dev-docker-compose.yml +43 -43
- package/bin/docker/dev.Dockerfile +24 -24
- package/bin/docker/prod-docker-compose.yml +17 -17
- package/bin/docker/prod.Dockerfile +29 -29
- package/bin/fdb2.js +220 -220
- package/dist/package.json +29 -29
- package/dist/pnpm-lock.yaml +1042 -354
- package/dist/public/explorer.css +1464 -1437
- package/dist/public/explorer.js +764 -226
- package/dist/public/index.css +1026 -1026
- package/dist/public/index.js +15 -9
- package/dist/public/layout.css +221 -221
- package/dist/public/layout.js +1 -1
- package/dist/public/vue.js +8 -2
- package/dist/scripts/preinstall.js +112 -112
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +8 -0
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.ts +680 -671
- package/dist/server/model/connection.entity.ts +65 -65
- package/dist/server/model/database.entity.ts +245 -245
- package/dist/server/service/connection.service.d.ts +6 -1
- package/dist/server/service/connection.service.d.ts.map +1 -1
- package/dist/server/service/connection.service.js +15 -0
- package/dist/server/service/connection.service.js.map +1 -1
- package/dist/server/service/connection.service.ts +356 -341
- package/dist/server/service/database/base.service.d.ts +27 -0
- package/dist/server/service/database/base.service.d.ts.map +1 -1
- package/dist/server/service/database/base.service.js +17 -0
- package/dist/server/service/database/base.service.js.map +1 -1
- package/dist/server/service/database/base.service.ts +406 -367
- package/dist/server/service/database/cockroachdb.service.d.ts +16 -0
- package/dist/server/service/database/cockroachdb.service.d.ts.map +1 -1
- package/dist/server/service/database/cockroachdb.service.js +220 -154
- package/dist/server/service/database/cockroachdb.service.js.map +1 -1
- package/dist/server/service/database/cockroachdb.service.ts +871 -782
- package/dist/server/service/database/database.service.d.ts +4 -0
- package/dist/server/service/database/database.service.d.ts.map +1 -1
- package/dist/server/service/database/database.service.js +123 -0
- package/dist/server/service/database/database.service.js.map +1 -1
- package/dist/server/service/database/database.service.ts +775 -638
- package/dist/server/service/database/index.ts +6 -6
- package/dist/server/service/database/mongodb.service.d.ts +16 -0
- package/dist/server/service/database/mongodb.service.d.ts.map +1 -1
- package/dist/server/service/database/mongodb.service.js +35 -0
- package/dist/server/service/database/mongodb.service.js.map +1 -1
- package/dist/server/service/database/mongodb.service.ts +39 -1
- package/dist/server/service/database/mssql.service.d.ts +16 -0
- package/dist/server/service/database/mssql.service.d.ts.map +1 -1
- package/dist/server/service/database/mssql.service.js +168 -96
- package/dist/server/service/database/mssql.service.js.map +1 -1
- package/dist/server/service/database/mssql.service.ts +931 -840
- package/dist/server/service/database/mysql.service.d.ts +16 -0
- package/dist/server/service/database/mysql.service.d.ts.map +1 -1
- package/dist/server/service/database/mysql.service.js +189 -80
- package/dist/server/service/database/mysql.service.js.map +1 -1
- package/dist/server/service/database/mysql.service.ts +1025 -890
- package/dist/server/service/database/oracle.service.d.ts +16 -0
- package/dist/server/service/database/oracle.service.d.ts.map +1 -1
- package/dist/server/service/database/oracle.service.js +182 -120
- package/dist/server/service/database/oracle.service.js.map +1 -1
- package/dist/server/service/database/oracle.service.ts +1035 -959
- package/dist/server/service/database/postgres.service.d.ts +16 -0
- package/dist/server/service/database/postgres.service.d.ts.map +1 -1
- package/dist/server/service/database/postgres.service.js +154 -88
- package/dist/server/service/database/postgres.service.js.map +1 -1
- package/dist/server/service/database/postgres.service.ts +960 -871
- package/dist/server/service/database/sap.service.d.ts +16 -0
- package/dist/server/service/database/sap.service.d.ts.map +1 -1
- package/dist/server/service/database/sap.service.js +66 -0
- package/dist/server/service/database/sap.service.js.map +1 -1
- package/dist/server/service/database/sap.service.ts +89 -0
- package/dist/server/service/database/sqlite.service.d.ts +16 -0
- package/dist/server/service/database/sqlite.service.d.ts.map +1 -1
- package/dist/server/service/database/sqlite.service.js +77 -18
- package/dist/server/service/database/sqlite.service.js.map +1 -1
- package/dist/server/service/database/sqlite.service.ts +787 -708
- package/dist/server/service/session.service.ts +158 -158
- package/dist/view/index.html +38 -38
- package/env.d.ts +1 -1
- package/package.json +1 -1
- package/packages/vscode/.vscodeignore +44 -44
- package/packages/vscode/README.md +62 -62
- package/packages/vscode/out/database-services/cockroachdb.service.js +154 -154
- package/packages/vscode/out/database-services/mssql.service.js +96 -96
- package/packages/vscode/out/database-services/mysql.service.js +80 -80
- package/packages/vscode/out/database-services/oracle.service.js +120 -120
- package/packages/vscode/out/database-services/postgres.service.js +88 -88
- package/packages/vscode/out/database-services/sqlite.service.js +18 -18
- package/packages/vscode/out/provider/WebViewProvider.js +32 -32
- package/packages/vscode/package.json +142 -142
- package/packages/vscode/resources/icon.svg +5 -5
- package/packages/vscode/resources/webview/connection.css +41 -41
- package/packages/vscode/resources/webview/database.css +163 -163
- package/packages/vscode/resources/webview/index.html +9 -9
- package/packages/vscode/resources/webview/modules/header.tpl +13 -13
- package/packages/vscode/resources/webview/modules/initial_state.tpl +54 -54
- package/packages/vscode/resources/webview/query.css +104 -104
- package/packages/vscode/src/database-services/base.service.ts +362 -362
- package/packages/vscode/src/database-services/cockroachdb.service.ts +659 -659
- package/packages/vscode/src/database-services/connection.service.ts +340 -340
- package/packages/vscode/src/database-services/database.service.ts +629 -629
- package/packages/vscode/src/database-services/index.ts +6 -6
- package/packages/vscode/src/database-services/model/connection.entity.ts +65 -65
- package/packages/vscode/src/database-services/model/database.entity.ts +245 -245
- package/packages/vscode/src/database-services/mssql.service.ts +722 -722
- package/packages/vscode/src/database-services/mysql.service.ts +760 -760
- package/packages/vscode/src/database-services/oracle.service.ts +831 -831
- package/packages/vscode/src/database-services/postgres.service.ts +740 -740
- package/packages/vscode/src/database-services/sqlite.service.ts +558 -558
- package/packages/vscode/src/extension.ts +76 -76
- package/packages/vscode/src/provider/DatabaseTreeProvider.ts +167 -167
- package/packages/vscode/src/provider/WebViewProvider.ts +277 -277
- package/packages/vscode/src/service/DatabaseServiceBridge.ts +414 -414
- package/packages/vscode/src/typings/connection.ts +90 -90
- package/packages/vscode/tsconfig.json +21 -21
- package/public/index.html +9 -9
- package/public/modules/header.tpl +13 -13
- package/public/modules/initial_state.tpl +54 -54
- package/scripts/preinstall.js +112 -112
- package/server/index.ts +680 -671
- package/server/model/connection.entity.ts +65 -65
- package/server/model/database.entity.ts +245 -245
- package/server/service/connection.service.ts +356 -341
- package/server/service/database/base.service.ts +406 -367
- package/server/service/database/cockroachdb.service.ts +871 -782
- package/server/service/database/database.service.ts +775 -638
- package/server/service/database/index.ts +6 -6
- package/server/service/database/mongodb.service.ts +39 -1
- package/server/service/database/mssql.service.ts +931 -840
- package/server/service/database/mysql.service.ts +1025 -890
- package/server/service/database/oracle.service.ts +1035 -959
- package/server/service/database/postgres.service.ts +960 -871
- package/server/service/database/sap.service.ts +89 -0
- package/server/service/database/sqlite.service.ts +787 -708
- package/server/service/session.service.ts +158 -158
- package/server/tsconfig.json +20 -20
- package/server.js +149 -149
- package/server.pid +1 -0
- package/src/adapter/ajax.ts +135 -135
- package/src/assets/base.css +1 -1
- package/src/assets/database.css +949 -949
- package/src/assets/images/svg/illustrations/illustration-1.svg +1 -1
- package/src/assets/images/svg/illustrations/illustration-2.svg +2 -2
- package/src/assets/images/svg/illustrations/illustration-3.svg +50 -50
- package/src/assets/images/svg/illustrations/illustration-4.svg +1 -1
- package/src/assets/images/svg/illustrations/illustration-5.svg +73 -73
- package/src/assets/images/svg/illustrations/illustration-6.svg +89 -89
- package/src/assets/images/svg/illustrations/illustration-7.svg +39 -39
- package/src/assets/images/svg/separators/curve-2.svg +3 -3
- package/src/assets/images/svg/separators/curve.svg +3 -3
- package/src/assets/images/svg/separators/line.svg +3 -3
- package/src/assets/logo.svg +73 -73
- package/src/assets/main.css +1 -1
- package/src/base/config.ts +20 -20
- package/src/base/detect.ts +134 -134
- package/src/base/entity.ts +92 -92
- package/src/base/eventBus.ts +36 -36
- package/src/components/connection-editor/index.vue +588 -588
- package/src/components/dataGrid/index.vue +104 -104
- package/src/components/dataGrid/pagination.vue +105 -105
- package/src/components/loading/index.vue +42 -42
- package/src/components/modal/index.ts +180 -180
- package/src/components/modal/index.vue +560 -560
- package/src/components/toast/index.ts +43 -43
- package/src/components/toast/toast.vue +57 -57
- package/src/components/user/name.vue +103 -103
- package/src/components/user/selector.vue +416 -416
- package/src/domain/SysConfig.ts +74 -74
- package/src/platform/App.vue +7 -7
- package/src/platform/database/components/connection-detail.vue +1153 -1154
- package/src/platform/database/components/data-editor.vue +477 -477
- package/src/platform/database/components/database-detail.vue +1173 -1172
- package/src/platform/database/components/database-monitor.vue +1085 -1085
- package/src/platform/database/components/db-tools.vue +1264 -816
- package/src/platform/database/components/query-history.vue +1348 -1348
- package/src/platform/database/components/sql-executor.vue +737 -737
- package/src/platform/database/components/sql-query-editor.vue +1045 -1045
- package/src/platform/database/components/table-detail.vue +1375 -1376
- package/src/platform/database/components/table-editor.vue +916 -916
- package/src/platform/database/explorer.vue +1839 -1839
- package/src/platform/database/index.vue +1192 -1192
- package/src/platform/database/layout.vue +366 -366
- package/src/platform/database/router.ts +36 -36
- package/src/platform/database/styles/common.scss +601 -601
- package/src/platform/database/types/common.ts +444 -444
- package/src/platform/database/utils/export.ts +231 -231
- package/src/platform/database/utils/helpers.ts +436 -436
- package/src/platform/index.ts +32 -32
- package/src/platform/router.ts +40 -40
- package/src/platform/vscode/bridge.ts +121 -121
- package/src/platform/vscode/components/ConnectionPanel.vue +272 -272
- package/src/platform/vscode/components/DatabasePanel.vue +532 -532
- package/src/platform/vscode/components/QueryPanel.vue +371 -371
- package/src/platform/vscode/entry/connection.ts +13 -13
- package/src/platform/vscode/entry/database.ts +13 -13
- package/src/platform/vscode/entry/query.ts +13 -13
- package/src/platform/vscode/index.ts +5 -5
- package/src/service/base.ts +133 -127
- package/src/service/database.ts +505 -495
- package/src/service/login.ts +120 -120
- package/src/shims-vue.d.ts +6 -6
- package/src/stores/connection.ts +266 -266
- package/src/stores/session.ts +87 -87
- package/src/typings/database-types.ts +412 -412
- package/src/typings/database.ts +363 -363
- package/src/typings/global.d.ts +58 -58
- package/src/typings/pinia.d.ts +7 -7
- package/src/utils/clipboard.ts +29 -29
- package/src/utils/database-types.ts +242 -242
- package/src/utils/modal.ts +123 -123
- package/src/utils/request.ts +55 -55
- package/src/utils/sleep.ts +3 -3
- package/src/utils/toast.ts +73 -73
- package/src/utils/util.ts +171 -171
- package/src/utils/xlsx.ts +228 -228
- package/tsconfig.json +33 -33
- package/view/index.html +9 -9
- package/view/modules/header.tpl +13 -13
- package/view/modules/initial_state.tpl +19 -19
- package/vite.config.ts +424 -424
- package/vite.config.vscode.ts +47 -47
- package/server/backups/db_ai_breakout_2026-03-11T08-38-48-677Z.sql +0 -0
|
@@ -1,341 +1,341 @@
|
|
|
1
|
-
import * as fs from 'fs';
|
|
2
|
-
import * as path from 'path';
|
|
3
|
-
import * as os from 'os';
|
|
4
|
-
import { ConnectionEntity } from './model/connection.entity';
|
|
5
|
-
import { DataSource, type DataSourceOptions } from 'typeorm';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 数据库连接管理服务
|
|
9
|
-
* 负责管理数据库连接配置和连接实例
|
|
10
|
-
*/
|
|
11
|
-
export class ConnectionService {
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* 连接配置文件路径
|
|
15
|
-
* 优先使用环境变量 DB_TOOL_DATA_DIR,否则使用用户主目录下的 .db-tool 目录
|
|
16
|
-
*/
|
|
17
|
-
private readonly configPath: string;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 活跃的数据库连接实例
|
|
21
|
-
*/
|
|
22
|
-
private activeConnections: Map<string, DataSource> = new Map();
|
|
23
|
-
|
|
24
|
-
constructor() {
|
|
25
|
-
const dataDir = process.env.DB_TOOL_DATA_DIR || path.join(os.homedir(), '.fdb2');
|
|
26
|
-
this.configPath = path.join(dataDir, 'connections.json');
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 初始化服务,创建配置目录
|
|
31
|
-
*/
|
|
32
|
-
async init() {
|
|
33
|
-
const configDir = path.dirname(this.configPath);
|
|
34
|
-
if (!fs.existsSync(configDir)) {
|
|
35
|
-
fs.mkdirSync(configDir, { recursive: true });
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* 获取所有数据库连接配置
|
|
41
|
-
*/
|
|
42
|
-
async getAllConnections(): Promise<ConnectionEntity[]> {
|
|
43
|
-
try {
|
|
44
|
-
if (!fs.existsSync(this.configPath)) {
|
|
45
|
-
return [];
|
|
46
|
-
}
|
|
47
|
-
const data = fs.readFileSync(this.configPath, 'utf8');
|
|
48
|
-
return JSON.parse(data) as ConnectionEntity[];
|
|
49
|
-
} catch (error) {
|
|
50
|
-
console.error('读取连接配置失败:', error);
|
|
51
|
-
return [];
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* 根据ID获取数据库连接配置
|
|
57
|
-
*/
|
|
58
|
-
async getConnectionById(id: string): Promise<ConnectionEntity | null> {
|
|
59
|
-
const connections = await this.getAllConnections();
|
|
60
|
-
return connections.find(conn => conn.id === id) || null;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 添加数据库连接配置
|
|
65
|
-
*/
|
|
66
|
-
async addConnection(connection: ConnectionEntity): Promise<ConnectionEntity> {
|
|
67
|
-
const connections = await this.getAllConnections();
|
|
68
|
-
|
|
69
|
-
// 检查名称是否重复
|
|
70
|
-
if (connections.find(conn => conn.name === connection.name)) {
|
|
71
|
-
throw new Error('连接名称已存在');
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// 生成ID并设置时间戳
|
|
75
|
-
connection.id = this.generateId();
|
|
76
|
-
connection.createdAt = new Date();
|
|
77
|
-
connection.updatedAt = new Date();
|
|
78
|
-
connection.enabled = connection.enabled !== undefined ? connection.enabled : true;
|
|
79
|
-
|
|
80
|
-
connections.push(connection);
|
|
81
|
-
await this.saveConnections(connections);
|
|
82
|
-
|
|
83
|
-
return connection;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 更新数据库连接配置
|
|
88
|
-
*/
|
|
89
|
-
async updateConnection(id: string, updates: Partial<ConnectionEntity>): Promise<ConnectionEntity> {
|
|
90
|
-
const connections = await this.getAllConnections();
|
|
91
|
-
const index = connections.findIndex(conn => conn.id === id);
|
|
92
|
-
|
|
93
|
-
if (index === -1) {
|
|
94
|
-
throw new Error('连接配置不存在');
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// 检查名称重复
|
|
98
|
-
if (updates.name && connections.find((conn, idx) => conn.name === updates.name && idx !== index)) {
|
|
99
|
-
throw new Error('连接名称已存在');
|
|
100
|
-
}
|
|
101
|
-
// @ts-ignore
|
|
102
|
-
connections[index] = { ...connections[index], ...updates, updatedAt: new Date() };
|
|
103
|
-
await this.saveConnections(connections);
|
|
104
|
-
// @ts-ignore
|
|
105
|
-
return connections[index];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* 删除数据库连接配置
|
|
110
|
-
*/
|
|
111
|
-
async deleteConnection(id: string): Promise<void> {
|
|
112
|
-
const connections = await this.getAllConnections();
|
|
113
|
-
const filteredConnections = connections.filter(conn => conn.id !== id);
|
|
114
|
-
|
|
115
|
-
if (filteredConnections.length === connections.length) {
|
|
116
|
-
throw new Error('连接配置不存在');
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
// 关闭活跃连接
|
|
120
|
-
if (this.activeConnections.has(id)) {
|
|
121
|
-
await this.activeConnections.get(id)?.destroy();
|
|
122
|
-
this.activeConnections.delete(id);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
await this.saveConnections(filteredConnections);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* 测试数据库连接
|
|
130
|
-
*/
|
|
131
|
-
async testConnection(connection: ConnectionEntity): Promise<boolean> {
|
|
132
|
-
try {
|
|
133
|
-
console.log('test', connection);
|
|
134
|
-
const tempDataSource = await this.createTypeORMDataSource(connection);
|
|
135
|
-
await tempDataSource.query('SELECT 1');
|
|
136
|
-
await tempDataSource.destroy();
|
|
137
|
-
return true;
|
|
138
|
-
} catch (error) {
|
|
139
|
-
console.error(error);
|
|
140
|
-
return false;
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* 获取活跃的数据库连接
|
|
146
|
-
*/
|
|
147
|
-
async getActiveConnection(id: string, database?: string): Promise<DataSource> {
|
|
148
|
-
const key = database ? `${id}_${database}` : id;
|
|
149
|
-
if (this.activeConnections.has(key)) {
|
|
150
|
-
const db = this.activeConnections.get(key);
|
|
151
|
-
// 检查连接是否仍然有效
|
|
152
|
-
if (db?.isInitialized) {
|
|
153
|
-
return db;
|
|
154
|
-
} else {
|
|
155
|
-
// 连接已关闭,从缓存中移除
|
|
156
|
-
this.activeConnections.delete(key);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// 连接池大小限制,防止连接数无限增长
|
|
161
|
-
if (this.activeConnections.size >= 10) {
|
|
162
|
-
// 关闭最旧的连接
|
|
163
|
-
const oldestKey = this.activeConnections.keys().next().value || '';
|
|
164
|
-
const oldestConnection = this.activeConnections.get(oldestKey);
|
|
165
|
-
try {
|
|
166
|
-
await oldestConnection?.destroy();
|
|
167
|
-
} catch (error) {
|
|
168
|
-
console.error(`关闭旧连接 ${oldestKey} 失败:`, error);
|
|
169
|
-
}
|
|
170
|
-
this.activeConnections.delete(oldestKey);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
const connectionConfig = await this.getConnectionById(id);
|
|
174
|
-
if (!connectionConfig) {
|
|
175
|
-
throw new Error('连接配置不存在');
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// 创建一个新的连接配置,使用指定的数据库
|
|
179
|
-
const updatedConnectionConfig: ConnectionEntity = {
|
|
180
|
-
...connectionConfig,
|
|
181
|
-
database: database || connectionConfig.database
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
const dataSource = await this.createTypeORMDataSource(updatedConnectionConfig);
|
|
185
|
-
this.activeConnections.set(key, dataSource);
|
|
186
|
-
|
|
187
|
-
return dataSource;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* 关闭数据库连接
|
|
192
|
-
*/
|
|
193
|
-
async closeConnection(id: string, database?: string): Promise<void> {
|
|
194
|
-
const key = database ? `${id}_${database}` : id;
|
|
195
|
-
if (this.activeConnections.has(key)) {
|
|
196
|
-
await this.activeConnections.get(key)?.destroy();
|
|
197
|
-
this.activeConnections.delete(key);
|
|
198
|
-
}
|
|
199
|
-
// 也关闭默认连接(如果存在)
|
|
200
|
-
if (database && this.activeConnections.has(id)) {
|
|
201
|
-
await this.activeConnections.get(id)?.destroy();
|
|
202
|
-
this.activeConnections.delete(id);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* 关闭特定连接的所有数据库连接
|
|
208
|
-
*/
|
|
209
|
-
async closeAllConnectionsForId(connectionId: string): Promise<void> {
|
|
210
|
-
const keysToDelete: string[] = [];
|
|
211
|
-
for (const [key, dataSource] of this.activeConnections) {
|
|
212
|
-
if (key.startsWith(connectionId + '_') || key === connectionId) {
|
|
213
|
-
try {
|
|
214
|
-
await dataSource.destroy();
|
|
215
|
-
keysToDelete.push(key);
|
|
216
|
-
} catch (error) {
|
|
217
|
-
console.error(`关闭连接 ${key} 失败:`, error);
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
keysToDelete.forEach(key => this.activeConnections.delete(key));
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* 关闭所有数据库连接
|
|
226
|
-
*/
|
|
227
|
-
async closeAllConnections(): Promise<void> {
|
|
228
|
-
for (const [id, dataSource] of this.activeConnections) {
|
|
229
|
-
try {
|
|
230
|
-
await dataSource.destroy();
|
|
231
|
-
} catch (error) {
|
|
232
|
-
console.error(`关闭连接 ${id} 失败:`, error);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
this.activeConnections.clear();
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* 创建TypeORM数据源
|
|
240
|
-
*/
|
|
241
|
-
private async createTypeORMDataSource(connectionConfig: ConnectionEntity): Promise<DataSource> {
|
|
242
|
-
const connectionOptions = this.getTypeORMOptions(connectionConfig);
|
|
243
|
-
return new DataSource(connectionOptions).initialize();
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* 获取TypeORM连接配置
|
|
248
|
-
*/
|
|
249
|
-
private getTypeORMOptions(connectionConfig: ConnectionEntity): DataSourceOptions {
|
|
250
|
-
const baseOptions = {
|
|
251
|
-
type: connectionConfig.type as any,
|
|
252
|
-
host: connectionConfig.host,
|
|
253
|
-
port: connectionConfig.port,
|
|
254
|
-
username: connectionConfig.username,
|
|
255
|
-
password: connectionConfig.password,
|
|
256
|
-
database: connectionConfig.database,
|
|
257
|
-
synchronize: false,
|
|
258
|
-
logging: false,
|
|
259
|
-
// 关键配置:开启多语句执行
|
|
260
|
-
extra: {
|
|
261
|
-
multipleStatements: true
|
|
262
|
-
},
|
|
263
|
-
...connectionConfig.options
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
// 根据数据库类型调整配置
|
|
267
|
-
switch (connectionConfig.type.toLowerCase()) {
|
|
268
|
-
case 'sqlite':
|
|
269
|
-
return {
|
|
270
|
-
...baseOptions,
|
|
271
|
-
type: 'sqlite' as any,
|
|
272
|
-
database: connectionConfig.database,
|
|
273
|
-
host: undefined,
|
|
274
|
-
port: undefined,
|
|
275
|
-
username: undefined,
|
|
276
|
-
password: undefined
|
|
277
|
-
};
|
|
278
|
-
case 'postgres':
|
|
279
|
-
case 'postgresql':
|
|
280
|
-
return {
|
|
281
|
-
...baseOptions,
|
|
282
|
-
type: 'postgres' as any,
|
|
283
|
-
ssl: connectionConfig.options?.ssl || false
|
|
284
|
-
};
|
|
285
|
-
case 'oracle':
|
|
286
|
-
return {
|
|
287
|
-
...baseOptions,
|
|
288
|
-
type: 'oracle' as any,
|
|
289
|
-
connectString: `${connectionConfig.host}:${connectionConfig.port}/${connectionConfig.database}`,
|
|
290
|
-
host: undefined,
|
|
291
|
-
port: undefined,
|
|
292
|
-
database: undefined,
|
|
293
|
-
extra: {
|
|
294
|
-
connectionTimeout: 60000,
|
|
295
|
-
poolMax: 10,
|
|
296
|
-
poolMin: 1,
|
|
297
|
-
poolIncrement: 1
|
|
298
|
-
}
|
|
299
|
-
};
|
|
300
|
-
case 'mssql':
|
|
301
|
-
case 'sqlserver':
|
|
302
|
-
return {
|
|
303
|
-
...baseOptions,
|
|
304
|
-
type: 'mssql' as any,
|
|
305
|
-
options: {
|
|
306
|
-
encrypt: connectionConfig.options?.encrypt || false,
|
|
307
|
-
trustServerCertificate: true
|
|
308
|
-
},
|
|
309
|
-
extra: {
|
|
310
|
-
connectionTimeout: 60000,
|
|
311
|
-
requestTimeout: 15000
|
|
312
|
-
}
|
|
313
|
-
};
|
|
314
|
-
default:
|
|
315
|
-
return baseOptions;
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* 保存连接配置到文件
|
|
321
|
-
*/
|
|
322
|
-
private async saveConnections(connections: ConnectionEntity[]): Promise<void> {
|
|
323
|
-
try {
|
|
324
|
-
const dir = path.dirname(this.configPath);
|
|
325
|
-
if (!fs.existsSync(dir)) {
|
|
326
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
327
|
-
}
|
|
328
|
-
fs.writeFileSync(this.configPath, JSON.stringify(connections, null, 2), 'utf8');
|
|
329
|
-
} catch (error) {
|
|
330
|
-
console.error('保存连接配置失败:', error);
|
|
331
|
-
throw error;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
|
|
335
|
-
/**
|
|
336
|
-
* 生成唯一ID
|
|
337
|
-
*/
|
|
338
|
-
private generateId(): string {
|
|
339
|
-
return Date.now().toString(36) + Math.random().toString(36).substr(2);
|
|
340
|
-
}
|
|
1
|
+
import * as fs from 'fs';
|
|
2
|
+
import * as path from 'path';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
import { ConnectionEntity } from './model/connection.entity';
|
|
5
|
+
import { DataSource, type DataSourceOptions } from 'typeorm';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 数据库连接管理服务
|
|
9
|
+
* 负责管理数据库连接配置和连接实例
|
|
10
|
+
*/
|
|
11
|
+
export class ConnectionService {
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 连接配置文件路径
|
|
15
|
+
* 优先使用环境变量 DB_TOOL_DATA_DIR,否则使用用户主目录下的 .db-tool 目录
|
|
16
|
+
*/
|
|
17
|
+
private readonly configPath: string;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 活跃的数据库连接实例
|
|
21
|
+
*/
|
|
22
|
+
private activeConnections: Map<string, DataSource> = new Map();
|
|
23
|
+
|
|
24
|
+
constructor() {
|
|
25
|
+
const dataDir = process.env.DB_TOOL_DATA_DIR || path.join(os.homedir(), '.fdb2');
|
|
26
|
+
this.configPath = path.join(dataDir, 'connections.json');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 初始化服务,创建配置目录
|
|
31
|
+
*/
|
|
32
|
+
async init() {
|
|
33
|
+
const configDir = path.dirname(this.configPath);
|
|
34
|
+
if (!fs.existsSync(configDir)) {
|
|
35
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 获取所有数据库连接配置
|
|
41
|
+
*/
|
|
42
|
+
async getAllConnections(): Promise<ConnectionEntity[]> {
|
|
43
|
+
try {
|
|
44
|
+
if (!fs.existsSync(this.configPath)) {
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
const data = fs.readFileSync(this.configPath, 'utf8');
|
|
48
|
+
return JSON.parse(data) as ConnectionEntity[];
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error('读取连接配置失败:', error);
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 根据ID获取数据库连接配置
|
|
57
|
+
*/
|
|
58
|
+
async getConnectionById(id: string): Promise<ConnectionEntity | null> {
|
|
59
|
+
const connections = await this.getAllConnections();
|
|
60
|
+
return connections.find(conn => conn.id === id) || null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 添加数据库连接配置
|
|
65
|
+
*/
|
|
66
|
+
async addConnection(connection: ConnectionEntity): Promise<ConnectionEntity> {
|
|
67
|
+
const connections = await this.getAllConnections();
|
|
68
|
+
|
|
69
|
+
// 检查名称是否重复
|
|
70
|
+
if (connections.find(conn => conn.name === connection.name)) {
|
|
71
|
+
throw new Error('连接名称已存在');
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 生成ID并设置时间戳
|
|
75
|
+
connection.id = this.generateId();
|
|
76
|
+
connection.createdAt = new Date();
|
|
77
|
+
connection.updatedAt = new Date();
|
|
78
|
+
connection.enabled = connection.enabled !== undefined ? connection.enabled : true;
|
|
79
|
+
|
|
80
|
+
connections.push(connection);
|
|
81
|
+
await this.saveConnections(connections);
|
|
82
|
+
|
|
83
|
+
return connection;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 更新数据库连接配置
|
|
88
|
+
*/
|
|
89
|
+
async updateConnection(id: string, updates: Partial<ConnectionEntity>): Promise<ConnectionEntity> {
|
|
90
|
+
const connections = await this.getAllConnections();
|
|
91
|
+
const index = connections.findIndex(conn => conn.id === id);
|
|
92
|
+
|
|
93
|
+
if (index === -1) {
|
|
94
|
+
throw new Error('连接配置不存在');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 检查名称重复
|
|
98
|
+
if (updates.name && connections.find((conn, idx) => conn.name === updates.name && idx !== index)) {
|
|
99
|
+
throw new Error('连接名称已存在');
|
|
100
|
+
}
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
connections[index] = { ...connections[index], ...updates, updatedAt: new Date() };
|
|
103
|
+
await this.saveConnections(connections);
|
|
104
|
+
// @ts-ignore
|
|
105
|
+
return connections[index];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 删除数据库连接配置
|
|
110
|
+
*/
|
|
111
|
+
async deleteConnection(id: string): Promise<void> {
|
|
112
|
+
const connections = await this.getAllConnections();
|
|
113
|
+
const filteredConnections = connections.filter(conn => conn.id !== id);
|
|
114
|
+
|
|
115
|
+
if (filteredConnections.length === connections.length) {
|
|
116
|
+
throw new Error('连接配置不存在');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// 关闭活跃连接
|
|
120
|
+
if (this.activeConnections.has(id)) {
|
|
121
|
+
await this.activeConnections.get(id)?.destroy();
|
|
122
|
+
this.activeConnections.delete(id);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
await this.saveConnections(filteredConnections);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 测试数据库连接
|
|
130
|
+
*/
|
|
131
|
+
async testConnection(connection: ConnectionEntity): Promise<boolean> {
|
|
132
|
+
try {
|
|
133
|
+
console.log('test', connection);
|
|
134
|
+
const tempDataSource = await this.createTypeORMDataSource(connection);
|
|
135
|
+
await tempDataSource.query('SELECT 1');
|
|
136
|
+
await tempDataSource.destroy();
|
|
137
|
+
return true;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
console.error(error);
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 获取活跃的数据库连接
|
|
146
|
+
*/
|
|
147
|
+
async getActiveConnection(id: string, database?: string): Promise<DataSource> {
|
|
148
|
+
const key = database ? `${id}_${database}` : id;
|
|
149
|
+
if (this.activeConnections.has(key)) {
|
|
150
|
+
const db = this.activeConnections.get(key);
|
|
151
|
+
// 检查连接是否仍然有效
|
|
152
|
+
if (db?.isInitialized) {
|
|
153
|
+
return db;
|
|
154
|
+
} else {
|
|
155
|
+
// 连接已关闭,从缓存中移除
|
|
156
|
+
this.activeConnections.delete(key);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 连接池大小限制,防止连接数无限增长
|
|
161
|
+
if (this.activeConnections.size >= 10) {
|
|
162
|
+
// 关闭最旧的连接
|
|
163
|
+
const oldestKey = this.activeConnections.keys().next().value || '';
|
|
164
|
+
const oldestConnection = this.activeConnections.get(oldestKey);
|
|
165
|
+
try {
|
|
166
|
+
await oldestConnection?.destroy();
|
|
167
|
+
} catch (error) {
|
|
168
|
+
console.error(`关闭旧连接 ${oldestKey} 失败:`, error);
|
|
169
|
+
}
|
|
170
|
+
this.activeConnections.delete(oldestKey);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const connectionConfig = await this.getConnectionById(id);
|
|
174
|
+
if (!connectionConfig) {
|
|
175
|
+
throw new Error('连接配置不存在');
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// 创建一个新的连接配置,使用指定的数据库
|
|
179
|
+
const updatedConnectionConfig: ConnectionEntity = {
|
|
180
|
+
...connectionConfig,
|
|
181
|
+
database: database || connectionConfig.database
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const dataSource = await this.createTypeORMDataSource(updatedConnectionConfig);
|
|
185
|
+
this.activeConnections.set(key, dataSource);
|
|
186
|
+
|
|
187
|
+
return dataSource;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* 关闭数据库连接
|
|
192
|
+
*/
|
|
193
|
+
async closeConnection(id: string, database?: string): Promise<void> {
|
|
194
|
+
const key = database ? `${id}_${database}` : id;
|
|
195
|
+
if (this.activeConnections.has(key)) {
|
|
196
|
+
await this.activeConnections.get(key)?.destroy();
|
|
197
|
+
this.activeConnections.delete(key);
|
|
198
|
+
}
|
|
199
|
+
// 也关闭默认连接(如果存在)
|
|
200
|
+
if (database && this.activeConnections.has(id)) {
|
|
201
|
+
await this.activeConnections.get(id)?.destroy();
|
|
202
|
+
this.activeConnections.delete(id);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* 关闭特定连接的所有数据库连接
|
|
208
|
+
*/
|
|
209
|
+
async closeAllConnectionsForId(connectionId: string): Promise<void> {
|
|
210
|
+
const keysToDelete: string[] = [];
|
|
211
|
+
for (const [key, dataSource] of this.activeConnections) {
|
|
212
|
+
if (key.startsWith(connectionId + '_') || key === connectionId) {
|
|
213
|
+
try {
|
|
214
|
+
await dataSource.destroy();
|
|
215
|
+
keysToDelete.push(key);
|
|
216
|
+
} catch (error) {
|
|
217
|
+
console.error(`关闭连接 ${key} 失败:`, error);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
keysToDelete.forEach(key => this.activeConnections.delete(key));
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 关闭所有数据库连接
|
|
226
|
+
*/
|
|
227
|
+
async closeAllConnections(): Promise<void> {
|
|
228
|
+
for (const [id, dataSource] of this.activeConnections) {
|
|
229
|
+
try {
|
|
230
|
+
await dataSource.destroy();
|
|
231
|
+
} catch (error) {
|
|
232
|
+
console.error(`关闭连接 ${id} 失败:`, error);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
this.activeConnections.clear();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* 创建TypeORM数据源
|
|
240
|
+
*/
|
|
241
|
+
private async createTypeORMDataSource(connectionConfig: ConnectionEntity): Promise<DataSource> {
|
|
242
|
+
const connectionOptions = this.getTypeORMOptions(connectionConfig);
|
|
243
|
+
return new DataSource(connectionOptions).initialize();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 获取TypeORM连接配置
|
|
248
|
+
*/
|
|
249
|
+
private getTypeORMOptions(connectionConfig: ConnectionEntity): DataSourceOptions {
|
|
250
|
+
const baseOptions = {
|
|
251
|
+
type: connectionConfig.type as any,
|
|
252
|
+
host: connectionConfig.host,
|
|
253
|
+
port: connectionConfig.port,
|
|
254
|
+
username: connectionConfig.username,
|
|
255
|
+
password: connectionConfig.password,
|
|
256
|
+
database: connectionConfig.database,
|
|
257
|
+
synchronize: false,
|
|
258
|
+
logging: false,
|
|
259
|
+
// 关键配置:开启多语句执行
|
|
260
|
+
extra: {
|
|
261
|
+
multipleStatements: true
|
|
262
|
+
},
|
|
263
|
+
...connectionConfig.options
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
// 根据数据库类型调整配置
|
|
267
|
+
switch (connectionConfig.type.toLowerCase()) {
|
|
268
|
+
case 'sqlite':
|
|
269
|
+
return {
|
|
270
|
+
...baseOptions,
|
|
271
|
+
type: 'sqlite' as any,
|
|
272
|
+
database: connectionConfig.database,
|
|
273
|
+
host: undefined,
|
|
274
|
+
port: undefined,
|
|
275
|
+
username: undefined,
|
|
276
|
+
password: undefined
|
|
277
|
+
};
|
|
278
|
+
case 'postgres':
|
|
279
|
+
case 'postgresql':
|
|
280
|
+
return {
|
|
281
|
+
...baseOptions,
|
|
282
|
+
type: 'postgres' as any,
|
|
283
|
+
ssl: connectionConfig.options?.ssl || false
|
|
284
|
+
};
|
|
285
|
+
case 'oracle':
|
|
286
|
+
return {
|
|
287
|
+
...baseOptions,
|
|
288
|
+
type: 'oracle' as any,
|
|
289
|
+
connectString: `${connectionConfig.host}:${connectionConfig.port}/${connectionConfig.database}`,
|
|
290
|
+
host: undefined,
|
|
291
|
+
port: undefined,
|
|
292
|
+
database: undefined,
|
|
293
|
+
extra: {
|
|
294
|
+
connectionTimeout: 60000,
|
|
295
|
+
poolMax: 10,
|
|
296
|
+
poolMin: 1,
|
|
297
|
+
poolIncrement: 1
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
case 'mssql':
|
|
301
|
+
case 'sqlserver':
|
|
302
|
+
return {
|
|
303
|
+
...baseOptions,
|
|
304
|
+
type: 'mssql' as any,
|
|
305
|
+
options: {
|
|
306
|
+
encrypt: connectionConfig.options?.encrypt || false,
|
|
307
|
+
trustServerCertificate: true
|
|
308
|
+
},
|
|
309
|
+
extra: {
|
|
310
|
+
connectionTimeout: 60000,
|
|
311
|
+
requestTimeout: 15000
|
|
312
|
+
}
|
|
313
|
+
};
|
|
314
|
+
default:
|
|
315
|
+
return baseOptions;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* 保存连接配置到文件
|
|
321
|
+
*/
|
|
322
|
+
private async saveConnections(connections: ConnectionEntity[]): Promise<void> {
|
|
323
|
+
try {
|
|
324
|
+
const dir = path.dirname(this.configPath);
|
|
325
|
+
if (!fs.existsSync(dir)) {
|
|
326
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
327
|
+
}
|
|
328
|
+
fs.writeFileSync(this.configPath, JSON.stringify(connections, null, 2), 'utf8');
|
|
329
|
+
} catch (error) {
|
|
330
|
+
console.error('保存连接配置失败:', error);
|
|
331
|
+
throw error;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* 生成唯一ID
|
|
337
|
+
*/
|
|
338
|
+
private generateId(): string {
|
|
339
|
+
return Date.now().toString(36) + Math.random().toString(36).substr(2);
|
|
340
|
+
}
|
|
341
341
|
}
|