fdb2 1.0.39 → 1.0.41
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 +6 -5
- package/dist/public/.vite/manifest.json +109 -1
- package/dist/public/database-monitor.css +445 -0
- package/dist/public/database-monitor.js +605 -0
- package/dist/public/database.css +740 -0
- package/dist/public/database.js +555 -0
- package/dist/public/database2.js +344 -0
- package/dist/public/echarts.js +79307 -0
- package/dist/public/explorer.css +1109 -436
- package/dist/public/explorer.js +4049 -34891
- package/dist/public/export.js +54831 -0
- package/dist/public/helpers.js +27 -0
- package/dist/public/index.js +545 -51
- package/dist/public/layout.css +98 -65
- package/dist/public/layout.js +59 -22
- package/dist/public/query-history.css +651 -0
- package/dist/public/query-history.js +526 -0
- package/dist/public/sql-query-editor.css +503 -0
- package/dist/public/sql-query-editor.js +638 -0
- package/dist/public/theme.js +82 -0
- package/dist/scripts/verify-db-support.cjs +239 -0
- package/dist/server/__tests__/import-normalize.test.ts +79 -0
- package/dist/server/__tests__/remote-proxy.test.ts +62 -0
- package/dist/server/core/dialect.d.ts +9 -0
- package/dist/server/core/dialect.d.ts.map +1 -1
- package/dist/server/core/dialect.js +24 -9
- package/dist/server/core/dialect.js.map +1 -1
- package/dist/server/core/dialect.ts +29 -9
- package/dist/server/core/schema/__tests__/ddl-emitter.test.ts +159 -3
- package/dist/server/core/schema/canonical.d.ts.map +1 -1
- package/dist/server/core/schema/canonical.js +4 -1
- package/dist/server/core/schema/canonical.js.map +1 -1
- package/dist/server/core/schema/canonical.ts +4 -1
- package/dist/server/core/schema/ddl-emitter.d.ts +3 -0
- package/dist/server/core/schema/ddl-emitter.d.ts.map +1 -1
- package/dist/server/core/schema/ddl-emitter.js +158 -24
- package/dist/server/core/schema/ddl-emitter.js.map +1 -1
- package/dist/server/core/schema/ddl-emitter.ts +171 -22
- package/dist/server/index.d.ts +20 -0
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +375 -25
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.ts +392 -27
- package/dist/server/model/ddl-spec.d.ts +9 -0
- package/dist/server/model/ddl-spec.d.ts.map +1 -1
- package/dist/server/model/ddl-spec.ts +9 -0
- package/dist/server/service/connection.service.d.ts +8 -0
- package/dist/server/service/connection.service.d.ts.map +1 -1
- package/dist/server/service/connection.service.js +75 -17
- package/dist/server/service/connection.service.js.map +1 -1
- package/dist/server/service/connection.service.ts +74 -17
- package/dist/server/service/database/base.service.d.ts +45 -0
- package/dist/server/service/database/base.service.d.ts.map +1 -1
- package/dist/server/service/database/base.service.js +120 -7
- package/dist/server/service/database/base.service.js.map +1 -1
- package/dist/server/service/database/base.service.ts +143 -8
- package/dist/server/service/database/cockroachdb.service.d.ts +9 -0
- package/dist/server/service/database/cockroachdb.service.d.ts.map +1 -1
- package/dist/server/service/database/cockroachdb.service.js +68 -57
- package/dist/server/service/database/cockroachdb.service.js.map +1 -1
- package/dist/server/service/database/cockroachdb.service.ts +67 -58
- package/dist/server/service/database/database.service.d.ts +25 -227
- package/dist/server/service/database/database.service.d.ts.map +1 -1
- package/dist/server/service/database/database.service.js +323 -78
- package/dist/server/service/database/database.service.js.map +1 -1
- package/dist/server/service/database/database.service.ts +334 -87
- package/dist/server/service/database/mongodb.service.d.ts +58 -3
- package/dist/server/service/database/mongodb.service.d.ts.map +1 -1
- package/dist/server/service/database/mongodb.service.js +264 -54
- package/dist/server/service/database/mongodb.service.js.map +1 -1
- package/dist/server/service/database/mongodb.service.ts +288 -60
- package/dist/server/service/database/mssql.service.d.ts +8 -0
- package/dist/server/service/database/mssql.service.d.ts.map +1 -1
- package/dist/server/service/database/mssql.service.js +45 -40
- package/dist/server/service/database/mssql.service.js.map +1 -1
- package/dist/server/service/database/mssql.service.ts +49 -44
- package/dist/server/service/database/oracle.service.d.ts +29 -1
- package/dist/server/service/database/oracle.service.d.ts.map +1 -1
- package/dist/server/service/database/oracle.service.js +166 -137
- package/dist/server/service/database/oracle.service.js.map +1 -1
- package/dist/server/service/database/oracle.service.ts +166 -146
- package/dist/server/service/database/sap.service.d.ts +8 -0
- package/dist/server/service/database/sap.service.d.ts.map +1 -1
- package/dist/server/service/database/sap.service.js +28 -0
- package/dist/server/service/database/sap.service.js.map +1 -1
- package/dist/server/service/database/sap.service.ts +29 -0
- package/dist/server/service/database/sqlite.service.d.ts.map +1 -1
- package/dist/server/service/database/sqlite.service.js +6 -3
- package/dist/server/service/database/sqlite.service.js.map +1 -1
- package/dist/server/service/database/sqlite.service.ts +6 -3
- package/package.json +8 -8
- package/public/fdb2.png +0 -0
- package/scripts/verify-db-support.cjs +239 -0
|
@@ -3,10 +3,23 @@ import { BaseDatabaseService } from './base.service';
|
|
|
3
3
|
import { TableEntity, ColumnEntity, IndexEntity, ForeignKeyEntity } from '../../model/database.entity';
|
|
4
4
|
/**
|
|
5
5
|
* MongoDB数据库服务实现
|
|
6
|
-
* MongoDB 是一个 NoSQL
|
|
6
|
+
* MongoDB 是一个 NoSQL 文档数据库,与关系型数据库有很大不同。
|
|
7
|
+
*
|
|
8
|
+
* 重要:TypeORM 的 MongoDB 驱动不支持通过 dataSource.query() 执行 shell 命令字符串
|
|
9
|
+
* (如 `db.collection.find()`),那种写法永远无法工作。这里统一通过驱动持有的
|
|
10
|
+
* 原生 MongoClient(dataSource.driver.queryRunner.databaseConnection)直接操作,
|
|
11
|
+
* 这是唯一可靠的访问方式。
|
|
7
12
|
*/
|
|
8
13
|
export declare class MongoDBService extends BaseDatabaseService {
|
|
9
14
|
getDatabaseType(): string;
|
|
15
|
+
/**
|
|
16
|
+
* 获取驱动持有的原生 MongoClient
|
|
17
|
+
*/
|
|
18
|
+
private nativeClient;
|
|
19
|
+
/**
|
|
20
|
+
* 获取指定数据库的原生 Db 句柄
|
|
21
|
+
*/
|
|
22
|
+
private nativeDb;
|
|
10
23
|
/**
|
|
11
24
|
* 获取MongoDB数据库列表
|
|
12
25
|
*/
|
|
@@ -16,13 +29,39 @@ export declare class MongoDBService extends BaseDatabaseService {
|
|
|
16
29
|
*/
|
|
17
30
|
getTables(dataSource: DataSource, database: string): Promise<TableEntity[]>;
|
|
18
31
|
/**
|
|
19
|
-
* 获取MongoDB
|
|
32
|
+
* 获取MongoDB集合的字段信息(取有限样本推断字段并集)
|
|
20
33
|
*/
|
|
21
34
|
getColumns(dataSource: DataSource, database: string, table: string): Promise<ColumnEntity[]>;
|
|
22
35
|
/**
|
|
23
36
|
* 推断字段类型
|
|
24
37
|
*/
|
|
25
38
|
private inferType;
|
|
39
|
+
/**
|
|
40
|
+
* 过滤条件:接受 JSON 对象或 JSON 字符串(如 {"status":"active"})。
|
|
41
|
+
* SQL WHERE 一律明确拒绝——静默忽略会变成"查全表",比报错更危险。
|
|
42
|
+
*/
|
|
43
|
+
private parseFilter;
|
|
44
|
+
/** 排序:支持 {"name":1} / {"name":-1} / "name ASC, age DESC" */
|
|
45
|
+
private parseSort;
|
|
46
|
+
/** 24 位十六进制字符串转 ObjectId(否则按原值匹配,兼容非 ObjectId 主键) */
|
|
47
|
+
private toObjectId;
|
|
48
|
+
/** 过滤条件中的 _id 做 ObjectId 归一化(含 $in/$nin 数组与操作符对象) */
|
|
49
|
+
private normalizeId;
|
|
50
|
+
/**
|
|
51
|
+
* 获取集合数据(文档模式):原生 find + sort + skip + limit,不走 SQL。
|
|
52
|
+
*/
|
|
53
|
+
getTableData(dataSource: DataSource, databaseName: string, tableName: string, page?: number, pageSize?: number, where?: string | Record<string, any>, orderBy?: string | Record<string, any>): Promise<{
|
|
54
|
+
data: any[];
|
|
55
|
+
total: number;
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* 集合详情:在原生元数据(字段/索引)之上补 collStats(文档数/数据大小/索引大小)
|
|
59
|
+
*/
|
|
60
|
+
getTableInfo(dataSource: DataSource, databaseName: string, tableName: string): Promise<TableEntity>;
|
|
61
|
+
/**
|
|
62
|
+
* MongoDB 不支持 SQL:给出明确错误,而不是让驱动抛 "Queries aren't supported by MongoDB"
|
|
63
|
+
*/
|
|
64
|
+
executeQuery(_dataSource: DataSource, _sql: string): Promise<any>;
|
|
26
65
|
/**
|
|
27
66
|
* 获取MongoDB索引信息
|
|
28
67
|
*/
|
|
@@ -52,7 +91,7 @@ export declare class MongoDBService extends BaseDatabaseService {
|
|
|
52
91
|
*/
|
|
53
92
|
getProcedureDefinition(dataSource: DataSource, database: string, procedureName: string): Promise<string>;
|
|
54
93
|
/**
|
|
55
|
-
* 创建MongoDB
|
|
94
|
+
* 创建MongoDB数据库(通过创建再删除一个临时集合来物化数据库)
|
|
56
95
|
*/
|
|
57
96
|
createDatabase(dataSource: DataSource, databaseName: string, options?: any): Promise<void>;
|
|
58
97
|
/**
|
|
@@ -111,5 +150,21 @@ export declare class MongoDBService extends BaseDatabaseService {
|
|
|
111
150
|
* 创建表(集合)
|
|
112
151
|
*/
|
|
113
152
|
createTable(dataSource: DataSource, databaseName: string, table: any): Promise<void>;
|
|
153
|
+
/** 按 _id 更新文档:$set 语义,_id 本身不可修改 */
|
|
154
|
+
updateDocument(dataSource: DataSource, databaseName: string, tableName: string, docId: any, document: any): Promise<void>;
|
|
155
|
+
/** 按 _id 删除文档 */
|
|
156
|
+
deleteDocument(dataSource: DataSource, databaseName: string, tableName: string, docId: any): Promise<void>;
|
|
157
|
+
/** 插入单个文档,返回 insertedId */
|
|
158
|
+
insertDocument(dataSource: DataSource, databaseName: string, tableName: string, document: any): Promise<{
|
|
159
|
+
insertedId: any;
|
|
160
|
+
}>;
|
|
161
|
+
/** 清空集合(保留集合与索引),返回删除条数 */
|
|
162
|
+
truncateCollection(dataSource: DataSource, databaseName: string, tableName: string): Promise<number>;
|
|
163
|
+
/** 创建索引:keys 形如 { name: 1 } 或 { "a.b": -1 } */
|
|
164
|
+
createIndex(dataSource: DataSource, databaseName: string, tableName: string, keys: Record<string, any>, options?: any): Promise<string>;
|
|
165
|
+
/** 删除索引 */
|
|
166
|
+
dropIndex(dataSource: DataSource, databaseName: string, tableName: string, indexName: string): Promise<void>;
|
|
167
|
+
/** 聚合管道(聚合构建器 / 高级查询) */
|
|
168
|
+
aggregate(dataSource: DataSource, databaseName: string, tableName: string, pipeline: any[]): Promise<any[]>;
|
|
114
169
|
}
|
|
115
170
|
//# sourceMappingURL=mongodb.service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongodb.service.d.ts","sourceRoot":"","sources":["../../../../server/service/database/mongodb.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;
|
|
1
|
+
{"version":3,"file":"mongodb.service.d.ts","sourceRoot":"","sources":["../../../../server/service/database/mongodb.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAErC,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AACrD,OAAO,EAEL,WAAW,EACX,YAAY,EACZ,WAAW,EACX,gBAAgB,EACjB,MAAM,6BAA6B,CAAC;AAUrC;;;;;;;;GAQG;AACH,qBAAa,cAAe,SAAQ,mBAAmB;IAErD,eAAe;IAIf;;OAEG;IACH,OAAO,CAAC,YAAY;IAIpB;;OAEG;IACH,OAAO,CAAC,QAAQ;IAIhB;;OAEG;IACG,YAAY,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAW7D;;OAEG;IACG,SAAS,CAAC,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAmBjF;;OAEG;IACG,UAAU,CAAC,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC;IAoClG;;OAEG;IACH,OAAO,CAAC,SAAS;IA6BjB;;;OAGG;IACH,OAAO,CAAC,WAAW;IAiBnB,4DAA4D;IAC5D,OAAO,CAAC,SAAS;IAyBjB,qDAAqD;IACrD,OA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AK,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,GACzF,OAAO,CAAC,IAAI,CAAC;IAMhB,iBAAiB;IACX,cAAc,CAClB,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAC1E,OAAO,CAAC,IAAI,CAAC;IAKhB,2BAA2B;IACrB,cAAc,CAClB,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,GAC7E,OAAO,CAAC;QAAE,UAAU,EAAE,GAAG,CAAA;KAAE,CAAC;IAM/B,2BAA2B;IACrB,kBAAkB,CACtB,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAC9D,OAAO,CAAC,MAAM,CAAC;IAMlB,+CAA+C;IACzC,WAAW,CACf,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAC/D,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,GAAG,GACvC,OAAO,CAAC,MAAM,CAAC;IAKlB,WAAW;IACL,SAAS,CACb,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GACjF,OAAO,CAAC,IAAI,CAAC;IAKhB,yBAAyB;IACnB,SAAS,CACb,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,EAAE,GAC/E,OAAO,CAAC,GAAG,EAAE,CAAC;CAIlB"}
|
|
@@ -34,24 +34,48 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.MongoDBService = void 0;
|
|
37
|
+
const mongodb_1 = require("mongodb");
|
|
37
38
|
const base_service_1 = require("./base.service");
|
|
38
39
|
const fs = __importStar(require("fs"));
|
|
39
40
|
const path = __importStar(require("path"));
|
|
41
|
+
/**
|
|
42
|
+
* MongoDB 数据库只有在拥有至少一个集合后才会被持久化并出现在 listDatabases 中。
|
|
43
|
+
* 创建空库时保留一个占位集合作为"存在证明",并在 getTables 中过滤掉它,避免暴露垃圾集合。
|
|
44
|
+
*/
|
|
45
|
+
const DB_INIT_MARKER = '__db_tool_init';
|
|
40
46
|
/**
|
|
41
47
|
* MongoDB数据库服务实现
|
|
42
|
-
* MongoDB 是一个 NoSQL
|
|
48
|
+
* MongoDB 是一个 NoSQL 文档数据库,与关系型数据库有很大不同。
|
|
49
|
+
*
|
|
50
|
+
* 重要:TypeORM 的 MongoDB 驱动不支持通过 dataSource.query() 执行 shell 命令字符串
|
|
51
|
+
* (如 `db.collection.find()`),那种写法永远无法工作。这里统一通过驱动持有的
|
|
52
|
+
* 原生 MongoClient(dataSource.driver.queryRunner.databaseConnection)直接操作,
|
|
53
|
+
* 这是唯一可靠的访问方式。
|
|
43
54
|
*/
|
|
44
55
|
class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
45
56
|
getDatabaseType() {
|
|
46
57
|
return 'mongodb';
|
|
47
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* 获取驱动持有的原生 MongoClient
|
|
61
|
+
*/
|
|
62
|
+
nativeClient(dataSource) {
|
|
63
|
+
return dataSource.driver.queryRunner.databaseConnection;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* 获取指定数据库的原生 Db 句柄
|
|
67
|
+
*/
|
|
68
|
+
nativeDb(dataSource, database) {
|
|
69
|
+
return this.nativeClient(dataSource).db(database);
|
|
70
|
+
}
|
|
48
71
|
/**
|
|
49
72
|
* 获取MongoDB数据库列表
|
|
50
73
|
*/
|
|
51
74
|
async getDatabases(dataSource) {
|
|
52
75
|
try {
|
|
53
|
-
const
|
|
54
|
-
return
|
|
76
|
+
const res = await this.nativeDb(dataSource, 'admin').admin().listDatabases();
|
|
77
|
+
return (res.databases || []).map((db) => db.name)
|
|
78
|
+
.filter((name) => name !== 'admin' && name !== 'local' && name !== 'config');
|
|
55
79
|
}
|
|
56
80
|
catch (error) {
|
|
57
81
|
console.error('获取MongoDB数据库列表失败:', error);
|
|
@@ -63,10 +87,13 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
63
87
|
*/
|
|
64
88
|
async getTables(dataSource, database) {
|
|
65
89
|
try {
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
90
|
+
const db = this.nativeDb(dataSource, database);
|
|
91
|
+
const collections = await db.listCollections().toArray();
|
|
92
|
+
return collections
|
|
93
|
+
.filter((c) => c.name !== DB_INIT_MARKER)
|
|
94
|
+
.map((c) => ({
|
|
95
|
+
name: c.name,
|
|
96
|
+
type: c.type === 'view' ? 'view' : 'collection',
|
|
70
97
|
rowCount: undefined,
|
|
71
98
|
dataSize: undefined,
|
|
72
99
|
indexSize: undefined
|
|
@@ -78,18 +105,32 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
78
105
|
}
|
|
79
106
|
}
|
|
80
107
|
/**
|
|
81
|
-
* 获取MongoDB
|
|
108
|
+
* 获取MongoDB集合的字段信息(取有限样本推断字段并集)
|
|
82
109
|
*/
|
|
83
110
|
async getColumns(dataSource, database, table) {
|
|
84
111
|
try {
|
|
85
|
-
const
|
|
86
|
-
|
|
112
|
+
const db = this.nativeDb(dataSource, database);
|
|
113
|
+
// 文档可以有不同的字段集;只取一条样本会让后插入的字段在表结构和跨库同步里消失。
|
|
114
|
+
const samples = await db.collection(table).find().limit(100).toArray();
|
|
115
|
+
if (samples.length === 0) {
|
|
87
116
|
return [];
|
|
88
117
|
}
|
|
89
|
-
const fields =
|
|
118
|
+
const fields = [];
|
|
119
|
+
const types = new Map();
|
|
120
|
+
for (const sample of samples) {
|
|
121
|
+
for (const field of Object.keys(sample)) {
|
|
122
|
+
const inferred = this.inferType(sample[field]);
|
|
123
|
+
const current = types.get(field);
|
|
124
|
+
if (!current)
|
|
125
|
+
fields.push(field);
|
|
126
|
+
if (!current || current === 'null' || current === 'unknown') {
|
|
127
|
+
types.set(field, inferred);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
90
131
|
return fields.map(field => ({
|
|
91
132
|
name: field,
|
|
92
|
-
type:
|
|
133
|
+
type: types.get(field) || 'unknown',
|
|
93
134
|
nullable: true,
|
|
94
135
|
defaultValue: undefined,
|
|
95
136
|
isPrimary: field === '_id',
|
|
@@ -128,7 +169,7 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
128
169
|
else if (value instanceof Buffer) {
|
|
129
170
|
return 'binData';
|
|
130
171
|
}
|
|
131
|
-
else if (value instanceof ObjectId) {
|
|
172
|
+
else if (value instanceof mongodb_1.ObjectId) {
|
|
132
173
|
return 'objectId';
|
|
133
174
|
}
|
|
134
175
|
else {
|
|
@@ -137,13 +178,150 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
137
178
|
}
|
|
138
179
|
return 'unknown';
|
|
139
180
|
}
|
|
181
|
+
// ---------------------------------------------------------------- 文档查询辅助
|
|
182
|
+
/**
|
|
183
|
+
* 过滤条件:接受 JSON 对象或 JSON 字符串(如 {"status":"active"})。
|
|
184
|
+
* SQL WHERE 一律明确拒绝——静默忽略会变成"查全表",比报错更危险。
|
|
185
|
+
*/
|
|
186
|
+
parseFilter(where) {
|
|
187
|
+
if (!where)
|
|
188
|
+
return {};
|
|
189
|
+
if (typeof where === 'object')
|
|
190
|
+
return where;
|
|
191
|
+
if (typeof where === 'string') {
|
|
192
|
+
const t = where.trim();
|
|
193
|
+
if (!t)
|
|
194
|
+
return {};
|
|
195
|
+
try {
|
|
196
|
+
const parsed = JSON.parse(t);
|
|
197
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed))
|
|
198
|
+
return parsed;
|
|
199
|
+
}
|
|
200
|
+
catch {
|
|
201
|
+
throw new Error('MongoDB 过滤条件必须是 JSON(如 {"status":"active"}),不支持 SQL WHERE');
|
|
202
|
+
}
|
|
203
|
+
throw new Error('MongoDB 过滤条件必须是 JSON 对象(如 {"status":"active"})');
|
|
204
|
+
}
|
|
205
|
+
return {};
|
|
206
|
+
}
|
|
207
|
+
/** 排序:支持 {"name":1} / {"name":-1} / "name ASC, age DESC" */
|
|
208
|
+
parseSort(orderBy) {
|
|
209
|
+
if (!orderBy)
|
|
210
|
+
return undefined;
|
|
211
|
+
if (typeof orderBy === 'object') {
|
|
212
|
+
const out = {};
|
|
213
|
+
for (const k of Object.keys(orderBy))
|
|
214
|
+
out[k] = Number(orderBy[k]) < 0 ? -1 : 1;
|
|
215
|
+
return Object.keys(out).length ? out : undefined;
|
|
216
|
+
}
|
|
217
|
+
const t = orderBy.trim();
|
|
218
|
+
if (!t)
|
|
219
|
+
return undefined;
|
|
220
|
+
if (t.startsWith('{')) {
|
|
221
|
+
try {
|
|
222
|
+
return this.parseSort(JSON.parse(t));
|
|
223
|
+
}
|
|
224
|
+
catch {
|
|
225
|
+
throw new Error('MongoDB 排序条件必须是 JSON(如 {"name":1})');
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
const out = {};
|
|
229
|
+
for (const part of t.split(',')) {
|
|
230
|
+
const seg = part.trim().split(/\s+/).filter(Boolean);
|
|
231
|
+
if (!seg.length)
|
|
232
|
+
continue;
|
|
233
|
+
out[seg[0]] = /desc/i.test(seg[1] || '') ? -1 : 1;
|
|
234
|
+
}
|
|
235
|
+
return Object.keys(out).length ? out : undefined;
|
|
236
|
+
}
|
|
237
|
+
/** 24 位十六进制字符串转 ObjectId(否则按原值匹配,兼容非 ObjectId 主键) */
|
|
238
|
+
toObjectId(id) {
|
|
239
|
+
if (id instanceof mongodb_1.ObjectId)
|
|
240
|
+
return id;
|
|
241
|
+
if (typeof id === 'string' && /^[0-9a-fA-F]{24}$/.test(id)) {
|
|
242
|
+
try {
|
|
243
|
+
return new mongodb_1.ObjectId(id);
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
return id;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return id;
|
|
250
|
+
}
|
|
251
|
+
/** 过滤条件中的 _id 做 ObjectId 归一化(含 $in/$nin 数组与操作符对象) */
|
|
252
|
+
normalizeId(filter) {
|
|
253
|
+
if (!filter || typeof filter !== 'object')
|
|
254
|
+
return filter;
|
|
255
|
+
const out = { ...filter };
|
|
256
|
+
if (out._id === undefined)
|
|
257
|
+
return out;
|
|
258
|
+
const conv = (v) => {
|
|
259
|
+
if (Array.isArray(v))
|
|
260
|
+
return v.map(conv);
|
|
261
|
+
if (v && typeof v === 'object') {
|
|
262
|
+
const r = {};
|
|
263
|
+
for (const k of Object.keys(v))
|
|
264
|
+
r[k] = conv(v[k]);
|
|
265
|
+
return r;
|
|
266
|
+
}
|
|
267
|
+
return this.toObjectId(v);
|
|
268
|
+
};
|
|
269
|
+
out._id = conv(out._id);
|
|
270
|
+
return out;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* 获取集合数据(文档模式):原生 find + sort + skip + limit,不走 SQL。
|
|
274
|
+
*/
|
|
275
|
+
async getTableData(dataSource, databaseName, tableName, page = 1, pageSize = 100, where, orderBy) {
|
|
276
|
+
try {
|
|
277
|
+
const col = this.nativeDb(dataSource, databaseName).collection(tableName);
|
|
278
|
+
const filter = this.normalizeId(this.parseFilter(where));
|
|
279
|
+
const sort = this.parseSort(orderBy);
|
|
280
|
+
const total = await col.countDocuments(filter);
|
|
281
|
+
const cursor = col.find(filter);
|
|
282
|
+
if (sort)
|
|
283
|
+
cursor.sort(sort);
|
|
284
|
+
const data = await cursor
|
|
285
|
+
.skip(Math.max(0, (page - 1) * pageSize))
|
|
286
|
+
.limit(pageSize)
|
|
287
|
+
.toArray();
|
|
288
|
+
return { data, total };
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
console.error('获取MongoDB集合数据失败:', error);
|
|
292
|
+
throw new Error(`获取集合数据失败: ${error instanceof Error ? error.message : String(error)}`);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* 集合详情:在原生元数据(字段/索引)之上补 collStats(文档数/数据大小/索引大小)
|
|
297
|
+
*/
|
|
298
|
+
async getTableInfo(dataSource, databaseName, tableName) {
|
|
299
|
+
const table = await super.getTableInfo(dataSource, databaseName, tableName);
|
|
300
|
+
try {
|
|
301
|
+
const stats = await this.nativeDb(dataSource, databaseName).command({ collStats: tableName });
|
|
302
|
+
table.rowCount = stats?.count;
|
|
303
|
+
table.dataSize = stats?.size;
|
|
304
|
+
table.indexSize = stats?.totalIndexSize;
|
|
305
|
+
}
|
|
306
|
+
catch {
|
|
307
|
+
// collStats 可能因权限不足失败:保持 undefined,不影响主流程
|
|
308
|
+
}
|
|
309
|
+
return table;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* MongoDB 不支持 SQL:给出明确错误,而不是让驱动抛 "Queries aren't supported by MongoDB"
|
|
313
|
+
*/
|
|
314
|
+
async executeQuery(_dataSource, _sql) {
|
|
315
|
+
throw new Error('MongoDB 不支持 SQL 查询,请使用文档查询(JSON 过滤条件)');
|
|
316
|
+
}
|
|
140
317
|
/**
|
|
141
318
|
* 获取MongoDB索引信息
|
|
142
319
|
*/
|
|
143
320
|
async getIndexes(dataSource, database, table) {
|
|
144
321
|
try {
|
|
145
|
-
const
|
|
146
|
-
|
|
322
|
+
const db = this.nativeDb(dataSource, database);
|
|
323
|
+
const indexes = await db.collection(table).indexes();
|
|
324
|
+
return indexes.map((index) => ({
|
|
147
325
|
name: index.name,
|
|
148
326
|
type: index.unique ? 'UNIQUE' : 'INDEX',
|
|
149
327
|
columns: Object.keys(index.key),
|
|
@@ -166,8 +344,9 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
166
344
|
*/
|
|
167
345
|
async getDatabaseSize(dataSource, database) {
|
|
168
346
|
try {
|
|
169
|
-
const
|
|
170
|
-
|
|
347
|
+
const db = this.nativeDb(dataSource, database);
|
|
348
|
+
const stats = await db.stats();
|
|
349
|
+
return stats.dataSize || 0;
|
|
171
350
|
}
|
|
172
351
|
catch (error) {
|
|
173
352
|
console.error('获取MongoDB数据库大小失败:', error);
|
|
@@ -199,12 +378,17 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
199
378
|
throw new Error('MongoDB不支持存储过程');
|
|
200
379
|
}
|
|
201
380
|
/**
|
|
202
|
-
* 创建MongoDB
|
|
381
|
+
* 创建MongoDB数据库(通过创建再删除一个临时集合来物化数据库)
|
|
203
382
|
*/
|
|
204
383
|
async createDatabase(dataSource, databaseName, options) {
|
|
205
384
|
try {
|
|
206
|
-
|
|
207
|
-
|
|
385
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
386
|
+
// MongoDB 没有"空数据库"的概念:数据库只有在拥有至少一个集合后才会被持久化,
|
|
387
|
+
// 并出现在 listDatabases 中。因此保留一个占位集合,否则刚创建的库在 getDatabases 中不可见。
|
|
388
|
+
const existing = await db.listCollections({ name: DB_INIT_MARKER }).toArray();
|
|
389
|
+
if (existing.length === 0) {
|
|
390
|
+
await db.createCollection(DB_INIT_MARKER);
|
|
391
|
+
}
|
|
208
392
|
}
|
|
209
393
|
catch (error) {
|
|
210
394
|
console.error('创建MongoDB数据库失败:', error);
|
|
@@ -216,7 +400,7 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
216
400
|
*/
|
|
217
401
|
async dropDatabase(dataSource, databaseName) {
|
|
218
402
|
try {
|
|
219
|
-
await
|
|
403
|
+
await this.nativeDb(dataSource, databaseName).dropDatabase();
|
|
220
404
|
}
|
|
221
405
|
catch (error) {
|
|
222
406
|
console.error('删除MongoDB数据库失败:', error);
|
|
@@ -254,8 +438,8 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
254
438
|
*/
|
|
255
439
|
async viewLogs(dataSource, database, limit = 100) {
|
|
256
440
|
try {
|
|
257
|
-
const
|
|
258
|
-
return
|
|
441
|
+
const res = await this.nativeDb(dataSource, 'admin').admin().command({ getLog: 'global' });
|
|
442
|
+
return (res.log || []).slice(0, limit);
|
|
259
443
|
}
|
|
260
444
|
catch (error) {
|
|
261
445
|
console.error('获取MongoDB日志失败:', error);
|
|
@@ -273,6 +457,7 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
273
457
|
}
|
|
274
458
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
275
459
|
const backupFile = path.join(backupPath, `${databaseName}_${timestamp}.json`);
|
|
460
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
276
461
|
const collections = await this.getTables(dataSource, databaseName);
|
|
277
462
|
const backupData = {
|
|
278
463
|
database: databaseName,
|
|
@@ -280,8 +465,8 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
280
465
|
collections: {}
|
|
281
466
|
};
|
|
282
467
|
for (const collection of collections) {
|
|
283
|
-
const
|
|
284
|
-
backupData.collections[collection.name] =
|
|
468
|
+
const docs = await db.collection(collection.name).find().toArray();
|
|
469
|
+
backupData.collections[collection.name] = docs;
|
|
285
470
|
}
|
|
286
471
|
fs.writeFileSync(backupFile, JSON.stringify(backupData, null, 2), 'utf8');
|
|
287
472
|
return `备份成功:${backupFile}`;
|
|
@@ -297,10 +482,11 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
297
482
|
async restoreDatabase(dataSource, databaseName, filePath, options) {
|
|
298
483
|
try {
|
|
299
484
|
const backupData = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
485
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
300
486
|
for (const collectionName in backupData.collections) {
|
|
301
487
|
const documents = backupData.collections[collectionName];
|
|
302
488
|
if (documents.length > 0) {
|
|
303
|
-
await
|
|
489
|
+
await db.collection(collectionName).insertMany(documents);
|
|
304
490
|
}
|
|
305
491
|
}
|
|
306
492
|
}
|
|
@@ -326,7 +512,8 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
326
512
|
}
|
|
327
513
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
328
514
|
const exportFile = path.join(exportPath, `${tableName}_data_${timestamp}.csv`);
|
|
329
|
-
const
|
|
515
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
516
|
+
const result = await db.collection(tableName).find().toArray();
|
|
330
517
|
if (result.length === 0) {
|
|
331
518
|
fs.writeFileSync(exportFile, '', 'utf8');
|
|
332
519
|
return exportFile;
|
|
@@ -382,7 +569,8 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
382
569
|
}
|
|
383
570
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
384
571
|
const exportFile = path.join(exportPath, `${tableName}_data_${timestamp}.json`);
|
|
385
|
-
const
|
|
572
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
573
|
+
const result = await db.collection(tableName).find().toArray();
|
|
386
574
|
fs.writeFileSync(exportFile, JSON.stringify(result, null, 2), 'utf8');
|
|
387
575
|
return exportFile;
|
|
388
576
|
}
|
|
@@ -402,7 +590,8 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
402
590
|
}
|
|
403
591
|
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
|
404
592
|
const exportFile = path.join(exportPath, `${tableName}_data_${timestamp}.xlsx`);
|
|
405
|
-
const
|
|
593
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
594
|
+
const result = await db.collection(tableName).find().toArray();
|
|
406
595
|
const columns = await this.getColumns(dataSource, databaseName, tableName);
|
|
407
596
|
const columnNames = columns.map(column => column.name);
|
|
408
597
|
const ExcelJS = require('exceljs');
|
|
@@ -446,49 +635,70 @@ class MongoDBService extends base_service_1.BaseDatabaseService {
|
|
|
446
635
|
async bulkInsert(dataSource, databaseName, tableName, data) {
|
|
447
636
|
if (data.length === 0)
|
|
448
637
|
return;
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
await repository.insertMany(data);
|
|
638
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
639
|
+
await db.collection(tableName).insertMany(data);
|
|
452
640
|
}
|
|
453
641
|
/**
|
|
454
642
|
* 插入单条数据
|
|
455
643
|
*/
|
|
456
644
|
async insertData(dataSource, databaseName, tableName, data) {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
await repository.insertOne(data);
|
|
645
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
646
|
+
await db.collection(tableName).insertOne(data);
|
|
460
647
|
}
|
|
461
648
|
/**
|
|
462
649
|
* 删除表(集合)
|
|
463
650
|
*/
|
|
464
651
|
async dropTable(dataSource, databaseName, tableName) {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
await repository.deleteMany({});
|
|
652
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
653
|
+
await db.collection(tableName).drop();
|
|
468
654
|
}
|
|
469
655
|
/**
|
|
470
656
|
* 创建表(集合)
|
|
471
657
|
*/
|
|
472
658
|
async createTable(dataSource, databaseName, table) {
|
|
473
659
|
const { name } = table;
|
|
474
|
-
|
|
475
|
-
await
|
|
476
|
-
await this.dropTable(dataSource, databaseName, name);
|
|
660
|
+
const db = this.nativeDb(dataSource, databaseName);
|
|
661
|
+
await db.createCollection(name);
|
|
477
662
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
663
|
+
// ---------------------------------------------------------------- 文档级操作(document 模式)
|
|
664
|
+
/** 按 _id 更新文档:$set 语义,_id 本身不可修改 */
|
|
665
|
+
async updateDocument(dataSource, databaseName, tableName, docId, document) {
|
|
666
|
+
const col = this.nativeDb(dataSource, databaseName).collection(tableName);
|
|
667
|
+
const { _id, ...rest } = document || {};
|
|
668
|
+
await col.updateOne({ _id: this.toObjectId(docId) }, { $set: rest });
|
|
669
|
+
}
|
|
670
|
+
/** 按 _id 删除文档 */
|
|
671
|
+
async deleteDocument(dataSource, databaseName, tableName, docId) {
|
|
672
|
+
const col = this.nativeDb(dataSource, databaseName).collection(tableName);
|
|
673
|
+
await col.deleteOne({ _id: this.toObjectId(docId) });
|
|
674
|
+
}
|
|
675
|
+
/** 插入单个文档,返回 insertedId */
|
|
676
|
+
async insertDocument(dataSource, databaseName, tableName, document) {
|
|
677
|
+
const col = this.nativeDb(dataSource, databaseName).collection(tableName);
|
|
678
|
+
const res = await col.insertOne(document);
|
|
679
|
+
return { insertedId: res.insertedId };
|
|
680
|
+
}
|
|
681
|
+
/** 清空集合(保留集合与索引),返回删除条数 */
|
|
682
|
+
async truncateCollection(dataSource, databaseName, tableName) {
|
|
683
|
+
const col = this.nativeDb(dataSource, databaseName).collection(tableName);
|
|
684
|
+
const res = await col.deleteMany({});
|
|
685
|
+
return res.deletedCount ?? 0;
|
|
488
686
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
687
|
+
/** 创建索引:keys 形如 { name: 1 } 或 { "a.b": -1 } */
|
|
688
|
+
async createIndex(dataSource, databaseName, tableName, keys, options) {
|
|
689
|
+
const col = this.nativeDb(dataSource, databaseName).collection(tableName);
|
|
690
|
+
return col.createIndex(keys, options || {});
|
|
691
|
+
}
|
|
692
|
+
/** 删除索引 */
|
|
693
|
+
async dropIndex(dataSource, databaseName, tableName, indexName) {
|
|
694
|
+
const col = this.nativeDb(dataSource, databaseName).collection(tableName);
|
|
695
|
+
await col.dropIndex(indexName);
|
|
696
|
+
}
|
|
697
|
+
/** 聚合管道(聚合构建器 / 高级查询) */
|
|
698
|
+
async aggregate(dataSource, databaseName, tableName, pipeline) {
|
|
699
|
+
const col = this.nativeDb(dataSource, databaseName).collection(tableName);
|
|
700
|
+
return col.aggregate(Array.isArray(pipeline) ? pipeline : []).toArray();
|
|
492
701
|
}
|
|
493
702
|
}
|
|
703
|
+
exports.MongoDBService = MongoDBService;
|
|
494
704
|
//# sourceMappingURL=mongodb.service.js.map
|