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(sidebar): optimize connection root lookup

vrustxe0835ad2806a
diff --git a/apps/desktop/src/stores/__tests__/connectionStore.findConnectionNode.spec.ts b/apps/desktop/src/stores/__tests__/connectionStore.findConnectionNode.spec.ts
new file mode 100644
index 0000000000..39d9558a94
--- /dev/null
+++ b/apps/desktop/src/stores/__tests__/connectionStore.findConnectionNode.spec.ts
@@ -0,0 +1,62 @@
+import { createPinia, setActivePinia } from "pinia";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import type { TreeNode } from "@/types/database";
+
+function installLocalStorage() {
+ const data = new Map<string, string>();
+ vi.stubGlobal("localStorage", {
+ getItem: vi.fn((key: string) => data.get(key) ?? null),
+ setItem: vi.fn((key: string, value: string) => data.set(key, value)),
+ removeItem: vi.fn((key: string) => data.delete(key)),
+ });
+}
+
+describe("connection root lookup", () => {
+ beforeEach(() => {
+ vi.resetModules();
+ vi.unstubAllGlobals();
+ installLocalStorage();
+ setActivePinia(createPinia());
+ });
+
+ it("finds a connection nested inside connection groups", async () => {
+ const { useConnectionStore } = await import("@/stores/connectionStore");
+ const store = useConnectionStore();
+ store.treeNodes = [
+ {
+ id: "group-1",
+ label: "Group",
+ type: "connection-group",
+ children: [{ id: "pg-1", label: "PG", type: "connection", connectionId: "pg-1", isExpanded: true, children: [{ id: "pg-1:db", label: "db", type: "database", connectionId: "pg-1" }] }],
+ },
+ ] as TreeNode[];
+ store.connectedIds.add("pg-1");
+ const conn = (store.treeNodes[0]!.children ?? [])[0]!;
+ conn.isLoading = true;
+
+ // markConnectionLost → clearConnectionNodeLoading 经连接根查找定位节点:
+ // 分组内的连接必须能被找到并清除 loading
+ store.markConnectionLost("pg-1", new Error("connection lost"));
+ expect(conn.isLoading).toBe(false);
+ });
+
+ it("does not mistake a colliding deep node id for a connection root", async () => {
+ const { useConnectionStore } = await import("@/stores/connectionStore");
+ const store = useConnectionStore();
+ // 深层节点 id 与连接 id 相同(构造性碰撞):连接查找不得命中深层节点
+ const collidingDeepNode: TreeNode = { id: "pg-1", label: "deep-collision", type: "schema", connectionId: "other" } as TreeNode;
+ store.treeNodes = [
+ { id: "other", label: "Other", type: "connection", connectionId: "other", children: [collidingDeepNode] },
+ { id: "pg-1", label: "PG", type: "connection", connectionId: "pg-1", isExpanded: true, children: [] },
+ ] as TreeNode[];
+ store.connectedIds.add("pg-1");
+ const realRoot = store.treeNodes[1]!;
+ realRoot.isLoading = true;
+ (collidingDeepNode as { isLoading?: boolean }).isLoading = true;
+
+ store.markConnectionLost("pg-1", new Error("connection lost"));
+ // 真正的连接根被清除 loading;深层碰撞节点不受影响
+ expect(realRoot.isLoading).toBe(false);
+ expect((collidingDeepNode as { isLoading?: boolean }).isLoading).toBe(true);
+ });
+});
diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts
index 867a725237..5795f830b8 100644
--- a/apps/desktop/src/stores/connectionStore.ts
+++ b/apps/desktop/src/stores/connectionStore.ts
@@ -448,7 +448,7 @@ export const useConnectionStore = defineStore("connection", () => {
bumpConnectionStateRevision(connectionId);
activeLocalConnectionAttempts.set(connectionId, attempt);
connectingIds.value.add(connectionId);
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (node) node.isLoading = true;
return attempt;
}
@@ -691,7 +691,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
function clearConnectionNodeLoading(connectionId: string) {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (node) node.isLoading = false;
}
@@ -1815,7 +1815,7 @@ export const useConnectionStore = defineStore("connection", () => {
clearConnectionHealthCheck(config.id);
invalidateCompletionCache(config.id);
clearLoadedChildrenCache(config.id);
- const node = findNode(treeNodes.value, config.id);
+ const node = findConnectionNode(config.id);
if (node?.isExpanded) {
await reloadConnectionDatabaseChildren(config.id);
}
@@ -2044,7 +2044,7 @@ export const useConnectionStore = defineStore("connection", () => {
clearConnectionError(config.id);
if (id !== config.id) clearConnectionError(id);
- const existing = findNode(treeNodes.value, id);
+ const existing = findConnectionNode(id);
if (existing) {
existing.label = config.name;
existing.type = "connection";
@@ -2103,7 +2103,7 @@ export const useConnectionStore = defineStore("connection", () => {
clearConnectionIdentifierQuote(connectionId);
forgetSuccessfulLocalConnectionAttempt(connectionId);
clearConnectionHealthCheck(connectionId);
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (node) {
node.isLoading = false;
node.isExpanded = false;
@@ -2248,7 +2248,7 @@ export const useConnectionStore = defineStore("connection", () => {
driverProfile: metadataDriverProfile(configForScope),
},
async () => {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
try {
@@ -2401,7 +2401,7 @@ export const useConnectionStore = defineStore("connection", () => {
if (!connectedIds.value.has(connectionId)) return;
const config = getConfig(connectionId);
if (!config || ["redis", "etcd", "zookeeper", "mongodb", "elasticsearch", "milvus", "qdrant", "weaviate", "chromadb", "mq", "nacos"].includes(config.db_type)) return;
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node || node.type !== "connection" || node.isLoading || hasConnectionMetadataChildren(node.children)) return;
const scope = { kind: "connection-databases" as const, connectionId, driverProfile: metadataDriverProfile(config) };
if (metadataLoadCoordinator.has(scope)) return;
@@ -2417,7 +2417,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
async function loadRedisDatabases(connectionId: string) {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
@@ -2460,7 +2460,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
async function loadEtcdRoot(connectionId: string) {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
@@ -2494,7 +2494,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
async function loadZooKeeperRoot(connectionId: string) {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
@@ -2528,7 +2528,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
async function loadMqTenants(connectionId: string, options?: LoadTreeOptions) {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
@@ -2574,7 +2574,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
async function loadNacosNamespaces(connectionId: string, options?: LoadTreeOptions) {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
@@ -2630,7 +2630,7 @@ export const useConnectionStore = defineStore("connection", () => {
// key trees under expanded db nodes are preserved. Used after a Redis write command so the
// `dbN (count)` labels reflect the new reality without a manual refresh.
async function refreshRedisDbKeyCounts(connectionId: string) {
- const connNode = findNode(treeNodes.value, connectionId);
+ const connNode = findConnectionNode(connectionId);
if (!connNode) return;
try {
await ensureConnected(connectionId);
@@ -2644,7 +2644,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
async function loadMongoDatabases(connectionId: string) {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
@@ -2679,7 +2679,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
async function loadElasticsearchIndices(connectionId: string) {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
@@ -2711,7 +2711,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
async function loadMilvusDatabases(connectionId: string) {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
@@ -2748,7 +2748,7 @@ export const useConnectionStore = defineStore("connection", () => {
const isMilvus = config?.db_type === "milvus";
const effectiveDb = database || config?.database || "default";
// Milvus groups collections under a per-database node; other vector stores stay flat under the connection.
- const node = isMilvus && database ? findNode(treeNodes.value, `${connectionId}:${database}`) : findNode(treeNodes.value, connectionId);
+ const node = isMilvus && database ? findNode(treeNodes.value, `${connectionId}:${database}`) : findConnectionNode(connectionId);
if (!node) return;
node.isLoading = true;
@@ -4421,7 +4421,7 @@ export const useConnectionStore = defineStore("connection", () => {
}
function databaseNamesFromTree(connectionId: string): string[] {
- const node = findNode(treeNodes.value, connectionId);
+ const node = findConnectionNode(connectionId);
if (!node?.children) return [];
const seen = new Set<string>();
const names: string[] = [];
@@ -4856,6 +4856,24 @@ export const useConnectionStore = defineStore("connection", () => {
return null;
}
+ /** 查连接根节点:沿 connection-group 层级下钻但不穿透连接的整棵子树
+ * (原通用 DFS 找第 N 个连接前要完整遍历前 N-1 个连接的数千个表/列节点)。
+ * 不能用"同层优先"版 findNode 代替通用 DFS——节点 id 并非全树唯一
+ * (如数据库 "a:b" 与数据库 "a" 下 schema "b" 同为 connectionId:a:b,
+ * 见 pinnedItems 对 colliding node IDs 的处理),改变遍历顺序会让深层
+ * 调用选中错误节点;连接根节点的 id 就是 connectionId,且只出现在
+ * 顶层或连接组内,无歧义。 */
+ function findConnectionNode(connectionId: string, nodes: TreeNode[] = treeNodes.value): TreeNode | null {
+ for (const node of nodes) {
+ if (node.id === connectionId && node.type !== "connection-group") return node;
+ if (node.type === "connection-group" && node.children) {
+ const found = findConnectionNode(connectionId, node.children);
+ if (found) return found;
+ }
+ }
+ return null;
+ }
+
async function persistConnections(nextConnections: ConnectionConfig[] = connections.value) {
await api.saveConnections(nextConnections);
}