fdb2 1.0.0 → 1.0.2
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/README.md +334 -221
- package/bin/fdb2.js +39 -22
- package/package.json +2 -2
- package/server/service/connection.service.ts +8 -1
- package/server/service/database/cockroachdb.service.ts +659 -0
- package/server/service/database/database.service.ts +120 -0
- package/server/service/database/mongodb.service.ts +454 -0
- package/server/service/database/oracle.service.ts +7 -14
- package/server/service/database/postgres.service.ts +4 -7
- package/server/service/database/sap.service.ts +713 -0
- package/server/service/database/sqlite.service.ts +3 -3
- package/server.js +23 -1
- package/server.pid +1 -0
- package/src/platform/database/components/database-detail.vue +11 -11
- package/src/platform/database/components/table-detail.vue +4 -4
- package/src/platform/database/types/common.ts +1 -1
- package/vite.config.ts +1 -1
- package/bin/docker/.env +0 -4
- package/data/connections.demo.json +0 -32
- package/nw-build.js +0 -120
- package/nw-dev.js +0 -65
- package/src/base//345/237/272/347/241/200/345/261/202.md +0 -7
|
@@ -9,6 +9,9 @@ import { PostgreSQLService } from './postgres.service';
|
|
|
9
9
|
import { SQLiteService } from './sqlite.service';
|
|
10
10
|
import { OracleService } from './oracle.service';
|
|
11
11
|
import { SQLServerService } from './mssql.service';
|
|
12
|
+
import { CockroachDBService } from './cockroachdb.service';
|
|
13
|
+
import { MongoDBService } from './mongodb.service';
|
|
14
|
+
import { SAPHANADatabaseService } from './sap.service';
|
|
12
15
|
|
|
13
16
|
/**
|
|
14
17
|
* 数据库服务管理类
|
|
@@ -22,6 +25,9 @@ export class DatabaseService {
|
|
|
22
25
|
private sqliteService: SQLiteService;
|
|
23
26
|
private oracleService: OracleService;
|
|
24
27
|
private sqlServerService: SQLServerService;
|
|
28
|
+
private cockroachDBService: CockroachDBService;
|
|
29
|
+
private mongoDBService: MongoDBService;
|
|
30
|
+
private sapHANADatabaseService: SAPHANADatabaseService;
|
|
25
31
|
|
|
26
32
|
constructor() {
|
|
27
33
|
this.connectionService = new ConnectionService();
|
|
@@ -31,6 +37,9 @@ export class DatabaseService {
|
|
|
31
37
|
this.sqliteService = new SQLiteService();
|
|
32
38
|
this.oracleService = new OracleService();
|
|
33
39
|
this.sqlServerService = new SQLServerService();
|
|
40
|
+
this.cockroachDBService = new CockroachDBService();
|
|
41
|
+
this.mongoDBService = new MongoDBService();
|
|
42
|
+
this.sapHANADatabaseService = new SAPHANADatabaseService();
|
|
34
43
|
}
|
|
35
44
|
|
|
36
45
|
/**
|
|
@@ -40,17 +49,34 @@ export class DatabaseService {
|
|
|
40
49
|
|
|
41
50
|
switch (type.toLowerCase()) {
|
|
42
51
|
case 'mysql':
|
|
52
|
+
case 'aurora-mysql':
|
|
53
|
+
case 'auroramysql':
|
|
43
54
|
return this.mysqlService;
|
|
44
55
|
case 'postgres':
|
|
45
56
|
case 'postgresql':
|
|
57
|
+
case 'aurora-postgres':
|
|
58
|
+
case 'aurorapostgres':
|
|
59
|
+
case 'aurora-postgresql':
|
|
46
60
|
return this.postgreSQLService;
|
|
47
61
|
case 'sqlite':
|
|
62
|
+
case 'better-sqlite3':
|
|
63
|
+
case 'bettersqlite3':
|
|
48
64
|
return this.sqliteService;
|
|
49
65
|
case 'oracle':
|
|
50
66
|
return this.oracleService;
|
|
51
67
|
case 'mssql':
|
|
52
68
|
case 'sqlserver':
|
|
53
69
|
return this.sqlServerService;
|
|
70
|
+
case 'cockroachdb':
|
|
71
|
+
case 'cockroach':
|
|
72
|
+
return this.cockroachDBService;
|
|
73
|
+
case 'mongodb':
|
|
74
|
+
case 'mongo':
|
|
75
|
+
return this.mongoDBService;
|
|
76
|
+
case 'sap':
|
|
77
|
+
case 'sap-hana':
|
|
78
|
+
case 'saphana':
|
|
79
|
+
return this.sapHANADatabaseService;
|
|
54
80
|
default:
|
|
55
81
|
throw new Error(`不支持的数据库类型: ${type}`);
|
|
56
82
|
}
|
|
@@ -250,6 +276,60 @@ export class DatabaseService {
|
|
|
250
276
|
supportArrays: false,
|
|
251
277
|
supportStoredProcedures: true
|
|
252
278
|
}
|
|
279
|
+
},
|
|
280
|
+
{
|
|
281
|
+
value: 'cockroachdb',
|
|
282
|
+
label: 'CockroachDB',
|
|
283
|
+
icon: 'bi-database',
|
|
284
|
+
defaultPort: 26257,
|
|
285
|
+
description: 'CockroachDB分布式SQL数据库',
|
|
286
|
+
features: {
|
|
287
|
+
supportSchemas: true,
|
|
288
|
+
supportProcedures: true,
|
|
289
|
+
supportTriggers: true,
|
|
290
|
+
supportViews: true,
|
|
291
|
+
supportFullTextSearch: true,
|
|
292
|
+
supportJson: true,
|
|
293
|
+
supportArrays: true,
|
|
294
|
+
supportEnum: true,
|
|
295
|
+
supportDistributed: true
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
{
|
|
299
|
+
value: 'mongodb',
|
|
300
|
+
label: 'MongoDB',
|
|
301
|
+
icon: 'bi-database',
|
|
302
|
+
defaultPort: 27017,
|
|
303
|
+
description: 'MongoDB文档数据库',
|
|
304
|
+
features: {
|
|
305
|
+
supportSchemas: false,
|
|
306
|
+
supportProcedures: false,
|
|
307
|
+
supportTriggers: false,
|
|
308
|
+
supportViews: false,
|
|
309
|
+
supportFullTextSearch: true,
|
|
310
|
+
supportJson: true,
|
|
311
|
+
supportArrays: true,
|
|
312
|
+
supportDocuments: true,
|
|
313
|
+
supportNoSQL: true
|
|
314
|
+
}
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
value: 'sap',
|
|
318
|
+
label: 'SAP HANA',
|
|
319
|
+
icon: 'bi-database',
|
|
320
|
+
defaultPort: 39013,
|
|
321
|
+
description: 'SAP HANA内存数据库',
|
|
322
|
+
features: {
|
|
323
|
+
supportSchemas: true,
|
|
324
|
+
supportProcedures: true,
|
|
325
|
+
supportTriggers: true,
|
|
326
|
+
supportViews: true,
|
|
327
|
+
supportFullTextSearch: true,
|
|
328
|
+
supportJson: true,
|
|
329
|
+
supportArrays: false,
|
|
330
|
+
supportInMemory: true,
|
|
331
|
+
supportHighPerformance: true
|
|
332
|
+
}
|
|
253
333
|
}
|
|
254
334
|
];
|
|
255
335
|
}
|
|
@@ -503,6 +583,46 @@ export class DatabaseService {
|
|
|
503
583
|
supportArrays: false,
|
|
504
584
|
supportStoredProcedures: true
|
|
505
585
|
};
|
|
586
|
+
case 'cockroachdb':
|
|
587
|
+
case 'cockroach':
|
|
588
|
+
return {
|
|
589
|
+
supportSchemas: true,
|
|
590
|
+
supportProcedures: true,
|
|
591
|
+
supportTriggers: true,
|
|
592
|
+
supportViews: true,
|
|
593
|
+
supportFullTextSearch: true,
|
|
594
|
+
supportJson: true,
|
|
595
|
+
supportArrays: true,
|
|
596
|
+
supportEnum: true,
|
|
597
|
+
supportDistributed: true
|
|
598
|
+
};
|
|
599
|
+
case 'mongodb':
|
|
600
|
+
case 'mongo':
|
|
601
|
+
return {
|
|
602
|
+
supportSchemas: false,
|
|
603
|
+
supportProcedures: false,
|
|
604
|
+
supportTriggers: false,
|
|
605
|
+
supportViews: false,
|
|
606
|
+
supportFullTextSearch: true,
|
|
607
|
+
supportJson: true,
|
|
608
|
+
supportArrays: true,
|
|
609
|
+
supportDocuments: true,
|
|
610
|
+
supportNoSQL: true
|
|
611
|
+
};
|
|
612
|
+
case 'sap':
|
|
613
|
+
case 'sap-hana':
|
|
614
|
+
case 'saphana':
|
|
615
|
+
return {
|
|
616
|
+
supportSchemas: true,
|
|
617
|
+
supportProcedures: true,
|
|
618
|
+
supportTriggers: true,
|
|
619
|
+
supportViews: true,
|
|
620
|
+
supportFullTextSearch: true,
|
|
621
|
+
supportJson: true,
|
|
622
|
+
supportArrays: false,
|
|
623
|
+
supportInMemory: true,
|
|
624
|
+
supportHighPerformance: true
|
|
625
|
+
};
|
|
506
626
|
default:
|
|
507
627
|
return {};
|
|
508
628
|
}
|
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
import { DataSource } from 'typeorm';
|
|
2
|
+
import { BaseDatabaseService } from './base.service';
|
|
3
|
+
import {
|
|
4
|
+
DatabaseEntity,
|
|
5
|
+
TableEntity,
|
|
6
|
+
ColumnEntity,
|
|
7
|
+
IndexEntity,
|
|
8
|
+
ForeignKeyEntity
|
|
9
|
+
} from '../../model/database.entity';
|
|
10
|
+
import * as fs from 'fs';
|
|
11
|
+
import * as path from 'path';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* MongoDB数据库服务实现
|
|
15
|
+
* MongoDB 是一个 NoSQL 文档数据库,与关系型数据库有很大不同
|
|
16
|
+
*/
|
|
17
|
+
export class MongoDBService extends BaseDatabaseService {
|
|
18
|
+
|
|
19
|
+
getDatabaseType() {
|
|
20
|
+
return 'mongodb';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 获取MongoDB数据库列表
|
|
25
|
+
*/
|
|
26
|
+
async getDatabases(dataSource: DataSource): Promise<string[]> {
|
|
27
|
+
try {
|
|
28
|
+
const result = await dataSource.query(`db.adminCommand({ listDatabases: 1 })`);
|
|
29
|
+
return result.databases.map((db: any) => db.name).filter((name: string) => name !== 'admin' && name !== 'local' && name !== 'config');
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error('获取MongoDB数据库列表失败:', error);
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 获取MongoDB集合列表(相当于关系型数据库的表)
|
|
38
|
+
*/
|
|
39
|
+
async getTables(dataSource: DataSource, database: string): Promise<TableEntity[]> {
|
|
40
|
+
try {
|
|
41
|
+
const result = await dataSource.query(`db.getCollectionNames()`);
|
|
42
|
+
return result.map((collectionName: string) => ({
|
|
43
|
+
name: collectionName,
|
|
44
|
+
type: 'collection',
|
|
45
|
+
rowCount: undefined,
|
|
46
|
+
dataSize: undefined,
|
|
47
|
+
indexSize: undefined
|
|
48
|
+
}));
|
|
49
|
+
} catch (error) {
|
|
50
|
+
console.error('获取MongoDB集合列表失败:', error);
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 获取MongoDB集合的字段信息(文档结构)
|
|
57
|
+
*/
|
|
58
|
+
async getColumns(dataSource: DataSource, database: string, table: string): Promise<ColumnEntity[]> {
|
|
59
|
+
try {
|
|
60
|
+
const result = await dataSource.query(`db.${table}.findOne()`);
|
|
61
|
+
if (!result) {
|
|
62
|
+
return [];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const fields = Object.keys(result);
|
|
66
|
+
return fields.map(field => ({
|
|
67
|
+
name: field,
|
|
68
|
+
type: this.inferType(result[field]),
|
|
69
|
+
nullable: true,
|
|
70
|
+
defaultValue: undefined,
|
|
71
|
+
isPrimary: field === '_id',
|
|
72
|
+
isAutoIncrement: field === '_id'
|
|
73
|
+
}));
|
|
74
|
+
} catch (error) {
|
|
75
|
+
console.error('获取MongoDB集合字段信息失败:', error);
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 推断字段类型
|
|
82
|
+
*/
|
|
83
|
+
private inferType(value: any): string {
|
|
84
|
+
if (value === null || value === undefined) {
|
|
85
|
+
return 'null';
|
|
86
|
+
}
|
|
87
|
+
const type = typeof value;
|
|
88
|
+
if (type === 'string') {
|
|
89
|
+
return 'string';
|
|
90
|
+
} else if (type === 'number') {
|
|
91
|
+
return Number.isInteger(value) ? 'int' : 'double';
|
|
92
|
+
} else if (type === 'boolean') {
|
|
93
|
+
return 'boolean';
|
|
94
|
+
} else if (type === 'object') {
|
|
95
|
+
if (Array.isArray(value)) {
|
|
96
|
+
return 'array';
|
|
97
|
+
} else if (value instanceof Date) {
|
|
98
|
+
return 'date';
|
|
99
|
+
} else if (value instanceof Buffer) {
|
|
100
|
+
return 'binData';
|
|
101
|
+
} else if (value instanceof ObjectId) {
|
|
102
|
+
return 'objectId';
|
|
103
|
+
} else {
|
|
104
|
+
return 'object';
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return 'unknown';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 获取MongoDB索引信息
|
|
112
|
+
*/
|
|
113
|
+
async getIndexes(dataSource: DataSource, database: string, table: string): Promise<IndexEntity[]> {
|
|
114
|
+
try {
|
|
115
|
+
const result = await dataSource.query(`db.${table}.getIndexes()`);
|
|
116
|
+
return result.map((index: any) => ({
|
|
117
|
+
name: index.name,
|
|
118
|
+
type: index.unique ? 'UNIQUE' : 'INDEX',
|
|
119
|
+
columns: Object.keys(index.key),
|
|
120
|
+
unique: index.unique || false
|
|
121
|
+
}));
|
|
122
|
+
} catch (error) {
|
|
123
|
+
console.error('获取MongoDB索引信息失败:', error);
|
|
124
|
+
return [];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* MongoDB不支持外键
|
|
130
|
+
*/
|
|
131
|
+
async getForeignKeys(dataSource: DataSource, database: string, table: string): Promise<ForeignKeyEntity[]> {
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 获取MongoDB数据库大小
|
|
137
|
+
*/
|
|
138
|
+
async getDatabaseSize(dataSource: DataSource, database: string): Promise<number> {
|
|
139
|
+
try {
|
|
140
|
+
const result = await dataSource.query(`db.stats({ scale: 1 })`);
|
|
141
|
+
return result.dataSize || 0;
|
|
142
|
+
} catch (error) {
|
|
143
|
+
console.error('获取MongoDB数据库大小失败:', error);
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* MongoDB不支持视图
|
|
150
|
+
*/
|
|
151
|
+
async getViews(dataSource: DataSource, database: string): Promise<any[]> {
|
|
152
|
+
return [];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* MongoDB不支持视图
|
|
157
|
+
*/
|
|
158
|
+
async getViewDefinition(dataSource: DataSource, database: string, viewName: string): Promise<string> {
|
|
159
|
+
throw new Error('MongoDB不支持视图');
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* MongoDB不支持存储过程
|
|
164
|
+
*/
|
|
165
|
+
async getProcedures(dataSource: DataSource, database: string): Promise<any[]> {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* MongoDB不支持存储过程
|
|
171
|
+
*/
|
|
172
|
+
async getProcedureDefinition(dataSource: DataSource, database: string, procedureName: string): Promise<string> {
|
|
173
|
+
throw new Error('MongoDB不支持存储过程');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 创建MongoDB数据库
|
|
178
|
+
*/
|
|
179
|
+
async createDatabase(dataSource: DataSource, databaseName: string, options?: any): Promise<void> {
|
|
180
|
+
try {
|
|
181
|
+
await dataSource.query(`db.getSiblingDB('${databaseName}').createCollection('temp')`);
|
|
182
|
+
await dataSource.query(`db.getSiblingDB('${databaseName}').temp.drop()`);
|
|
183
|
+
} catch (error) {
|
|
184
|
+
console.error('创建MongoDB数据库失败:', error);
|
|
185
|
+
throw new Error(`创建数据库失败: ${error.message}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 删除MongoDB数据库
|
|
191
|
+
*/
|
|
192
|
+
async dropDatabase(dataSource: DataSource, databaseName: string): Promise<void> {
|
|
193
|
+
try {
|
|
194
|
+
await dataSource.query(`db.getSiblingDB('${databaseName}').dropDatabase()`);
|
|
195
|
+
} catch (error) {
|
|
196
|
+
console.error('删除MongoDB数据库失败:', error);
|
|
197
|
+
throw new Error(`删除数据库失败: ${error.message}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 导出数据库架构
|
|
203
|
+
*/
|
|
204
|
+
async exportSchema(dataSource: DataSource, databaseName: string): Promise<string> {
|
|
205
|
+
const collections = await this.getTables(dataSource, databaseName);
|
|
206
|
+
let schemaSql = `-- MongoDB数据库架构导出 - ${databaseName}\n`;
|
|
207
|
+
schemaSql += `-- 导出时间: ${new Date().toISOString()}\n\n`;
|
|
208
|
+
|
|
209
|
+
for (const collection of collections) {
|
|
210
|
+
const columns = await this.getColumns(dataSource, databaseName, collection.name);
|
|
211
|
+
const indexes = await this.getIndexes(dataSource, databaseName, collection.name);
|
|
212
|
+
|
|
213
|
+
schemaSql += `// 集合: ${collection.name}\n`;
|
|
214
|
+
schemaSql += `// 字段:\n`;
|
|
215
|
+
columns.forEach(column => {
|
|
216
|
+
schemaSql += `// ${column.name}: ${column.type}${column.isPrimary ? ' (主键)' : ''}\n`;
|
|
217
|
+
});
|
|
218
|
+
schemaSql += `\n`;
|
|
219
|
+
|
|
220
|
+
if (indexes.length > 0) {
|
|
221
|
+
schemaSql += `// 索引:\n`;
|
|
222
|
+
indexes.forEach(index => {
|
|
223
|
+
schemaSql += `// ${index.name}: ${index.type} (${index.columns.join(', ')})\n`;
|
|
224
|
+
});
|
|
225
|
+
schemaSql += `\n`;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return schemaSql;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 查看MongoDB日志
|
|
234
|
+
*/
|
|
235
|
+
async viewLogs(dataSource: DataSource, database?: string, limit: number = 100): Promise<any[]> {
|
|
236
|
+
try {
|
|
237
|
+
const result = await dataSource.query(`db.getSiblingDB('admin').adminCommand({ getLog: 'global' })`);
|
|
238
|
+
return result.log || [];
|
|
239
|
+
} catch (error) {
|
|
240
|
+
console.error('获取MongoDB日志失败:', error);
|
|
241
|
+
return [{ message: 'MongoDB日志功能需要管理员权限,请检查数据库配置' }];
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 备份MongoDB数据库
|
|
247
|
+
*/
|
|
248
|
+
async backupDatabase(dataSource: DataSource, databaseName: string, options?: any): Promise<string> {
|
|
249
|
+
try {
|
|
250
|
+
const backupPath = options?.path || path.join(__dirname, '..', '..', '..', 'data', 'backups');
|
|
251
|
+
if (!fs.existsSync(backupPath)) {
|
|
252
|
+
fs.mkdirSync(backupPath, { recursive: true });
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
256
|
+
const backupFile = path.join(backupPath, `${databaseName}_${timestamp}.json`);
|
|
257
|
+
|
|
258
|
+
const collections = await this.getTables(dataSource, databaseName);
|
|
259
|
+
const backupData: any = {
|
|
260
|
+
database: databaseName,
|
|
261
|
+
exportedAt: new Date().toISOString(),
|
|
262
|
+
collections: {}
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
for (const collection of collections) {
|
|
266
|
+
const result = await dataSource.query(`db.${collection.name}.find().toArray()`);
|
|
267
|
+
backupData.collections[collection.name] = result;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
fs.writeFileSync(backupFile, JSON.stringify(backupData, null, 2), 'utf8');
|
|
271
|
+
return `备份成功:${backupFile}`;
|
|
272
|
+
} catch (error) {
|
|
273
|
+
console.error('MongoDB备份失败:', error);
|
|
274
|
+
throw new Error(`备份失败: ${error.message}`);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* 恢复MongoDB数据库
|
|
280
|
+
*/
|
|
281
|
+
async restoreDatabase(dataSource: DataSource, databaseName: string, filePath: string, options?: any): Promise<void> {
|
|
282
|
+
try {
|
|
283
|
+
const backupData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
284
|
+
|
|
285
|
+
for (const collectionName in backupData.collections) {
|
|
286
|
+
const documents = backupData.collections[collectionName];
|
|
287
|
+
if (documents.length > 0) {
|
|
288
|
+
await dataSource.query(`db.${collectionName}.insertMany(${JSON.stringify(documents)})`);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
} catch (error) {
|
|
292
|
+
console.error('MongoDB恢复失败:', error);
|
|
293
|
+
throw new Error(`恢复失败: ${error.message}`);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* 导出表数据到 SQL 文件(MongoDB不支持SQL)
|
|
299
|
+
*/
|
|
300
|
+
async exportTableDataToSQL(dataSource: DataSource, databaseName: string, tableName: string, options?: any): Promise<string> {
|
|
301
|
+
throw new Error('MongoDB不支持导出为SQL格式,请使用JSON格式');
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 导出表数据到 CSV 文件
|
|
306
|
+
*/
|
|
307
|
+
async exportTableDataToCSV(dataSource: DataSource, databaseName: string, tableName: string, options?: any): Promise<string> {
|
|
308
|
+
try {
|
|
309
|
+
const exportPath = options?.path || path.join(__dirname, '..', '..', '..', 'data', 'exports');
|
|
310
|
+
if (!fs.existsSync(exportPath)) {
|
|
311
|
+
fs.mkdirSync(exportPath, { recursive: true });
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
315
|
+
const exportFile = path.join(exportPath, `${tableName}_data_${timestamp}.csv`);
|
|
316
|
+
|
|
317
|
+
const result = await dataSource.query(`db.${tableName}.find().toArray()`);
|
|
318
|
+
|
|
319
|
+
if (result.length === 0) {
|
|
320
|
+
fs.writeFileSync(exportFile, '', 'utf8');
|
|
321
|
+
return exportFile;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const columns = await this.getColumns(dataSource, databaseName, tableName);
|
|
325
|
+
const columnNames = columns.map(column => column.name);
|
|
326
|
+
|
|
327
|
+
const bom = Buffer.from([0xEF, 0xBB, 0xBF]);
|
|
328
|
+
fs.writeFileSync(exportFile, bom);
|
|
329
|
+
fs.appendFileSync(exportFile, columnNames.map(name => `"${name}"`).join(',') + '\n', 'utf8');
|
|
330
|
+
|
|
331
|
+
let csv = '';
|
|
332
|
+
result.forEach((doc) => {
|
|
333
|
+
const values = columnNames.map(column => {
|
|
334
|
+
const value = doc[column];
|
|
335
|
+
if (value === null || value === undefined) {
|
|
336
|
+
return '';
|
|
337
|
+
} else if (typeof value === 'string') {
|
|
338
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
339
|
+
} else if (value instanceof Date) {
|
|
340
|
+
return `"${value.toISOString()}"`;
|
|
341
|
+
} else if (typeof value === 'object' && value !== null) {
|
|
342
|
+
try {
|
|
343
|
+
return `"${JSON.stringify(value).replace(/"/g, '""')}"`;
|
|
344
|
+
} catch {
|
|
345
|
+
return `"${String(value).replace(/"/g, '""')}"`;
|
|
346
|
+
}
|
|
347
|
+
} else {
|
|
348
|
+
return String(value);
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
csv += values.join(',') + '\n';
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
fs.appendFileSync(exportFile, csv, 'utf8');
|
|
355
|
+
return exportFile;
|
|
356
|
+
} catch (error) {
|
|
357
|
+
console.error('MongoDB导出表数据到CSV失败:', error);
|
|
358
|
+
throw new Error(`导出表数据到CSV失败: ${error.message}`);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
/**
|
|
363
|
+
* 导出表数据到 JSON 文件
|
|
364
|
+
*/
|
|
365
|
+
async exportTableDataToJSON(dataSource: DataSource, databaseName: string, tableName: string, options?: any): Promise<string> {
|
|
366
|
+
try {
|
|
367
|
+
const exportPath = options?.path || path.join(__dirname, '..', '..', '..', 'data', 'exports');
|
|
368
|
+
if (!fs.existsSync(exportPath)) {
|
|
369
|
+
fs.mkdirSync(exportPath, { recursive: true });
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
373
|
+
const exportFile = path.join(exportPath, `${tableName}_data_${timestamp}.json`);
|
|
374
|
+
|
|
375
|
+
const result = await dataSource.query(`db.${tableName}.find().toArray()`);
|
|
376
|
+
fs.writeFileSync(exportFile, JSON.stringify(result, null, 2), 'utf8');
|
|
377
|
+
return exportFile;
|
|
378
|
+
} catch (error) {
|
|
379
|
+
console.error('MongoDB导出表数据到JSON失败:', error);
|
|
380
|
+
throw new Error(`导出表数据到JSON失败: ${error.message}`);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* 导出表数据到 Excel 文件
|
|
386
|
+
*/
|
|
387
|
+
async exportTableDataToExcel(dataSource: DataSource, databaseName: string, tableName: string, options?: any): Promise<string> {
|
|
388
|
+
try {
|
|
389
|
+
const exportPath = options?.path || path.join(__dirname, '..', '..', '..', 'data', 'exports');
|
|
390
|
+
if (!fs.existsSync(exportPath)) {
|
|
391
|
+
fs.mkdirSync(exportPath, { recursive: true });
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
395
|
+
const exportFile = path.join(exportPath, `${tableName}_data_${timestamp}.xlsx`);
|
|
396
|
+
|
|
397
|
+
const result = await dataSource.query(`db.${tableName}.find().toArray()`);
|
|
398
|
+
const columns = await this.getColumns(dataSource, databaseName, tableName);
|
|
399
|
+
const columnNames = columns.map(column => column.name);
|
|
400
|
+
|
|
401
|
+
const ExcelJS = require('exceljs');
|
|
402
|
+
const workbook = new ExcelJS.Workbook();
|
|
403
|
+
const worksheet = workbook.addWorksheet(tableName);
|
|
404
|
+
|
|
405
|
+
worksheet.columns = columnNames.map(name => ({
|
|
406
|
+
header: name,
|
|
407
|
+
key: name
|
|
408
|
+
}));
|
|
409
|
+
|
|
410
|
+
const flatData = result.map(doc => {
|
|
411
|
+
const flatDoc: any = {};
|
|
412
|
+
columnNames.forEach(column => {
|
|
413
|
+
const value = doc[column];
|
|
414
|
+
if (value !== null && value !== undefined && typeof value === 'object' && !Array.isArray(value) && !(value instanceof Date)) {
|
|
415
|
+
flatDoc[column] = JSON.stringify(value);
|
|
416
|
+
} else {
|
|
417
|
+
flatDoc[column] = value;
|
|
418
|
+
}
|
|
419
|
+
});
|
|
420
|
+
return flatDoc;
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
worksheet.addRows(flatData);
|
|
424
|
+
|
|
425
|
+
await workbook.xlsx.writeFile(exportFile);
|
|
426
|
+
return exportFile;
|
|
427
|
+
} catch (error) {
|
|
428
|
+
console.error('MongoDB导出表数据到Excel失败:', error);
|
|
429
|
+
throw new Error(`导出表数据到Excel失败: ${error.message}`);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* MongoDB使用反引号作为标识符
|
|
435
|
+
*/
|
|
436
|
+
public quoteIdentifier(identifier: string): string {
|
|
437
|
+
return `\`${identifier}\``;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* ObjectId 类型定义
|
|
443
|
+
*/
|
|
444
|
+
class ObjectId {
|
|
445
|
+
constructor(id?: string) {
|
|
446
|
+
if (id) {
|
|
447
|
+
this.id = id;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
id?: string;
|
|
451
|
+
toString() {
|
|
452
|
+
return this.id || '';
|
|
453
|
+
}
|
|
454
|
+
}
|
|
@@ -47,20 +47,13 @@ export class OracleService extends BaseDatabaseService {
|
|
|
47
47
|
ORDER BY table_name
|
|
48
48
|
`, [database.toUpperCase()]);
|
|
49
49
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
rowCount: stats.rowCount,
|
|
58
|
-
dataSize: stats.dataSize,
|
|
59
|
-
indexSize: stats.indexSize
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return tablesWithStats;
|
|
50
|
+
return result.map((row: any) => ({
|
|
51
|
+
name: row.name,
|
|
52
|
+
type: row.type,
|
|
53
|
+
rowCount: undefined,
|
|
54
|
+
dataSize: undefined,
|
|
55
|
+
indexSize: undefined
|
|
56
|
+
}));
|
|
64
57
|
}
|
|
65
58
|
|
|
66
59
|
/**
|
|
@@ -39,9 +39,6 @@ export class PostgreSQLService extends BaseDatabaseService {
|
|
|
39
39
|
SELECT
|
|
40
40
|
t.table_name as name,
|
|
41
41
|
'BASE TABLE' as type,
|
|
42
|
-
(SELECT COUNT(*) FROM information_schema.columns WHERE table_name = t.table_name) as rowCount,
|
|
43
|
-
0 as dataSize,
|
|
44
|
-
0 as indexSize,
|
|
45
42
|
'' as collation,
|
|
46
43
|
obj_description(c.oid) as comment
|
|
47
44
|
FROM information_schema.tables t
|
|
@@ -53,11 +50,11 @@ export class PostgreSQLService extends BaseDatabaseService {
|
|
|
53
50
|
return result.map((row: any) => ({
|
|
54
51
|
name: row.name,
|
|
55
52
|
type: row.type,
|
|
56
|
-
rowCount:
|
|
57
|
-
dataSize:
|
|
58
|
-
indexSize:
|
|
53
|
+
rowCount: undefined,
|
|
54
|
+
dataSize: undefined,
|
|
55
|
+
indexSize: undefined,
|
|
59
56
|
collation: row.collation,
|
|
60
|
-
comment: row.comment
|
|
57
|
+
comment: row.comment || ''
|
|
61
58
|
}));
|
|
62
59
|
}
|
|
63
60
|
|