Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

feat(import): support DataGrip Kingbase and groups

t8y2b8c488b39855
diff --git a/apps/desktop/src/lib/__tests__/imports/datagripImport.spec.ts b/apps/desktop/src/lib/__tests__/imports/datagripImport.spec.ts
new file mode 100644
index 0000000000..9be188be12
--- /dev/null
+++ b/apps/desktop/src/lib/__tests__/imports/datagripImport.spec.ts
@@ -0,0 +1,132 @@
+// @vitest-environment happy-dom
+
+import { describe, expect, it } from "vitest";
+import type { SidebarLayout, SidebarOrderEntry } from "@/types/database";
+import { parseDataGripConnections, parseDataGripImport, type DataGripImportPayload } from "@/lib/imports/datagripImport";
+
+function payload(dataSources: string, dataSourcesLocal?: string, dbForestConfig?: string): DataGripImportPayload {
+ return { format: "datagrip-import", dataSources, dataSourcesLocal, dbForestConfig };
+}
+
+function layoutLabels(layout: SidebarLayout, connectionNames: Map<string, string>): unknown[] {
+ const groupNames = new Map(layout.groups.map((group) => [group.id, group.name]));
+ const visit = (entries: SidebarOrderEntry[]): unknown[] => entries.map((entry) => (entry.type === "connection" ? connectionNames.get(entry.id) : { group: groupNames.get(entry.id), children: visit(entry.children ?? []) }));
+ return visit(layout.order);
+}
+
+describe("DataGrip connection import", () => {
+ it("recognizes Kingbase custom JDBC drivers", () => {
+ const connections = parseDataGripConnections(
+ payload(`
+ <project>
+ <component name="DataSourceManagerImpl">
+ <data-source name="Kingbase V8R6" uuid="kingbase-1">
+ <driver-ref>java.sql.Driver</driver-ref>
+ <jdbc-driver>com.kingbase8.Driver</jdbc-driver>
+ <jdbc-url>jdbc:kingbase8://192.168.31.87:54321/test</jdbc-url>
+ </data-source>
+ </component>
+ </project>
+ `),
+ );
+
+ expect(connections).toHaveLength(1);
+ expect(connections[0]).toMatchObject({
+ name: "Kingbase V8R6",
+ db_type: "kingbase",
+ driver_profile: "kingbase",
+ driver_label: "KingbaseES",
+ host: "192.168.31.87",
+ port: 54321,
+ database: "test",
+ username: "SYSTEM",
+ });
+ });
+
+ it("preserves DataGrip connection groups as sidebar groups", () => {
+ const result = parseDataGripImport(
+ payload(
+ `
+ <project>
+ <component name="DataSourceManagerImpl">
+ <data-source name="Production" uuid="mysql-prod">
+ <driver-ref>mysql</driver-ref>
+ <jdbc-url>jdbc:mysql://prod.example.com:3306/app</jdbc-url>
+ </data-source>
+ <data-source name="Development" uuid="mysql-dev">
+ <driver-ref>mysql</driver-ref>
+ <jdbc-url>jdbc:mysql://dev.example.com:3306/app</jdbc-url>
+ </data-source>
+ <data-source name="Ungrouped" uuid="mysql-root">
+ <driver-ref>mysql</driver-ref>
+ <jdbc-url>jdbc:mysql://localhost:3306/app</jdbc-url>
+ </data-source>
+ </component>
+ </project>
+ `,
+ undefined,
+ `
+ <project>
+ <component name="db-forest-configuration">
+ <data version="2">.
+ 1:0:group-environment:Environment
+ 2:1:group-production:Production
+ 3:1:group-development:Development
+ ----------------------------------------
+ 4:2:mysql-prod
+ 5:3:mysql-dev
+ 6:0:mysql-root
+ .</data>
+ </component>
+ </project>
+ `,
+ ),
+ );
+
+ const names = new Map(result.connections.map((connection) => [connection.id, connection.name]));
+ expect(layoutLabels(result.layout!, names)).toEqual([
+ {
+ group: "Environment",
+ children: [
+ { group: "Production", children: ["Production"] },
+ { group: "Development", children: ["Development"] },
+ ],
+ },
+ "Ungrouped",
+ ]);
+ });
+
+ it("keeps legacy group-name imports compatible", () => {
+ const result = parseDataGripImport(
+ payload(`
+ <project>
+ <component name="DataSourceManagerImpl">
+ <data-source name="Legacy" uuid="mysql-legacy" group-name="Legacy Group">
+ <driver-ref>mysql</driver-ref>
+ <jdbc-url>jdbc:mysql://localhost:3306/legacy</jdbc-url>
+ </data-source>
+ </component>
+ </project>
+ `),
+ );
+
+ const names = new Map(result.connections.map((connection) => [connection.id, connection.name]));
+ expect(layoutLabels(result.layout!, names)).toEqual([{ group: "Legacy Group", children: ["Legacy"] }]);
+ });
+
+ it("keeps the connection-only API and empty layout behavior", () => {
+ const importPayload = payload(`
+ <project>
+ <component name="DataSourceManagerImpl">
+ <data-source name="PostgreSQL" uuid="postgres-1">
+ <driver-ref>postgresql</driver-ref>
+ <jdbc-url>jdbc:postgresql://localhost:5432/postgres</jdbc-url>
+ </data-source>
+ </component>
+ </project>
+ `);
+
+ expect(parseDataGripConnections(importPayload)).toHaveLength(1);
+ expect(parseDataGripImport(importPayload).layout).toBeUndefined();
+ });
+});
diff --git a/apps/desktop/src/lib/imports/datagripImport.ts b/apps/desktop/src/lib/imports/datagripImport.ts
index 9562d4a2ed..ec1a20ec5d 100644
--- a/apps/desktop/src/lib/imports/datagripImport.ts
+++ b/apps/desktop/src/lib/imports/datagripImport.ts
@@ -1,14 +1,21 @@
-import type { ConnectionConfig, DatabaseType } from "@/types/database";
+import type { ConnectionConfig, DatabaseType, SidebarLayout } from "@/types/database";
import { uuid } from "@/lib/common/utils";
+import { buildSidebarLayoutFromFolderPaths } from "@/lib/sidebar/sidebarLayout";
declare var process: { env: Record<string, string | undefined> } | undefined;
type PartialConnection = Omit<ConnectionConfig, "id">;
+export type DataGripImportResult = {
+ connections: ConnectionConfig[];
+ layout?: SidebarLayout;
+};
+
export type DataGripImportPayload = {
format: "datagrip-import";
dataSources: string;
dataSourcesLocal?: string;
+ dbForestConfig?: string;
};
type DataSourceFragment = {
@@ -54,6 +61,8 @@ const driverRefMap: Record<string, DriverProfile> = {
elasticsearch: { dbType: "elasticsearch", profile: "elasticsearch", label: "Elasticsearch", port: 9200, user: "" },
h2: { dbType: "h2", profile: "h2", label: "H2", port: 9092, user: "sa" },
snowflake: { dbType: "snowflake", profile: "snowflake", label: "Snowflake", port: 443, user: "" },
+ kingbase: { dbType: "kingbase", profile: "kingbase", label: "KingbaseES", port: 54321, user: "SYSTEM" },
+ kingbase8: { dbType: "kingbase", profile: "kingbase", label: "KingbaseES", port: 54321, user: "SYSTEM" },
};
// product name from <database-info product="..."> → dbx profile
@@ -74,6 +83,7 @@ const productMap: Record<string, DriverProfile> = {
redshift: { dbType: "redshift", profile: "redshift", label: "Redshift", port: 5439, user: "awsuser" },
elasticsearch: { dbType: "elasticsearch", profile: "elasticsearch", label: "Elasticsearch", port: 9200, user: "" },
snowflake: { dbType: "snowflake", profile: "snowflake", label: "Snowflake", port: 443, user: "" },
+ kingbase: { dbType: "kingbase", profile: "kingbase", label: "KingbaseES", port: 54321, user: "SYSTEM" },
};
// JDBC subprotocol → dbx profile (fallback when driver-ref and product are unknown)
@@ -92,6 +102,8 @@ const subprotocolMap: Record<string, DriverProfile> = {
duckdb: { dbType: "duckdb", profile: "duckdb", label: "DuckDB", port: 0, user: "" },
bigquery: { dbType: "bigquery", profile: "bigquery", label: "BigQuery", port: 443, user: "" },
redshift: { dbType: "redshift", profile: "redshift", label: "Redshift", port: 5439, user: "awsuser" },
+ kingbase: { dbType: "kingbase", profile: "kingbase", label: "KingbaseES", port: 54321, user: "SYSTEM" },
+ kingbase8: { dbType: "kingbase", profile: "kingbase", label: "KingbaseES", port: 54321, user: "SYSTEM" },
};
function getNumber(value: string | undefined): number {
@@ -248,6 +260,7 @@ function inferProfile(driverRef: string, subprotocol: string, driverClass: strin
if (classLower.includes("mongo")) return driverRefMap.mongodb;
if (classLower.includes("redis")) return driverRefMap.redis;
if (classLower.includes("clickhouse")) return driverRefMap.clickhouse;
+ if (classLower.includes("kingbase")) return driverRefMap.kingbase;
// 5. Fallback to JDBC
return { dbType: "jdbc", profile: "jdbc", label: driverClass || "JDBC", port: 0, user: "" };
@@ -322,6 +335,59 @@ function parseDataSourcesLocalXml(xml: string): Map<string, Partial<DataSourceFr
return result;
}
+function parseDbForestGroupPaths(xml: string): Map<string, string> {
+ const doc = new DOMParser().parseFromString(xml, "application/xml");
+ if (doc.querySelector("parsererror")) return new Map();
+
+ const data = doc.getElementsByTagName("data")[0]?.textContent;
+ if (!data) return new Map();
+
+ const groupRecords = new Map<string, { parentId: string; name: string }>();
+ const connectionRecords: Array<{ uuid: string; parentId: string }> = [];
+ let readingConnections = false;
+
+ for (const rawLine of data.split(/\r?\n/)) {
+ const line = rawLine.trim();
+ if (!line || line === ".") continue;
+ if (/^-{8,}$/.test(line)) {
+ readingConnections = true;
+ continue;
+ }
+
+ const match = line.match(/^(\d+):(\d+):([^:]+)(?::(.*))?$/);
+ if (!match) continue;
+ const [, recordId, parentId, uuidVal, rawName] = match;
+ if (!readingConnections && rawName) {
+ const name = rawName.replace(/^[\u200B\uFEFF]+/, "").trim();
+ if (name) groupRecords.set(recordId, { parentId, name });
+ } else if (readingConnections) {
+ connectionRecords.push({ uuid: uuidVal, parentId });
+ }
+ }
+
+ const groupPathByRecordId = new Map<string, string>();
+ const resolveGroupPath = (recordId: string, visiting = new Set<string>()): string => {
+ const cached = groupPathByRecordId.get(recordId);
+ if (cached) return cached;
+ const group = groupRecords.get(recordId);
+ if (!group || visiting.has(recordId)) return "";
+
+ visiting.add(recordId);
+ const parentPath = group.parentId === "0" ? "" : resolveGroupPath(group.parentId, visiting);
+ visiting.delete(recordId);
+ const path = parentPath ? `${parentPath}/${group.name}` : group.name;
+ groupPathByRecordId.set(recordId, path);
+ return path;
+ };
+
+ const paths = new Map<string, string>();
+ for (const connection of connectionRecords) {
+ const path = resolveGroupPath(connection.parentId);
+ if (path) paths.set(connection.uuid, path);
+ }
+ return paths;
+}
+
function mergeFragments(shared: Map<string, Partial<DataSourceFragment>>, local: Map<string, Partial<DataSourceFragment>>): DataSourceFragment[] {
const merged = new Map<string, Partial<DataSourceFragment>>();
@@ -412,12 +478,14 @@ export function isDataGripImportPayload(content: string): boolean {
}
}
-export function parseDataGripConnections(payload: DataGripImportPayload): ConnectionConfig[] {
+export function parseDataGripImport(payload: DataGripImportPayload): DataGripImportResult {
const shared = parseDataSourcesXml(payload.dataSources);
const local = payload.dataSourcesLocal ? parseDataSourcesLocalXml(payload.dataSourcesLocal) : new Map();
const fragments = mergeFragments(shared, local);
+ const forestGroupPaths = payload.dbForestConfig ? parseDbForestGroupPaths(payload.dbForestConfig) : new Map<string, string>();
const configs: ConnectionConfig[] = [];
+ const connectionGroupPaths = new Map<string, string>();
const seen = new Set<string>();
for (const fragment of fragments) {
@@ -426,9 +494,22 @@ export function parseDataGripConnections(payload: DataGripImportPayload): Connec
if (seen.has(key)) continue;
seen.add(key);
configs.push(config);
+ const groupPath = forestGroupPaths.get(fragment.uuid) || fragment.groupName;
+ if (groupPath) connectionGroupPaths.set(config.id, groupPath);
}
- return configs;
+ // DataGrip stores the selected group on each data source rather than in a
+ // separate folder registry, so rebuild the layout from those references.
+ const layout = buildSidebarLayoutFromFolderPaths(
+ configs.map((config) => config.id),
+ new Set(connectionGroupPaths.values()),
+ connectionGroupPaths,
+ );
+ return { connections: configs, layout };
+}
+
+export function parseDataGripConnections(payload: DataGripImportPayload): ConnectionConfig[] {
+ return parseDataGripImport(payload).connections;
}
/** Returns a map of dedup key (name\0host\0port\0db) → DataGrip UUID for Keychain lookup. */
diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts
index 2147d4b07e..b542016e94 100644
--- a/apps/desktop/src/stores/connectionStore.ts
+++ b/apps/desktop/src/stores/connectionStore.ts
@@ -187,7 +187,7 @@ function ensureSqlServerLinkedRootNode(connectionId: string, children: TreeNode[
}
// Temporary storage for DataGrip import payload (used to read Keychain passwords after import)
-let pendingDataGripPayload: { format: "datagrip-import"; dataSources: string; dataSourcesLocal?: string } | null = null;
+let pendingDataGripPayload: { format: "datagrip-import"; dataSources: string; dataSourcesLocal?: string; dbForestConfig?: string } | null = null;
interface TreeClipboardTableEntry {
connectionId: string;
@@ -5277,6 +5277,7 @@ export const useConnectionStore = defineStore("connection", () => {
async function readDataGripImportFile(): Promise<{ content: string; encrypted: boolean } | null> {
let dataSources: string;
let dataSourcesLocal = "";
+ let dbForestConfig = "";
if (isTauriRuntime()) {
const { open } = await import("@tauri-apps/plugin-dialog");
@@ -5294,6 +5295,11 @@ export const useConnectionStore = defineStore("connection", () => {
} catch {
dataSourcesLocal = "";
}
+ try {
+ dbForestConfig = await readTextFile(dir + "db-forest-config.xml");
+ } catch {
+ dbForestConfig = "";
+ }
} else {
const files = await new Promise<FileList>((resolve, reject) => {
const input = document.createElement("input");
@@ -5312,15 +5318,19 @@ export const useConnectionStore = defineStore("connection", () => {
const fileList = Array.from(files);
const dsFile = fileList.find((f) => /^dataSources\.xml$/i.test(f.name)) || fileList[0];
const localFile = fileList.find((f) => /^dataSources\.local\.xml$/i.test(f.name));
+ const forestFile = fileList.find((f) => /^db-forest-config\.xml$/i.test(f.name));
if (!dsFile) throw new Error("Select dataSources.xml");
dataSources = await dsFile.text();
if (localFile) {
dataSourcesLocal = await localFile.text();
}
+ if (forestFile) {
+ dbForestConfig = await forestFile.text();
+ }
}
return {
- content: JSON.stringify({ format: "datagrip-import", dataSources, dataSourcesLocal }),
+ content: JSON.stringify({ format: "datagrip-import", dataSources, dataSourcesLocal, dbForestConfig }),
encrypted: false,
};
}
@@ -5379,15 +5389,18 @@ export const useConnectionStore = defineStore("connection", () => {
imported = await parseNavicatConnections(content);
} else if (!passphrase) {
const { isDbeaverImportPayload, parseDbeaverImport } = await import("@/lib/imports/dbeaverImport");
- const { isDataGripImportPayload, parseDataGripConnections } = await import("@/lib/imports/datagripImport");
+ const { isDataGripImportPayload, parseDataGripImport } = await import("@/lib/imports/datagripImport");
if (isDataGripImportPayload(content)) {
const payload = JSON.parse(content) as {
format: "datagrip-import";
dataSources: string;
dataSourcesLocal?: string;
+ dbForestConfig?: string;
};
pendingDataGripPayload = payload;
- imported = parseDataGripConnections(payload);
+ const result = parseDataGripImport(payload);
+ imported = result.connections;
+ importedLayout = result.layout;
} else if (isDbeaverImportPayload(content)) {
const result = await parseDbeaverImport(content);
imported = result.connections;