feat(mongo): open object browser from database tree nodes

This commit is contained in:
高瑞强 authored and GitHub committed 2026-08-25 22:15:40 +08:00
1 parent 46111850db
commit bfd7c7def2
29 files changed
+354 -61

No files matched your search

@@ -1,6 +1,7 @@
import { createPinia, setActivePinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { DataTabReuseMode } from "@/lib/tabs/dataTabReuseMode";
import { buildMongoObjectBrowserRows } from "@/lib/table/objectBrowserRows";
import type { QueryResult } from "@/types/database";
const mocks = vi.hoisted(() => ({
@@ -233,6 +234,65 @@ describe("useNavigationTargets with the real query store", () => {
expect(queryStore.activeTabId).toBe(firstTabId);
});
it("reuses only the same MongoDB collection in same-table mode", async () => {
mocks.connectionStore.getConfig.mockImplementation((connectionId: string) => ({ id: connectionId, db_type: "mongodb" }));
const { navigation, queryStore } = await setupNavigation();
const rows = buildMongoObjectBrowserRows({ collections: [{ name: " users " }, { name: "users" }], database: "app" });
for (const row of rows) {
await navigation.openObjectBrowserTableTarget({ connectionId: "connection-1", database: "app", tableName: row.name, tableType: row.type });
}
await navigation.openObjectBrowserTableTarget({ connectionId: "connection-1", database: "app", tableName: rows[0]!.name, tableType: rows[0]!.type });
expect(queryStore.tabs).toHaveLength(2);
expect(new Set(queryStore.tabs.map((tab) => tab.id))).toHaveLength(2);
expect(queryStore.tabs.map((tab) => tab.sql)).toEqual([" users ", "users"]);
expect(queryStore.tabs.map((tab) => tab.tableMeta?.tableName)).toEqual([" users ", "users"]);
expect(queryStore.activeTabId).toBe(queryStore.tabs[0]?.id);
});
it("always creates a new MongoDB collection tab in always-new mode", async () => {
mocks.connectionStore.getConfig.mockImplementation((connectionId: string) => ({ id: connectionId, db_type: "mongodb" }));
mocks.settingsStore.editorSettings.dataTabReuseMode = "always-new";
const { navigation, queryStore } = await setupNavigation();
const target = { connectionId: "connection-1", database: "app", tableName: "users", tableType: "TABLE" };
await navigation.openObjectBrowserTableTarget(target);
await navigation.openObjectBrowserTableTarget(target);
expect(queryStore.tabs).toHaveLength(2);
expect(new Set(queryStore.tabs.map((tab) => tab.id))).toHaveLength(2);
});
it("reuses the active safe MongoDB collection tab in active-tab mode", async () => {
mocks.connectionStore.getConfig.mockImplementation((connectionId: string) => ({ id: connectionId, db_type: "mongodb" }));
mocks.settingsStore.editorSettings.dataTabReuseMode = "active-tab";
const { navigation, queryStore } = await setupNavigation();
const target = { connectionId: "connection-1", database: "app", tableName: "users", tableType: "TABLE" };
await navigation.openObjectBrowserTableTarget(target);
const originalTabId = queryStore.activeTabId;
await navigation.openObjectBrowserTableTarget({ ...target, tableName: "orders" });
expect(queryStore.tabs).toHaveLength(1);
expect(queryStore.activeTabId).toBe(originalTabId);
expect(queryStore.tabs[0]).toMatchObject({ title: "app.orders", sql: "orders", tableMeta: { tableName: "orders" } });
});
it("does not reuse an unsafe active MongoDB collection tab", async () => {
mocks.connectionStore.getConfig.mockImplementation((connectionId: string) => ({ id: connectionId, db_type: "mongodb" }));
mocks.settingsStore.editorSettings.dataTabReuseMode = "active-tab";
const { navigation, queryStore } = await setupNavigation();
const target = { connectionId: "connection-1", database: "app", tableName: "users", tableType: "TABLE" };
await navigation.openObjectBrowserTableTarget(target);
queryStore.tabs[0]!.pinned = true;
await navigation.openObjectBrowserTableTarget({ ...target, tableName: "orders" });
expect(queryStore.tabs).toHaveLength(2);
expect(queryStore.tabs.map((tab) => tab.sql)).toEqual(["users", "orders"]);
});
it("reuses a restored legacy MySQL tab when the same table is opened from the sidebar", async () => {
mocks.connectionStore.getConfig.mockImplementation((connectionId: string) => ({ id: connectionId, db_type: "mysql" }));
mocks.loadOpenTabsState.mockResolvedValue({
@@ -1,7 +1,7 @@
import * as api from "@/lib/backend/api";
import { connectionObjectTreeNodeSchema, effectiveDatabaseTypeForConnection, metadataSchemaForConnection } from "@/lib/database/jdbcDialect";
import { invalidateTableMetadataCache, loadTableMetadata } from "@/lib/metadata/tableMetadataCache";
import { canApplyDataTabMetadata } from "@/lib/sidebar/dataTabOpenPolicy";
import { canApplyDataTabMetadata, canReuseActiveMongoTab, type DataTabReuseMode } from "@/lib/sidebar/dataTabOpenPolicy";
import { isNoSnapshotErrorResult, isQueryExecutionErrorResult } from "@/lib/query/queryResultError";
import { buildTableSelectSql } from "@/lib/table/tableSelectSql";
import { tableDataLargeValuePreviewOptions } from "@/lib/dataGrid/dataGridLargeValues";
@@ -26,6 +26,30 @@ export type NavigationTarget = {
whereInput?: string;
};
function openMongoCollectionTarget(target: NavigationTarget, reuseMode: DataTabReuseMode) {
const connectionStore = useConnectionStore();
const queryStore = useQueryStore();
connectionStore.activeConnectionId = target.connectionId;
const tabTitle = `${target.database}.${target.tableName}`;
const sameCollectionTab = reuseMode === "always-new" ? undefined : queryStore.tabs.find((tab) => tab.mode === "mongo" && tab.connectionId === target.connectionId && tab.database === target.database && tab.sql === target.tableName);
const activeTab = queryStore.tabs.find((tab) => tab.id === queryStore.activeTabId);
const reusableActiveTab = reuseMode === "active-tab" && !sameCollectionTab && canReuseActiveMongoTab(activeTab, target) ? activeTab : undefined;
const reusableTab = sameCollectionTab ?? reusableActiveTab;
const tabId = reusableTab?.id ?? queryStore.createTab(target.connectionId, target.database, tabTitle, "mongo", undefined, undefined, undefined, { forceNew: true });
if (reusableTab) {
queryStore.switchTab(tabId);
reusableTab.title = tabTitle;
}
queryStore.updateSql(tabId, target.tableName);
queryStore.setTableMeta(tabId, {
database: target.database,
tableName: target.tableName,
tableType: target.tableType || "TABLE",
columns: [],
primaryKeys: [],
});
}
async function openTableTarget(target: NavigationTarget, options: { tableInfoTab?: TableInfoTab } = {}) {
const connectionStore = useConnectionStore();
const queryStore = useQueryStore();
@@ -34,6 +58,10 @@ async function openTableTarget(target: NavigationTarget, options: { tableInfoTab
connectionStore.activeConnectionId = target.connectionId;
const config = connectionStore.getConfig(target.connectionId);
if (config?.db_type === "mongodb") {
openMongoCollectionTarget(target, settingsStore.editorSettings.dataTabReuseMode);
return;
}
const tableSchema = connectionObjectTreeNodeSchema(config, target.database, target.schema);
const tabTitle = target.catalog ? `${target.catalog}.${tableSchema || target.database}.${target.tableName}` : tableSchema ? `${tableSchema}.${target.tableName}` : target.tableName;
if (config?.db_type === "qdrant" || config?.db_type === "milvus" || config?.db_type === "weaviate" || config?.db_type === "chromadb") {
@@ -287,6 +315,10 @@ export function useNavigationTargets(dialogs: { showFieldLineageDialog: { value:
const { openData } = useSidebarDataOpenRuntime();
async function openObjectBrowserTableTarget(target: NavigationTarget) {
if (connectionStore.getConfig(target.connectionId)?.db_type === "mongodb") {
openMongoCollectionTarget(target, settingsStore.editorSettings.dataTabReuseMode);
return;
}
if (settingsStore.editorSettings.dataTabReuseMode === "always-new") {
await openTableTarget(target);
return;