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。

fix(desktop): reuse cached indexes for sidebar table search (#13062)

Satrio Ponco Sushadi204e5a1efc1a
diff --git a/apps/desktop/src/components/sidebar/ConnectionTree.vue b/apps/desktop/src/components/sidebar/ConnectionTree.vue
index b543006b5b..09eecb7473 100644
--- a/apps/desktop/src/components/sidebar/ConnectionTree.vue
+++ b/apps/desktop/src/components/sidebar/ConnectionTree.vue
@@ -14,25 +14,24 @@ import type { ElasticsearchIndexMetadataKind } from "@/lib/backend/tauri";
import { listEventTriggers } from "@/lib/backend/api";
import {
filterLocallySearchedTables,
- createSidebarSearchSubtreePreserver,
filterSidebarSearchRootsByConnectionState,
filterSidebarTree,
filterSidebarTreeToConnectedConnections,
findNodePathByIdentity,
mergeSidebarRegexIndexScopes,
resolveSidebarFilterGuards,
- resolveSidebarObjectSearchFilter,
type SidebarRegexIndexScope,
type SidebarRegexScopeIdentity,
localTableSearchParentTypes,
} from "@/lib/sidebar/sidebarSearchTree";
import { createSidebarLabelMatcher } from "@/lib/sidebar/sidebarSearch";
import { firstSidebarSearchMatchDataNode, hasActiveSidebarSearch } from "@/lib/sidebar/sidebarSearchEnter";
-import { collectSidebarRegexIndexScopes, resolveSidebarRemoteSearchQuery, resolveSidebarSearchDispatchMode, shouldRestoreTrackedSidebarSearchTargetsInRegexMode } from "@/lib/sidebar/sidebarRegexSearchIndex";
-import { needsSidebarObjectGroupDiscovery } from "@/lib/sidebar/sidebarSearchDiscovery";
-import { isSidebarSearchPrunedDatabaseNode, resolveSidebarSearchDatabaseScope } from "@/lib/sidebar/sidebarSearchDatabaseScope";
-import { createSidebarSearchExpansionState } from "@/lib/sidebar/sidebarSearchExpansionState";
-import { createSidebarSearchLoadingTracker } from "@/lib/sidebar/sidebarSearchLoadingTracker";
+import { collectSidebarRegexIndexScopes } from "@/lib/sidebar/sidebarRegexSearchIndex";
+import { collectSidebarGlobalSearchIndexes, sidebarIndexedObjectGroupTypes } from "@/lib/sidebar/sidebarGlobalSearchIndex";
+import { resolveSidebarSearchDatabaseScope } from "@/lib/sidebar/sidebarSearchDatabaseScope";
+import { useSidebarSearchIndexes } from "@/composables/useSidebarSearchIndexes";
+import { useSidebarRemoteSearch } from "@/composables/useSidebarRemoteSearch";
+import { collectSidebarRemoteSearchResults, mergeSidebarRemoteSearchResults, type SidebarRemoteSearchResult } from "@/lib/sidebar/sidebarRemoteSearch";
import { isCancelSearchShortcut, isCopySidebarSelectionShortcut, isDisconnectSidebarConnectionShortcut, isEditSidebarConnectionShortcut, isEditSidebarTableStructureShortcut, isPasteSidebarSelectionShortcut, isRefreshDataShortcut, isViewTableDdlShortcut } from "@/lib/editor/keyboardShortcuts";
import { sidebarNodeSupportsDdlView } from "@/lib/sidebar/sidebarTreeDdlShortcut";
import { hasTreeNodeDatabaseContext } from "@/lib/sidebar/treeNodeContext";
@@ -64,8 +63,7 @@ import { sidebarTreeContextKey } from "@/lib/sidebar/sidebarTreeContext";
import { createSidebarTreeRuntime, sidebarTreeRuntimeKey, type SidebarTreeRuntimeHostInstance } from "@/lib/sidebar/sidebarTreeRuntime";
import { createSidebarPasteHandlerRegistry } from "@/lib/sidebar/sidebarPasteHandlerRegistry";
import { insertSidebarTableSearchControls, isSidebarTableSearchControlNode } from "@/lib/sidebar/sidebarTableSearchControl";
-import { createSidebarTableSearchDebouncer, invalidateSidebarTableSearchBuild, loadOrBuildSidebarTableSearchIndex, scheduleExclusiveSidebarTableSearchDebounce } from "@/lib/sidebar/sidebarTableSearchIndex";
-import { runSidebarSearchTasks, type SidebarSearchTask } from "./sidebarSearchTaskRunner";
+import { createSidebarTableSearchDebouncer, scheduleExclusiveSidebarTableSearchDebounce } from "@/lib/sidebar/sidebarTableSearchIndex";
import TreeItem from "./TreeItem.vue";
import ProductionContextBadge from "@/components/common/ProductionContextBadge.vue";
import { showSidebarStickyProductionBadge } from "@/lib/sidebar/sidebarProductionBadge";
@@ -116,8 +114,7 @@ const { toast } = useToast();
const searchQuery = ref("");
const deferredSearchQuery = ref("");
const regexMode = ref(false);
-const sidebarSearchLoadingTracker = createSidebarSearchLoadingTracker();
-const isSidebarSearchLoading = ref(false);
+store.sidebarSearchQuery = "";
const showConnectedConnectionsOnly = ref(false);
const isDisconnectingAllActiveConnections = ref(false);
const searchInputRef = ref<HTMLInputElement>();
@@ -187,13 +184,6 @@ const sidebarObjectSourceFormatDialect = computed(() => sqlFormatDialectForDbTyp
type SearchScope = "connection" | "database" | "schema" | "table" | "view";
const selectedSearchScopes = ref<SearchScope[]>([]);
const searchCollapsedIds = ref<Set<string>>(new Set());
-const searchExpansionState = createSidebarSearchExpansionState();
-const searchRefreshedNodeIds = searchExpansionState.filteredNodeIds;
-// Group nodes that search force-expanded on the user's behalf (they were
-// collapsed before the query started). Cleared alongside searchRefreshedNodeIds
-// so clearing the search box collapses them back instead of leaving every
-// touched group open and reloading it again.
-const searchAutoExpandedNodeIds = searchExpansionState.autoExpandedNodeIds;
let searchTimer: number | undefined;
const remoteTableSearchDebouncer = createSidebarTableSearchDebouncer();
// Local-mode table searches debounce like remote ones: rapid typing coalesces
@@ -278,13 +268,6 @@ async function disconnectAllActiveConnections() {
}
}
-function refreshActiveSidebarTableSearches() {
- if (isTreeSearchFiltering.value) return;
- for (const parentNodeId of Object.keys(store.sidebarTableSearchQueries)) {
- scheduleSidebarTableSearchRefresh(parentNodeId);
- }
-}
-
watch(
() => settingsStore.editorSettings.sidebarTableSearchEnabled,
(enabled) => {
@@ -304,201 +287,6 @@ watch(
},
);
-watch([deferredSearchQuery, regexMode], ([newQuery, isRegexMode], [oldQuery, wasRegexMode]) => {
- if (!isRegexMode || !newQuery) regexTableSearchScopes.value = [];
- // The regex source is a client-side projection; the remote tree-loading
- // search state must never carry it, or explicit node expansion would leak
- // the expression as a remote searchFilter.
- store.sidebarSearchQuery = resolveSidebarRemoteSearchQuery(isRegexMode, newQuery);
- const dispatchMode = resolveSidebarSearchDispatchMode({ query: newQuery, regexMode: isRegexMode, wasRegexMode }, { localSearchEnabled: settingsStore.editorSettings.sidebarGlobalSearchLocal });
- if (dispatchMode === "regex") {
- // Regex search is a read-only projection over live nodes and the local
- // table index. It must never trigger ensureConnected/listTables.
- // Keep results already loaded by an ordinary query until the regex is
- // cleared. Restoring them here can empty the tree when this connection has
- // no complete local table index yet (for example: "user" -> Regex mode).
- const restoreTasks = shouldRestoreTrackedSidebarSearchTargetsInRegexMode(newQuery) ? restoreTrackedSearchTargets() : [];
- const searchGeneration = sidebarSearchLoadingTracker.begin();
- isSidebarSearchLoading.value = true;
- void Promise.allSettled([loadRegexTableSearchIndexes(), runSidebarSearchTasks(restoreTasks)])
- .then(() => {
- if (restoreTasks.length > 0) refreshActiveSidebarTableSearches();
- })
- .finally(() => {
- if (sidebarSearchLoadingTracker.end(searchGeneration)) isSidebarSearchLoading.value = false;
- });
- return;
- }
- if (dispatchMode === "none") {
- sidebarSearchLoadingTracker.cancel();
- isSidebarSearchLoading.value = false;
- const restoreTasks = restoreTrackedSearchTargets();
- if (restoreTasks.length > 0) {
- void runSidebarSearchTasks(restoreTasks)
- .then(() => refreshActiveSidebarTableSearches())
- .catch(() => {});
- }
- return;
- }
- const preservesSearchSubtree = newQuery ? createSidebarSearchSubtreePreserver(newQuery, searchableNodeTypes.value) : undefined;
- const restoreTasks = !newQuery && oldQuery ? restoreTrackedSearchTargets() : [];
- let searchGeneration = -1;
- if (newQuery) {
- searchGeneration = sidebarSearchLoadingTracker.begin();
- isSidebarSearchLoading.value = true;
- } else {
- sidebarSearchLoadingTracker.cancel();
- isSidebarSearchLoading.value = false;
- }
- const searchWork = newQuery ? loadSidebarSearchTargets(newQuery, preservesSearchSubtree) : runSidebarSearchTasks(restoreTasks);
- void searchWork
- .then(() => {
- if (!newQuery && oldQuery) refreshActiveSidebarTableSearches();
- })
- .finally(() => {
- if (searchGeneration >= 0 && sidebarSearchLoadingTracker.end(searchGeneration)) isSidebarSearchLoading.value = false;
- });
-});
-
-const searchableObjectGroupTypes = new Set<TreeNodeType>([
- "group-tables",
- "group-dolt-system-tables",
- "group-views",
- "group-materialized-views",
- "group-procedures",
- "group-functions",
- "group-internal-functions",
- "group-udf-functions",
- "group-triggers",
- "group-events",
- "group-sequences",
- "group-database-indexes",
- "group-synonyms",
- "group-jobs",
- "group-packages",
- "group-types",
-]);
-const simpleObjectParentTypes = new Set<TreeNodeType>(["database", "schema", "linked-server-schema"]);
-
-function isSimpleObjectSearchParent(node: TreeNode): boolean {
- return settingsStore.editorSettings.sidebarObjectDisplay === "simple" && simpleObjectParentTypes.has(node.type) && node.connectionId != null && node.database != null;
-}
-
-function isSidebarSearchContainer(node: TreeNode): boolean {
- return simpleObjectParentTypes.has(node.type) && node.connectionId != null && node.database != null;
-}
-
-async function loadSidebarSearchTargets(query: string, preservesNodeSubtree?: (node: TreeNode) => boolean) {
- if (!query) return;
- const refreshedNodeIds = searchRefreshedNodeIds;
- const scheduledNodeIds = new Set<string>();
- let tasks: SidebarSearchTask[] = [];
- do {
- tasks = [];
- for (const root of store.treeNodes) {
- collectExpandedObjectSearchTargets(root, tasks, refreshedNodeIds, preservesNodeSubtree, false, scheduledNodeIds);
- }
- if (tasks.length === 0) return;
- await runSidebarSearchTasks(tasks);
- } while (deferredSearchQuery.value === query && store.sidebarSearchQuery === query);
-}
-
-function collectExpandedObjectSearchTargets(node: TreeNode, tasks: SidebarSearchTask[], refreshedNodeIds?: Set<string>, preservesNodeSubtree?: (node: TreeNode) => boolean, ancestorPreservesSearchSubtree = false, scheduledNodeIds?: Set<string>, databaseScope?: ReadonlySet<string> | null) {
- const preservesSearchSubtree = ancestorPreservesSearchSubtree || (!!refreshedNodeIds && !!preservesNodeSubtree?.(node));
- if (refreshedNodeIds && node.type === "connection" && node.connectionId) {
- // 数据库级节点只有被用户真正打开(树已加载或被打开的页签引用,与侧栏
- // 「打开」高亮同口径)才参与自动搜索;一个都没打开时退回全库搜索。
- databaseScope = resolveSidebarSearchDatabaseScope(node, {
- enabled: settingsStore.editorSettings.sidebarSearchOpenedDatabasesOnly,
- isChildrenLoaded: store.isTreeNodeChildrenLoaded,
- openDatabaseKeys: queryStore.openDatabaseKeys,
- });
- const connectionIsConnected = store.connectedIds.has(node.connectionId);
- if (connectionIsConnected && (!scheduledNodeIds || !scheduledNodeIds.has(node.id))) {
- const connectionId = node.connectionId;
- scheduledNodeIds?.add(node.id);
- tasks.push(() => store.loadConnectedConnectionRootForSidebarSearch(connectionId, { sidebarSearch: true }));
- }
- // 搜索不得替用户建连:只有连接中的连接(含当前激活的那个)才继续刷新子树。
- // 断开或连不上的连接直接跳过,后台搜索不会因此弹出凭据输入或写入整段连接错误。
- if (!connectionIsConnected || node.connectionId !== store.activeConnectionId) return;
- }
- if (refreshedNodeIds && databaseScope && isSidebarSearchPrunedDatabaseNode(node, databaseScope)) return;
- if (refreshedNodeIds && isSimpleObjectSearchParent(node)) {
- if (!scheduledNodeIds || !scheduledNodeIds.has(node.id)) {
- scheduledNodeIds?.add(node.id);
- if (preservesSearchSubtree) {
- if (refreshedNodeIds.delete(node.id)) {
- tasks.push(() => store.loadTreeNodeChildren(node, { force: true, searchFilter: "", allowGlobalSearchMismatch: true, expectedSidebarSearchQuery: store.sidebarSearchQuery, sidebarSearch: true }));
- }
- } else {
- const wasCollapsed = node.isExpanded !== true;
- refreshedNodeIds.add(node.id);
- if (wasCollapsed) searchExpansionState.markFiltered(node.id, true);
- tasks.push(() => store.refreshTreeNode(node, { sidebarSearch: true }));
- }
- }
- return;
- }
- if (refreshedNodeIds && isSidebarSearchContainer(node) && needsSidebarObjectGroupDiscovery(node, searchableObjectGroupTypes) && (!scheduledNodeIds || !scheduledNodeIds.has(node.id))) {
- scheduledNodeIds?.add(node.id);
- const wasCollapsed = node.isExpanded !== true;
- searchExpansionState.markFiltered(node.id, wasCollapsed);
- tasks.push(() => store.loadTreeNodeChildren(node, { force: true, expectedSidebarSearchQuery: store.sidebarSearchQuery, sidebarSearch: true }));
- }
- if (refreshedNodeIds && node.children) {
- for (const child of node.children) {
- if (child.connectionId && searchableObjectGroupTypes.has(child.type)) {
- if (scheduledNodeIds?.has(child.id)) continue;
- scheduledNodeIds?.add(child.id);
- if (preservesSearchSubtree) {
- if (searchExpansionState.markUnfiltered(child.id)) {
- tasks.push(() => store.loadObjectGroupChildren(child, { force: true, searchFilter: "", allowGlobalSearchMismatch: true, expectedSidebarSearchQuery: store.sidebarSearchQuery, sidebarSearch: true }));
- }
- } else {
- searchExpansionState.markFiltered(child.id, !child.isExpanded);
- tasks.push(() => store.loadObjectGroupChildren(child, { force: true, sidebarSearch: true }));
- }
- }
- }
- } else if (!refreshedNodeIds && searchExpansionState.shouldRestore(node.id)) {
- if (searchableObjectGroupTypes.has(node.type)) {
- if (searchAutoExpandedNodeIds.has(node.id)) {
- // Search opened this group on the user's behalf to check for matches;
- // once the query is gone, drop the filtered projection and collapse it
- // back. Its next explicit expansion will load the ordinary first page.
- node.isExpanded = false;
- store.discardFilteredTreeNodeChildren(node.id);
- } else if (!store.restoreFilteredObjectGroupChildren(node)) {
- // Nothing was captured because the group had not been loaded before the
- // search, so there is no previous list to put back.
- tasks.push(() => store.loadObjectGroupChildren(node, { force: true, sidebarSearch: true }));
- }
- } else if (simpleObjectParentTypes.has(node.type)) {
- const shouldCollapse = searchAutoExpandedNodeIds.has(node.id);
- tasks.push(async () => {
- await store.refreshTreeNode(node, { sidebarSearch: true });
- if (shouldCollapse) node.isExpanded = false;
- });
- }
- }
- if (node.children) {
- for (const child of node.children) {
- collectExpandedObjectSearchTargets(child, tasks, refreshedNodeIds, preservesNodeSubtree, preservesSearchSubtree, scheduledNodeIds, databaseScope);
- }
- }
-}
-
-function restoreTrackedSearchTargets(): SidebarSearchTask[] {
- if (!searchExpansionState.hasTrackedNodes()) return [];
- const tasks: SidebarSearchTask[] = [];
- for (const root of store.treeNodes) {
- collectExpandedObjectSearchTargets(root, tasks);
- }
- searchExpansionState.clear();
- return tasks;
-}
-
const sidebarFilterGuards = computed(() => resolveSidebarFilterGuards(showConnectedConnectionsOnly.value, searchQuery.value, hasSearchScopeFilter.value));
// Connected-only filtering changes only root visibility, so descendant-local
// features stay available while operations requiring the full root list pause.
@@ -722,27 +510,124 @@ const localTableSearchResults = ref<Record<string, TableInfo[] | null>>({});
const localTableSearchRequestRevisions = new Map<string, number>();
type InvalidatedTableSearchScope = SidebarRegexScopeIdentity & { parentNodeId: string };
const pendingInvalidatedTableSearchScopes = new Map<string, InvalidatedTableSearchScope>();
-const regexTableSearchScopes = shallowRef<SidebarRegexIndexScope[]>([]);
+const globalSearchContext = [
+ () => regexMode.value,
+ () => settingsStore.editorSettings.sidebarGlobalSearchLocal,
+ () => settingsStore.editorSettings.sidebarObjectDisplay,
+ () => settingsStore.editorSettings.sidebarSearchOpenedDatabasesOnly,
+ () => store.activeConnectionId,
+ () => [...store.connectedIds].sort().join("\0"),
+ () => [...queryStore.openDatabaseKeys].sort().join("\0"),
+ () => store.sidebarTableSearchIndexInvalidation.revision,
+ () => selectedSearchScopes.value.join(","),
+];
+const { scopes: globalTableSearchScopes, isLoading: isGlobalIndexLoading } = useSidebarSearchIndexes({
+ query: deferredSearchQuery,
+ context: globalSearchContext,
+ load: loadGlobalTableSearchIndexes,
+ onError: (error) => toast(error instanceof Error ? error.message : String(error), 5000),
+});
+const { results: remoteSearchResults, isLoading: isRemoteSearchLoading } = useSidebarRemoteSearch({
+ query: searchQuery,
+ enabled: computed(() => !regexMode.value && !settingsStore.editorSettings.sidebarGlobalSearchLocal && !searchableNodeTypes.value),
+ context: [...globalSearchContext, () => store.sidebarObjectSearchRevision],
+ load: loadGlobalRemoteSearchResults,
+ onError: (error) => toast(error instanceof Error ? error.message : String(error), 5000),
+});
+const isSidebarSearchLoading = computed(() => isGlobalIndexLoading.value || isRemoteSearchLoading.value);
+
+async function loadGlobalRemoteSearchResults(query: string, signal: AbortSignal, onProgress: (results: SidebarRemoteSearchResult[]) => void) {
+ const connectionId = store.activeConnectionId;
+ if (!connectionId || !store.connectedIds.has(connectionId) || signal.aborted) return [];
+ const rootPath = findNodePathByIdentity(store.treeNodes, connectionId, { connectionId, database: "", nodeType: "connection" });
+ const root = rootPath?.[rootPath.length - 1];
+ if (!root) return [];
+ return collectSidebarRemoteSearchResults({
+ root,
+ connectionId,
+ databaseScope: resolveSidebarSearchDatabaseScope(root, { enabled: settingsStore.editorSettings.sidebarSearchOpenedDatabasesOnly, isChildrenLoaded: store.isTreeNodeChildrenLoaded, openDatabaseKeys: queryStore.openDatabaseKeys }),
+ signal,
+ isSimpleParent: (node) => !sidebarIndexedObjectGroupTypes.has(node.type) && isGlobalTableIndexParent(node),
+ discover: discoverGlobalSearchChildren,
+ search: (node, publish) => store.searchSidebarObjects(node, query, signal, publish),
+ onProgress,
+ onError: (error) => toast(error instanceof Error ? error.message : String(error), 5000),
+ });
+}
+
+function isGlobalTableIndexParent(node: TreeNode): boolean {
+ if (sidebarIndexedObjectGroupTypes.has(node.type)) return true;
+ if (settingsStore.editorSettings.sidebarObjectDisplay !== "simple") return false;
+ if (node.type === "schema" || node.type === "linked-server-schema") return true;
+ if (node.type !== "database") return false;
+ const config = store.getConfig(node.connectionId || "");
+ return config?.db_type !== "sqlserver" && !(usesTreeSchemaMode(effectiveDatabaseTypeForConnection(config)) && !connectionUsesDatabaseObjectTreeMode(config)) && !connectionShouldDiscoverJdbcSchemas(config);
+}
-async function loadRegexTableSearchIndexes() {
- if (!regexMode.value || !deferredSearchQuery.value) return;
- const loadedScopes = await collectSidebarRegexIndexScopes(
+async function discoverGlobalSearchChildren(node: TreeNode): Promise<readonly TreeNode[]> {
+ if (node.children?.some((child) => sidebarIndexedObjectGroupTypes.has(child.type) || child.type === "database" || child.type === "schema" || child.type === "doris-catalog")) return node.children;
+ if (!node.connectionId || !store.connectedIds.has(node.connectionId) || store.isTreeNodeChildrenLoaded(node.id)) return node.children ?? [];
+ const wasExpanded = node.isExpanded;
+ try {
+ if (node.type === "connection") {
+ await store.loadConnectedConnectionRootForSidebarSearch(node.connectionId, { sidebarSearch: true });
+ } else {
+ await store.loadTreeNodeChildren(node, { searchFilter: "", allowGlobalSearchMismatch: true, expectedSidebarSearchQuery: "", sidebarSearch: true });
+ }
+ return node.children ?? [];
+ } finally {
+ node.isExpanded = wasExpanded;
+ }
+}
+
+async function loadGlobalTableSearchIndexes(shouldCancel: () => boolean, onProgress: (scopes: SidebarRegexIndexScope[]) => void): Promise<SidebarRegexIndexScope[]> {
+ if (searchableNodeTypes.value && !["table", "view", "materialized_view"].some((type) => searchableNodeTypes.value?.has(type as TreeNodeType))) return [];
+ if (regexMode.value || settingsStore.editorSettings.sidebarGlobalSearchLocal) {
+ return collectSidebarRegexIndexScopes(
+ {
+ loadSidebarTableSearchIndexScopes: () => store.loadSidebarTableSearchIndexScopes(),
+ loadSidebarTableSearchIndex: (parent) => store.loadSidebarTableSearchIndex(parent.parentNodeId, parent),
+ },
+ store.treeNodes,
+ shouldCancel,
+ );
+ }
+ const connectionId = store.activeConnectionId;
+ if (!connectionId || !store.connectedIds.has(connectionId)) return [];
+ const rootPath = findNodePathByIdentity(store.treeNodes, connectionId, { connectionId, database: "", nodeType: "connection" });
+ const root = rootPath?.[rootPath.length - 1];
+ if (!root) return [];
+ const databaseScope = resolveSidebarSearchDatabaseScope(root, {
+ enabled: settingsStore.editorSettings.sidebarSearchOpenedDatabasesOnly,
+ isChildrenLoaded: store.isTreeNodeChildrenLoaded,
+ openDatabaseKeys: queryStore.openDatabaseKeys,
+ });
+ return collectSidebarGlobalSearchIndexes(
{
- loadSidebarTableSearchIndexScopes: () => store.loadSidebarTableSearchIndexScopes(),
- loadSidebarTableSearchIndex: (parent) => store.loadSidebarTableSearchIndex(parent.parentNodeId, parent),
+ loadCachedScopes: async () =>
+ (await store.loadSidebarTableSearchIndexScopes((scope) => scope.connectionId === connectionId && (settingsStore.editorSettings.sidebarObjectDisplay === "simple" ? !scope.nodeType.startsWith("group-") : sidebarIndexedObjectGroupTypes.has(scope.nodeType as TreeNodeType)))).map(
+ ({ scope, entries, stale }) => ({ ...scope, entries, stale }),
+ ),
+ isIndexParent: isGlobalTableIndexParent,
+ discover: discoverGlobalSearchChildren,
+ loadIndex: (node, options) => {
+ if (shouldCancel() || !store.connectedIds.has(connectionId)) return Promise.resolve(null);
+ return store.ensureSidebarTableSearchIndex(node.id, deferredSearchQuery.value, { connectionId, database: node.database ?? "", schema: node.schema, catalog: node.catalog, nodeType: node.type }, { connectedOnly: true, refresh: options?.refresh });
+ },
+ onError: (_node, error) => toast(error instanceof Error ? error.message : String(error), 5000),
},
- store.treeNodes,
- () => !regexMode.value || deferredSearchQuery.value === "",
+ [root],
+ connectionId,
+ databaseScope,
+ shouldCancel,
+ onProgress,
);
- if (regexMode.value) {
- regexTableSearchScopes.value = loadedScopes;
- }
}
-function filterGloballyIndexedRegexTables(nodes: TreeNode[]): TreeNode[] {
- if (!regexMode.value || !deferredSearchQuery.value) return nodes;
- const matcher = createSidebarLabelMatcher(deferredSearchQuery.value, { regexMode: true });
- const matchingScopes = regexTableSearchScopes.value.map((scope) => ({ ...scope, entries: scope.entries.filter((entry) => !!matcher(entry.name) || !!matcher(entry.comment || "")) })).filter((scope) => scope.entries.length > 0);
+function filterGloballyIndexedTables(nodes: TreeNode[]): TreeNode[] {
+ if (!deferredSearchQuery.value) return nodes;
+ const matcher = createSidebarLabelMatcher(deferredSearchQuery.value, { regexMode: regexMode.value });
+ const matchingScopes = globalTableSearchScopes.value.map((scope) => ({ ...scope, entries: scope.entries.filter((entry) => !!matcher(entry.name) || !!matcher(entry.comment || "")) })).filter((scope) => scope.entries.length > 0);
return mergeSidebarRegexIndexScopes(nodes, matchingScopes);
}
@@ -765,21 +650,8 @@ async function loadLocalTableSearchResults(parentNodeId: string, refresh = false
const requestRevision = (localTableSearchRequestRevisions.get(parentNodeId) ?? 0) + 1;
localTableSearchRequestRevisions.set(parentNodeId, requestRevision);
try {
- // A missing persisted index (null) means this scope was never indexed, so
- // only the currently loaded first page of children is searchable. That
- // silently misses alphabetically-late tables (e.g. "T_Erp_Nc_SuPlan_List"
- // for "erpncs") until an explicit index refresh happens. Build the index
- // on the first search so results always cover the complete table set,
- // matching the behavior of an explicit refresh. The scope key deduplicates
- // concurrent full builds, and an empty (cleared) query never builds.
const query = store.sidebarTableSearchQueries[parentNodeId]?.trim() ?? "";
- const entries = await loadOrBuildSidebarTableSearchIndex(
- parentNodeId,
- query,
- () => store.loadSidebarTableSearchIndex(parentNodeId, identity),
- () => store.refreshSidebarTableSearchIndex(parentNodeId, identity),
- refresh,
- );
+ const entries = await store.ensureSidebarTableSearchIndex(parentNodeId, query, identity, { refresh });
if (localTableSearchRequestRevisions.get(parentNodeId) !== requestRevision) return;
if (query || refresh) localTableSearchResults.value = { ...localTableSearchResults.value, [parentNodeId]: entries };
} finally {
@@ -811,7 +683,6 @@ watch(
for (const scope of invalidation.scopes) {
delete nextResults[scope.parentNodeId];
localTableSearchDebouncer.cancel(scope.parentNodeId);
- invalidateSidebarTableSearchBuild(scope.parentNodeId);
pendingInvalidatedTableSearchScopes.set(invalidatedTableSearchScopeKey(scope), scope);
}
localTableSearchResults.value = nextResults;
@@ -826,7 +697,8 @@ const filteredNodes = computed(() => {
}
nodes = filterLocallySearchedTables(nodes, { enabled: settingsStore.editorSettings.sidebarTableSearchLocal, queries: store.sidebarTableSearchQueries, indexedResults: localTableSearchResults.value });
- nodes = filterGloballyIndexedRegexTables(nodes);
+ nodes = filterGloballyIndexedTables(nodes);
+ nodes = mergeSidebarRemoteSearchResults(nodes, remoteSearchResults.value);
const q = deferredSearchQuery.value;
nodes = filterSidebarTree(nodes, q, searchCollapsedIds.value, searchableNodeTypes.value, {
@@ -1534,16 +1406,9 @@ provide(sidebarTreeContextKey, {
// collapse while a projection is about to start, and the deferred query
// keeps the currently rendered projection alive while clearing settles.
isSearchProjectionActive: () => isTreeSearchFiltering.value || !!deferredSearchQuery.value,
- getTreeLoadSearchOptions: (node) => {
- const query = deferredSearchQuery.value;
- if (regexMode.value) {
- // Explicit expansion stays allowed and may connect, but must never send
- // the regex expression as a remote search filter.
- return { searchFilter: "", allowGlobalSearchMismatch: true, expectedSidebarSearchQuery: "" };
- }
- if (!query) return undefined;
- const searchFilter = resolveSidebarObjectSearchFilter(store.treeNodes, node.id, query, searchableNodeTypes.value);
- return searchFilter ? undefined : { searchFilter: "", allowGlobalSearchMismatch: true, expectedSidebarSearchQuery: query };
+ getTreeLoadSearchOptions: () => {
+ if (!deferredSearchQuery.value) return undefined;
+ return { searchFilter: "", allowGlobalSearchMismatch: true, expectedSidebarSearchQuery: "" };
},
setTableSearchQuery: (parentNodeId, query, local) => {
const focusRestore = captureTableSearchFocus(parentNodeId);
@@ -2767,6 +2632,7 @@ onMounted(() => {
});
onUnmounted(() => {
+ store.sidebarSearchQuery = "";
sidebarLayoutMonitor.dispose();
sidebarTreeRuntime.dispose();
sidebarActionGeneration += 1;
diff --git a/apps/desktop/src/components/sidebar/__tests__/ConnectionTree.remoteSearch.spec.ts b/apps/desktop/src/components/sidebar/__tests__/ConnectionTree.remoteSearch.spec.ts
new file mode 100644
index 0000000000..6aae4d8763
--- /dev/null
+++ b/apps/desktop/src/components/sidebar/__tests__/ConnectionTree.remoteSearch.spec.ts
@@ -0,0 +1,202 @@
+// @vitest-environment happy-dom
+import { readFileSync } from "node:fs";
+import ts from "typescript";
+import { parse } from "vue/compiler-sfc";
+import { computed, createApp, defineComponent, h, nextTick, reactive, ref } from "vue";
+import { afterEach, beforeEach, expect, test, vi } from "vitest";
+import { useSidebarRemoteSearch } from "@/composables/useSidebarRemoteSearch";
+import { collectSidebarRemoteSearchResults, type SidebarRemoteSearchResult } from "@/lib/sidebar/sidebarRemoteSearch";
+import { findNodePathByIdentity } from "@/lib/sidebar/sidebarSearchTree";
+import { sidebarIndexedObjectGroupTypes } from "@/lib/sidebar/sidebarGlobalSearchIndex";
+import { resolveSidebarSearchDatabaseScope } from "@/lib/sidebar/sidebarSearchDatabaseScope";
+import type { TreeNode } from "@/types/database";
+
+const source = parse(readFileSync("apps/desktop/src/components/sidebar/ConnectionTree.vue", "utf8")).descriptor.scriptSetup!.content;
+const script = ts.createSourceFile("ConnectionTree.ts", source, ts.ScriptTarget.Latest, true);
+const statements = script.statements.filter((statement) => {
+ if (ts.isFunctionDeclaration(statement)) return ["loadGlobalRemoteSearchResults", "isGlobalTableIndexParent", "discoverGlobalSearchChildren"].includes(statement.name?.text ?? "");
+ return ts.isVariableStatement(statement) && statement.declarationList.declarations.some((declaration) => ["globalSearchContext", "{ results: remoteSearchResults, isLoading: isRemoteSearchLoading }"].includes(declaration.name.getText(script)));
+});
+const compiled = ts.transpileModule(statements.map((statement) => statement.getText(script)).join("\n"), { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
+const cleanups: Array<() => void> = [];
+
+beforeEach(() => vi.useFakeTimers());
+afterEach(() => {
+ for (const cleanup of cleanups.splice(0)) cleanup();
+ vi.useRealTimers();
+ document.body.replaceChildren();
+});
+
+function mountSearch() {
+ const searchQuery = ref("");
+ const regexMode = ref(false);
+ const settingsStore = reactive({ editorSettings: { sidebarGlobalSearchLocal: false, sidebarObjectDisplay: "grouped", sidebarSearchOpenedDatabasesOnly: false } });
+ const group: TreeNode = { id: "conn:app:__procedures", connectionId: "conn", database: "app", type: "group-procedures", label: "Procedures", children: [] };
+ const root: TreeNode = { id: "conn", connectionId: "conn", type: "connection", label: "Connection", children: [group] };
+ const store = reactive({
+ activeConnectionId: "conn",
+ connectedIds: new Set(["conn"]),
+ treeNodes: [root],
+ sidebarTableSearchIndexInvalidation: { revision: 0 },
+ sidebarObjectSearchRevision: 0,
+ isTreeNodeChildrenLoaded: vi.fn(() => true),
+ searchSidebarObjects: vi.fn(async (_node: TreeNode, _query: string, _signal: AbortSignal): Promise<TreeNode[]> => []),
+ loadTreeNodeChildren: vi.fn(),
+ loadConnectedConnectionRootForSidebarSearch: vi.fn(),
+ });
+ const toast = vi.fn();
+ const bindings = {
+ ref,
+ computed,
+ searchQuery,
+ regexMode,
+ settingsStore,
+ store,
+ toast,
+ queryStore: { openDatabaseKeys: new Set() },
+ selectedSearchScopes: ref([]),
+ searchableNodeTypes: ref(undefined),
+ useSidebarRemoteSearch,
+ collectSidebarRemoteSearchResults,
+ findNodePathByIdentity,
+ sidebarIndexedObjectGroupTypes,
+ resolveSidebarSearchDatabaseScope,
+ };
+ let state!: { remoteSearchResults: { value: SidebarRemoteSearchResult[] }; isRemoteSearchLoading: { value: boolean } };
+ const app = createApp(
+ defineComponent({
+ setup() {
+ state = new Function(...Object.keys(bindings), `${compiled}; return { remoteSearchResults, isRemoteSearchLoading };`)(...Object.values(bindings));
+ return () =>
+ h("input", {
+ placeholder: "Search...",
+ value: searchQuery.value,
+ onInput: (event: Event) => {
+ searchQuery.value = (event.target as HTMLInputElement).value;
+ },
+ });
+ },
+ }),
+ );
+ const host = document.createElement("div");
+ document.body.append(host);
+ app.mount(host);
+ cleanups.push(() => app.unmount());
+ const input = host.querySelector("input")!;
+ const type = (query: string) => {
+ input.value = query;
+ input.dispatchEvent(new Event("input"));
+ };
+ return { ...state, store, settingsStore, regexMode, toast, type, app };
+}
+
+test("component input debounces fallback and cancels the old request immediately before the next debounce", async () => {
+ const view = mountSearch();
+ let finishOld!: (nodes: TreeNode[]) => void;
+ view.store.searchSidebarObjects.mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ finishOld = resolve;
+ }),
+ );
+ view.type("o");
+ view.type("old");
+ await vi.advanceTimersByTimeAsync(300);
+ expect(view.store.searchSidebarObjects).toHaveBeenCalledTimes(1);
+ const signal = view.store.searchSidebarObjects.mock.calls[0][2];
+ view.type("new");
+ expect(signal.aborted).toBe(true);
+ expect(view.remoteSearchResults.value).toEqual([]);
+ expect(view.store.searchSidebarObjects).toHaveBeenCalledTimes(1);
+ await vi.advanceTimersByTimeAsync(300);
+ expect(view.store.searchSidebarObjects.mock.calls[1][1]).toBe("new");
+ finishOld([{ id: "old", label: "old", type: "procedure" }]);
+ await nextTick();
+ await nextTick();
+ expect(view.remoteSearchResults.value.flatMap((result) => result.children)).toEqual([]);
+ expect(view.toast).not.toHaveBeenCalled();
+});
+
+test("keeps the last remote projection visible until the replacement request completes", async () => {
+ const view = mountSearch();
+ view.store.searchSidebarObjects.mockResolvedValueOnce([{ id: "shared", label: "shared_routine", type: "procedure" }]);
+ view.type("shared");
+ await vi.advanceTimersByTimeAsync(300);
+ await nextTick();
+ expect(view.remoteSearchResults.value.flatMap((result) => result.children).map((node) => node.label)).toEqual(["shared_routine"]);
+
+ let finishReplacement!: (nodes: TreeNode[]) => void;
+ view.store.searchSidebarObjects.mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ finishReplacement = resolve;
+ }),
+ );
+ view.type("shared_r");
+ expect(view.remoteSearchResults.value.flatMap((result) => result.children).map((node) => node.label)).toEqual(["shared_routine"]);
+ await vi.advanceTimersByTimeAsync(300);
+ expect(view.store.searchSidebarObjects.mock.calls[1][1]).toBe("shared_r");
+ expect(view.remoteSearchResults.value.flatMap((result) => result.children).map((node) => node.label)).toEqual(["shared_routine"]);
+
+ finishReplacement([{ id: "replacement", label: "shared_replacement", type: "procedure" }]);
+ await vi.advanceTimersByTimeAsync(0);
+ await nextTick();
+ expect(view.remoteSearchResults.value.flatMap((result) => result.children).map((node) => node.label)).toEqual(["shared_replacement"]);
+});
+
+test("retains the last remote projection when its background replacement fails", async () => {
+ const view = mountSearch();
+ view.store.searchSidebarObjects.mockResolvedValueOnce([{ id: "shared", label: "shared_routine", type: "procedure" }]);
+ view.type("shared");
+ await vi.advanceTimersByTimeAsync(300);
+ await nextTick();
+ expect(view.remoteSearchResults.value.flatMap((result) => result.children).map((node) => node.label)).toEqual(["shared_routine"]);
+
+ view.store.searchSidebarObjects.mockRejectedValueOnce(new Error("refresh failed"));
+ view.type("shared_r");
+ await vi.advanceTimersByTimeAsync(300);
+ await nextTick();
+ await nextTick();
+ expect(view.remoteSearchResults.value.flatMap((result) => result.children).map((node) => node.label)).toEqual(["shared_routine"]);
+ expect(view.toast).toHaveBeenCalledOnce();
+});
+
+test.each(["local", "regex"])("%s search never dispatches remote object search or discovery", async (mode) => {
+ const view = mountSearch();
+ if (mode === "local") view.settingsStore.editorSettings.sidebarGlobalSearchLocal = true;
+ else view.regexMode.value = true;
+ view.type("routine");
+ await vi.advanceTimersByTimeAsync(600);
+ expect(view.store.searchSidebarObjects).not.toHaveBeenCalled();
+ expect(view.store.loadTreeNodeChildren).not.toHaveBeenCalled();
+ expect(view.store.loadConnectedConnectionRootForSidebarSearch).not.toHaveBeenCalled();
+});
+
+test.each(["clear", "local", "regex", "disconnect", "refresh", "unmount"])("%s cancels a pending search and ignores its late failure", async (action) => {
+ const view = mountSearch();
+ let fail!: (error: Error) => void;
+ view.store.searchSidebarObjects.mockImplementationOnce(
+ () =>
+ new Promise((_resolve, reject) => {
+ fail = reject;
+ }),
+ );
+ view.type("routine");
+ await vi.advanceTimersByTimeAsync(300);
+ const signal = view.store.searchSidebarObjects.mock.calls[0][2];
+ if (action === "clear") view.type("");
+ if (action === "local") view.settingsStore.editorSettings.sidebarGlobalSearchLocal = true;
+ if (action === "regex") view.regexMode.value = true;
+ if (action === "disconnect") view.store.connectedIds.clear();
+ if (action === "refresh") view.store.sidebarObjectSearchRevision += 1;
+ if (action === "unmount") {
+ view.app.unmount();
+ cleanups.pop();
+ }
+ expect(signal.aborted).toBe(true);
+ fail(new Error("cancelled"));
+ await nextTick();
+ await nextTick();
+ expect(view.toast).not.toHaveBeenCalled();
+ expect(view.remoteSearchResults.value).toEqual([]);
+});
diff --git a/apps/desktop/src/composables/__tests__/useSidebarSearchIndexes.spec.ts b/apps/desktop/src/composables/__tests__/useSidebarSearchIndexes.spec.ts
new file mode 100644
index 0000000000..8182e166f9
--- /dev/null
+++ b/apps/desktop/src/composables/__tests__/useSidebarSearchIndexes.spec.ts
@@ -0,0 +1,161 @@
+import { effectScope, nextTick, ref } from "vue";
+import { describe, expect, it, vi } from "vitest";
+import { useSidebarSearchIndexes } from "@/composables/useSidebarSearchIndexes";
+import { filterSidebarTree, mergeSidebarRegexIndexScopes, type SidebarRegexIndexScope } from "@/lib/sidebar/sidebarSearchTree";
+import type { TreeNode } from "@/types/database";
+
+const indexed: SidebarRegexIndexScope[] = [{ parentNodeId: "conn:app", connectionId: "conn", database: "app", nodeType: "database", entries: [{ name: "orders", table_type: "TABLE" }] }];
+
+function fixture(load: (shouldCancel: () => boolean, onProgress: (scopes: SidebarRegexIndexScope[]) => void) => Promise<SidebarRegexIndexScope[]>) {
+ const scope = effectScope();
+ const query = ref("");
+ const revision = ref(0);
+ const onError = vi.fn();
+ const state = scope.run(() => useSidebarSearchIndexes({ query, context: [() => revision.value], load, onError }))!;
+ return { scope, query, revision, onError, ...state };
+}
+
+describe("global sidebar index session", () => {
+ it("loads once while typing and filters cached entries without touching the backing tree", async () => {
+ const load = vi.fn(async () => indexed);
+ const state = fixture(load);
+ const live: TreeNode[] = [{ id: "conn:app", label: "app", type: "database", connectionId: "conn", database: "app", isExpanded: false, children: [] }];
+ try {
+ for (const query of ["o", "or", "ord", "orders", "missing", "orders"]) {
+ state.query.value = query;
+ await nextTick();
+ await nextTick();
+ const projected = mergeSidebarRegexIndexScopes(live, state.scopes.value);
+ const filtered = filterSidebarTree(projected, query, new Set());
+ expect(filtered.length).toBe(query === "missing" ? 0 : 1);
+ await vi.waitFor(() => expect(state.isLoading.value).toBe(false));
+ }
+ expect(load).toHaveBeenCalledTimes(1);
+ expect(live[0].children).toEqual([]);
+ expect(live[0].isExpanded).toBe(false);
+ state.query.value = "";
+ await nextTick();
+ expect(state.scopes.value).toEqual([]);
+ expect(load).toHaveBeenCalledTimes(1);
+ expect(filterSidebarTree(live, "", new Set())).toBe(live);
+ } finally {
+ state.scope.stop();
+ }
+ });
+
+ it("publishes cached progress while background index preparation is still running", async () => {
+ let finish!: (scopes: SidebarRegexIndexScope[]) => void;
+ const refreshed = [{ ...indexed[0], entries: [...indexed[0].entries, { name: "orders_archive", table_type: "TABLE" }] }];
+ const state = fixture(
+ (_cancel, publish) =>
+ new Promise((resolve) => {
+ publish(indexed);
+ finish = resolve;
+ }),
+ );
+ try {
+ state.query.value = "orders";
+ await nextTick();
+ expect(state.scopes.value).toEqual(indexed);
+ expect(state.isLoading.value).toBe(true);
+ finish(refreshed);
+ await nextTick();
+ await nextTick();
+ expect(state.scopes.value).toEqual(refreshed);
+ } finally {
+ state.scope.stop();
+ }
+ });
+
+ it("shares a pending preparation while the keyword changes", async () => {
+ let resolve!: (scopes: SidebarRegexIndexScope[]) => void;
+ const load = vi.fn(
+ () =>
+ new Promise<SidebarRegexIndexScope[]>((complete) => {
+ resolve = complete;
+ }),
+ );
+ const state = fixture(load);
+ try {
+ state.query.value = "o";
+ await nextTick();
+ state.query.value = "orders";
+ await nextTick();
+ expect(state.isLoading.value).toBe(true);
+ expect(load).toHaveBeenCalledTimes(1);
+ resolve(indexed);
+ await nextTick();
+ await nextTick();
+ expect(state.scopes.value).toEqual(indexed);
+ await vi.waitFor(() => expect(state.isLoading.value).toBe(false));
+ } finally {
+ state.scope.stop();
+ }
+ });
+
+ it("discards stale work across a refresh, connection change, or mode switch", async () => {
+ const pending: Array<{ cancel: () => boolean; resolve: (scopes: SidebarRegexIndexScope[]) => void }> = [];
+ const state = fixture((cancel) => new Promise((resolve) => pending.push({ cancel, resolve })));
+ try {
+ state.query.value = "orders";
+ await nextTick();
+ state.revision.value += 1;
+ await nextTick();
+ expect(pending[0].cancel()).toBe(true);
+ pending[0].resolve(indexed);
+ await nextTick();
+ expect(state.scopes.value).toEqual([]);
+ expect(state.isLoading.value).toBe(true);
+ pending[1].resolve(indexed);
+ await nextTick();
+ await nextTick();
+ expect(state.scopes.value).toEqual(indexed);
+ await vi.waitFor(() => expect(state.isLoading.value).toBe(false));
+ } finally {
+ state.scope.stop();
+ }
+ });
+
+ it.each(["clear", "unmount"])("ignores a late result after %s", async (action) => {
+ let resolve!: (scopes: SidebarRegexIndexScope[]) => void;
+ let shouldCancel!: () => boolean;
+ const state = fixture((cancel) => {
+ shouldCancel = cancel;
+ return new Promise((complete) => {
+ resolve = complete;
+ });
+ });
+ state.query.value = "orders";
+ await nextTick();
+ if (action === "clear") state.query.value = "";
+ else state.scope.stop();
+ await nextTick();
+ expect(shouldCancel()).toBe(true);
+ resolve(indexed);
+ await nextTick();
+ expect(state.scopes.value).toEqual([]);
+ state.scope.stop();
+ });
+
+ it("reports a load failure once and allows retry on refresh", async () => {
+ const error = new Error("Metadata unavailable");
+ const load = vi.fn().mockRejectedValueOnce(error).mockResolvedValue(indexed);
+ const state = fixture(load);
+ try {
+ state.query.value = "o";
+ await nextTick();
+ await nextTick();
+ state.query.value = "orders";
+ await nextTick();
+ expect(state.onError).toHaveBeenCalledExactlyOnceWith(error);
+ expect(load).toHaveBeenCalledTimes(1);
+ state.revision.value += 1;
+ await nextTick();
+ await nextTick();
+ expect(state.scopes.value).toEqual(indexed);
+ await vi.waitFor(() => expect(state.isLoading.value).toBe(false));
+ } finally {
+ state.scope.stop();
+ }
+ });
+});
diff --git a/apps/desktop/src/composables/useSidebarRemoteSearch.ts b/apps/desktop/src/composables/useSidebarRemoteSearch.ts
new file mode 100644
index 0000000000..5386e6d491
--- /dev/null
+++ b/apps/desktop/src/composables/useSidebarRemoteSearch.ts
@@ -0,0 +1,90 @@
+import { onScopeDispose, ref, shallowRef, watch, type Ref, type WatchSource } from "vue";
+import type { SidebarRemoteSearchResult } from "@/lib/sidebar/sidebarRemoteSearch";
+
+export function useSidebarRemoteSearch(options: {
+ query: Ref<string>;
+ enabled: Readonly<Ref<boolean>>;
+ context: WatchSource[];
+ load: (query: string, signal: AbortSignal, onProgress: (results: SidebarRemoteSearchResult[]) => void) => Promise<SidebarRemoteSearchResult[]>;
+ onError: (error: unknown) => void;
+}) {
+ const results = shallowRef<SidebarRemoteSearchResult[]>([]);
+ const isLoading = ref(false);
+ let controller: AbortController | undefined;
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const abortPending = () => {
+ clearTimeout(timer);
+ timer = undefined;
+ controller?.abort();
+ controller = undefined;
+ isLoading.value = false;
+ };
+ const reset = () => {
+ abortPending();
+ results.value = [];
+ };
+ const scopeKey = (result: SidebarRemoteSearchResult) => JSON.stringify([result.parentNodeId, result.connectionId, result.database, result.schema ?? null, result.catalog ?? null, result.nodeType]);
+ const mergeProgress = (loaded: SidebarRemoteSearchResult[]) => {
+ const merged = new Map(results.value.map((result) => [scopeKey(result), result]));
+ for (const result of loaded) merged.set(scopeKey(result), result);
+ results.value = [...merged.values()];
+ };
+ const schedule = () => {
+ const query = options.query.value.trim().toLowerCase();
+ if (!query || !options.enabled.value) {
+ results.value = [];
+ return;
+ }
+ const request = new AbortController();
+ controller = request;
+ timer = setTimeout(() => {
+ timer = undefined;
+ isLoading.value = true;
+ void options
+ .load(query, request.signal, (loaded) => {
+ if (!request.signal.aborted) mergeProgress(loaded);
+ })
+ .then((loaded) => {
+ // A scope that failed to refresh is absent from `loaded`; retain its
+ // previous snapshot. Successful empty scopes are still published as
+ // explicit results with `children: []`, so they replace stale hits.
+ if (!request.signal.aborted) mergeProgress(loaded);
+ })
+ .catch((error) => {
+ if (!request.signal.aborted) options.onError(error);
+ })
+ .finally(() => {
+ if (controller !== request) return;
+ controller = undefined;
+ if (!request.signal.aborted) isLoading.value = false;
+ });
+ }, 300);
+ };
+
+ watch(
+ options.query,
+ () => {
+ // Keep the last completed projection visible while its replacement is
+ // debounced/loaded. The normal tree filter applies the new query to that
+ // snapshot, so overlapping matches stay stable instead of disappearing
+ // and reappearing when remote-only object types refresh.
+ abortPending();
+ schedule();
+ },
+ { flush: "sync" },
+ );
+
+ watch(
+ [options.enabled, ...options.context],
+ () => {
+ // Context boundaries can point at a different connection, database, or
+ // object layout. Never retain a projection across those boundaries.
+ reset();
+ schedule();
+ },
+ { flush: "sync" },
+ );
+
+ onScopeDispose(reset);
+ return { results, isLoading };
+}
diff --git a/apps/desktop/src/composables/useSidebarSearchIndexes.ts b/apps/desktop/src/composables/useSidebarSearchIndexes.ts
new file mode 100644
index 0000000000..9418c260a9
--- /dev/null
+++ b/apps/desktop/src/composables/useSidebarSearchIndexes.ts
@@ -0,0 +1,36 @@
+import { onScopeDispose, ref, shallowRef, watch, type Ref, type WatchSource } from "vue";
+import type { SidebarRegexIndexScope } from "@/lib/sidebar/sidebarSearchTree";
+
+export function useSidebarSearchIndexes(options: { query: Ref<string>; context: WatchSource[]; load: (shouldCancel: () => boolean, onProgress: (scopes: SidebarRegexIndexScope[]) => void) => Promise<SidebarRegexIndexScope[]>; onError: (error: unknown) => void }) {
+ const scopes = shallowRef<SidebarRegexIndexScope[]>([]);
+ const isLoading = ref(false);
+ let generation = 0;
+
+ watch([() => !!options.query.value, ...options.context], () => {
+ const currentGeneration = ++generation;
+ scopes.value = [];
+ isLoading.value = !!options.query.value;
+ if (!isLoading.value) return;
+ const shouldCancel = () => currentGeneration !== generation || !options.query.value;
+ const publish = (loaded: SidebarRegexIndexScope[]) => {
+ if (!shouldCancel()) scopes.value = loaded;
+ };
+ void options
+ .load(shouldCancel, publish)
+ .then((loaded) => {
+ if (!shouldCancel()) scopes.value = loaded;
+ })
+ .catch((error) => {
+ if (!shouldCancel()) options.onError(error);
+ })
+ .finally(() => {
+ if (currentGeneration === generation) isLoading.value = false;
+ });
+ });
+
+ onScopeDispose(() => {
+ generation += 1;
+ });
+
+ return { scopes, isLoading };
+}
diff --git a/apps/desktop/src/i18n/locales/az.ts b/apps/desktop/src/i18n/locales/az.ts
index 158b1fb8b0..78e21b96fb 100644
--- a/apps/desktop/src/i18n/locales/az.ts
+++ b/apps/desktop/src/i18n/locales/az.ts
@@ -497,7 +497,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "Uzaq sorğular göndərmədən yerli cədvəl indeksində axtarın. Uzaqdan axtarmaq üçün deaktiv edin. İndeksi yaratmaq və ya yeniləmək üçün yeniləmə əməliyyatından istifadə edin.",
refreshLocalTableSearchIndex: "Yerli cədvəl indeksini yenilə",
globalLocalSearch: "Yerli axtarış",
- globalLocalSearchTooltip: "Uzaq sorğular göndərmədən yalnız yan paneldə hazırda yüklənmiş obyektlərdə axtarın.",
+ globalLocalSearchTooltip: "Uzaq sorğular göndərmədən yüklənmiş obyektlərdə və keşlənmiş cədvəl indekslərində axtarın. Çatışmayan indeksləri yaratmaq və digər obyektləri uzaqdan axtarmaq üçün deaktiv edin. Metaverilənləri yeniləmək üçün yeniləmə əməliyyatından istifadə edin.",
regexSearch: "Müntəzəm ifadə ilə axtarış",
regexSearchTooltip: "Yan paneldəki adları, şərhləri və ləqəbləri JavaScript müntəzəm ifadələri ilə uyğunlaşdırın.",
clearFilter: "Süzgəci təmizlə",
diff --git a/apps/desktop/src/i18n/locales/en.ts b/apps/desktop/src/i18n/locales/en.ts
index 13a6ccd48a..a5ff7e0c93 100644
--- a/apps/desktop/src/i18n/locales/en.ts
+++ b/apps/desktop/src/i18n/locales/en.ts
@@ -1074,7 +1074,7 @@ export default {
localTableSearchTooltip: "Search the local table index without remote requests. Turn off to search remotely. Use refresh to build or update the index.",
refreshLocalTableSearchIndex: "Refresh local table index",
globalLocalSearch: "Local search",
- globalLocalSearchTooltip: "Search only currently loaded sidebar objects without remote requests.",
+ globalLocalSearchTooltip: "Search loaded objects and cached table indexes without remote requests. Turn off to build missing indexes and search other objects remotely. Use Refresh to update metadata.",
regexSearch: "Regex search",
regexSearchTooltip: "Match sidebar names, comments, and aliases with JavaScript regular expressions.",
clearFilter: "Clear filter",
diff --git a/apps/desktop/src/i18n/locales/es.ts b/apps/desktop/src/i18n/locales/es.ts
index db273bab2e..571dd376a6 100644
--- a/apps/desktop/src/i18n/locales/es.ts
+++ b/apps/desktop/src/i18n/locales/es.ts
@@ -924,7 +924,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "Busca en el índice local de tablas sin solicitudes remotas. Desactívalo para buscar de forma remota. Usa actualizar para crear o actualizar el índice.",
refreshLocalTableSearchIndex: "Actualizar índice local de tablas",
globalLocalSearch: "Búsqueda local",
- globalLocalSearchTooltip: "Busca solo los objetos de la barra lateral cargados actualmente sin solicitudes remotas.",
+ globalLocalSearchTooltip: "Busca objetos cargados e índices de tablas en caché sin solicitudes remotas. Desactívalo para crear los índices faltantes y buscar otros objetos de forma remota. Usa Actualizar para actualizar los metadatos.",
regexSearch: "Búsqueda con regex",
regexSearchTooltip: "Coincide con nombres, comentarios y alias mediante expresiones regulares de JavaScript.",
clearFilter: "Limpiar filtro",
diff --git a/apps/desktop/src/i18n/locales/id.ts b/apps/desktop/src/i18n/locales/id.ts
index 842b7494eb..c46b775369 100644
--- a/apps/desktop/src/i18n/locales/id.ts
+++ b/apps/desktop/src/i18n/locales/id.ts
@@ -702,7 +702,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "Cari indeks tabel lokal tanpa permintaan jarak jauh. Nonaktifkan untuk mencari secara jarak jauh. Gunakan segarkan untuk membangun atau memperbarui indeks.",
refreshLocalTableSearchIndex: "Segarkan indeks tabel lokal",
globalLocalSearch: "Pencarian lokal",
- globalLocalSearchTooltip: "Cari hanya objek bilah sisi yang sudah dimuat tanpa permintaan jarak jauh.",
+ globalLocalSearchTooltip: "Cari objek yang sudah dimuat dan indeks tabel tersimpan tanpa permintaan jarak jauh. Nonaktifkan untuk membuat indeks yang belum tersedia dan mencari objek lain di server. Gunakan Segarkan untuk memperbarui metadata.",
regexSearch: "Pencarian regex",
regexSearchTooltip: "Cocokkan nama, komentar, dan alias di bilah sisi dengan ekspresi reguler JavaScript.",
clearFilter: "Bersihkan filter",
diff --git a/apps/desktop/src/i18n/locales/it.ts b/apps/desktop/src/i18n/locales/it.ts
index 1806f82354..a8e1d79065 100644
--- a/apps/desktop/src/i18n/locales/it.ts
+++ b/apps/desktop/src/i18n/locales/it.ts
@@ -913,7 +913,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "Cerca nell'indice locale delle tabelle senza richieste remote. Disattiva per cercare in remoto. Usa aggiorna per creare o aggiornare l'indice.",
refreshLocalTableSearchIndex: "Aggiorna indice locale delle tabelle",
globalLocalSearch: "Ricerca locale",
- globalLocalSearchTooltip: "Cerca solo gli oggetti della barra laterale attualmente caricati senza richieste remote.",
+ globalLocalSearchTooltip: "Cerca oggetti caricati e indici delle tabelle in cache senza richieste remote. Disattiva per creare gli indici mancanti e cercare altri oggetti in remoto. Usa Aggiorna per aggiornare i metadati.",
regexSearch: "Ricerca regex",
regexSearchTooltip: "Trova nomi, commenti e alias con espressioni regolari JavaScript.",
clearFilter: "Cancella filtro",
diff --git a/apps/desktop/src/i18n/locales/ja.ts b/apps/desktop/src/i18n/locales/ja.ts
index 82aa003a9c..2fd0ce3e59 100644
--- a/apps/desktop/src/i18n/locales/ja.ts
+++ b/apps/desktop/src/i18n/locales/ja.ts
@@ -914,7 +914,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "リモート要求を行わずにローカルのテーブルインデックスを検索します。リモート検索するにはオフにしてください。更新でインデックスを作成または更新します。",
refreshLocalTableSearchIndex: "ローカルテーブルインデックスを更新",
globalLocalSearch: "ローカル検索",
- globalLocalSearchTooltip: "リモート要求を行わず、現在読み込まれているサイドバーオブジェクトのみを検索します。",
+ globalLocalSearchTooltip: "リモート要求を行わずに読み込み済みオブジェクトとキャッシュされたテーブルインデックスを検索します。オフにすると不足しているインデックスを作成し、他のオブジェクトをリモートで検索します。メタデータを更新するには更新を使用してください。",
regexSearch: "正規表現検索",
regexSearchTooltip: "JavaScript 正規表現でサイドバーの名前、コメント、エイリアスを照合します。",
clearFilter: "フィルターをクリア",
diff --git a/apps/desktop/src/i18n/locales/ko.ts b/apps/desktop/src/i18n/locales/ko.ts
index 6e957bcecd..c4ea5fed6d 100644
--- a/apps/desktop/src/i18n/locales/ko.ts
+++ b/apps/desktop/src/i18n/locales/ko.ts
@@ -916,6 +916,11 @@ export default withEnglishFallback({
searchScopeView: "뷰",
searchTablesInCurrentScope: "이 데이터베이스에서 테이블 검색...",
clearTableSearch: "테이블 검색 지우기",
+ localTableSearch: "로컬 검색",
+ localTableSearchTooltip: "원격 요청 없이 로컬 테이블 인덱스를 검색합니다. 원격으로 검색하려면 끄세요. 인덱스를 생성하거나 업데이트하려면 새로 고침을 사용하세요.",
+ refreshLocalTableSearchIndex: "로컬 테이블 인덱스 새로 고침",
+ globalLocalSearch: "로컬 검색",
+ globalLocalSearchTooltip: "원격 요청 없이 로드된 객체 및 캐시된 테이블 인덱스를 검색합니다. 누락된 인덱스를 생성하고 다른 객체를 원격으로 검색하려면 끄세요. 메타데이터를 업데이트하려면 새로 고침을 사용하세요.",
regexSearch: "정규식 검색",
regexSearchTooltip: "JavaScript 정규식으로 사이드바 이름, 주석, 별칭을 검색합니다.",
clearFilter: "필터 지우기",
diff --git a/apps/desktop/src/i18n/locales/pl.ts b/apps/desktop/src/i18n/locales/pl.ts
index 6f53bd1af8..f8b733e492 100644
--- a/apps/desktop/src/i18n/locales/pl.ts
+++ b/apps/desktop/src/i18n/locales/pl.ts
@@ -766,7 +766,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "Przeszukuj lokalny indeks tabel bez zapytań sieciowych. Wyłącz tę opcję, aby szukać zdalnie. Użyj odświeżania, aby utworzyć lub zaktualizować indeks.",
refreshLocalTableSearchIndex: "Odśwież lokalny indeks tabel",
globalLocalSearch: "Wyszukiwanie lokalne",
- globalLocalSearchTooltip: "Szukaj tylko w obiektach już wczytanych w panelu bocznym, bez zapytań zdalnych.",
+ globalLocalSearchTooltip: "Przeszukuj wczytane obiekty i indeksy tabel w pamięci podręcznej bez zapytań zdalnych. Wyłącz, aby utworzyć brakujące indeksy i szukać innych obiektów zdalnie. Użyj Odśwież, aby zaktualizować metadane.",
regexSearch: "Wyszukiwanie wyrażeniem regularnym",
regexSearchTooltip: "Dopasowuj nazwy, komentarze i aliasy w panelu bocznym przy użyciu wyrażeń regularnych JavaScript.",
clearFilter: "Wyczyść filtr",
diff --git a/apps/desktop/src/i18n/locales/pt-BR.ts b/apps/desktop/src/i18n/locales/pt-BR.ts
index 1453abbc68..3bba955c2c 100644
--- a/apps/desktop/src/i18n/locales/pt-BR.ts
+++ b/apps/desktop/src/i18n/locales/pt-BR.ts
@@ -914,7 +914,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "Pesquisa o índice local de tabelas sem solicitações remotas. Desative para pesquisar remotamente. Use atualizar para criar ou atualizar o índice.",
refreshLocalTableSearchIndex: "Atualizar índice local de tabelas",
globalLocalSearch: "Busca local",
- globalLocalSearchTooltip: "Pesquisa apenas os objetos da barra lateral carregados no momento, sem solicitações remotas.",
+ globalLocalSearchTooltip: "Pesquisa objetos carregados e índices de tabelas em cache sem solicitações remotas. Desative para criar índices ausentes e pesquisar outros objetos remotamente. Use Atualizar para atualizar os metadatos.",
regexSearch: "Busca por regex",
regexSearchTooltip: "Correspondência de nomes, comentários e aliases com expressões regulares JavaScript.",
clearFilter: "Limpar filtro",
diff --git a/apps/desktop/src/i18n/locales/ru.ts b/apps/desktop/src/i18n/locales/ru.ts
index d67fab95eb..37886d8eaa 100644
--- a/apps/desktop/src/i18n/locales/ru.ts
+++ b/apps/desktop/src/i18n/locales/ru.ts
@@ -894,7 +894,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "Поиск по локальному индексу таблиц без удалённых запросов. Отключите для удалённого поиска. Используйте обновление для построения или обновления индекса.",
refreshLocalTableSearchIndex: "Обновить локальный индекс таблиц",
globalLocalSearch: "Локальный поиск",
- globalLocalSearchTooltip: "Поиск только среди загруженных объектов боковой панели без удалённых запросов.",
+ globalLocalSearchTooltip: "Поиск по загруженным объектам и кэшированным индексам таблиц без удалённых запросов. Отключите для построения недостающих индексов и удалённого поиска других объектов. Используйте обновление для обновления метаданных.",
regexSearch: "Поиск по регулярному выражению",
regexSearchTooltip: "Сопоставлять имена, комментарии и псевдонимы в боковой панели с помощью регулярных выражений JavaScript.",
clearFilter: "Очистить фильтр",
diff --git a/apps/desktop/src/i18n/locales/th.ts b/apps/desktop/src/i18n/locales/th.ts
index 56b0ed8d49..b10f30f4de 100644
--- a/apps/desktop/src/i18n/locales/th.ts
+++ b/apps/desktop/src/i18n/locales/th.ts
@@ -1041,7 +1041,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "ค้นหา Index ตารางในเครื่องโดยไม่ส่ง Request ระยะไกล ปิดเพื่อค้นหาระยะไกล ใช้รีเฟรชเพื่อสร้างหรืออัปเดต Index",
refreshLocalTableSearchIndex: "รีเฟรช Index ตารางในเครื่อง",
globalLocalSearch: "การค้นหาในเครื่อง",
- globalLocalSearchTooltip: "ค้นหาเฉพาะออบเจ็กต์ในแถบด้านข้างที่โหลดอยู่ในขณะนี้โดยไม่ส่ง Request ระยะไกล",
+ globalLocalSearchTooltip: "ค้นหาออบเจ็กต์ที่โหลดแล้วและ Index ตารางในแคชโดยไม่ส่ง Request ระยะไกล ปิดเพื่อสร้าง Index ที่ขาดหายไปและค้นหาออบเจ็กต์อื่นจากระยะไกล ใช้รีเฟรชเพื่ออัปเดต Metadata",
regexSearch: "การค้นหา Regex",
regexSearchTooltip: "จับคู่ชื่อ คอมเมนต์ และ Alias ในแถบด้านข้างด้วย Regular Expression ของ JavaScript",
clearFilter: "ล้างตัวกรอง",
diff --git a/apps/desktop/src/i18n/locales/tr.ts b/apps/desktop/src/i18n/locales/tr.ts
index ee0f1f6ae3..0be8e2de8e 100644
--- a/apps/desktop/src/i18n/locales/tr.ts
+++ b/apps/desktop/src/i18n/locales/tr.ts
@@ -497,7 +497,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "Uzak istek olmadan yerel tablo dizinini arayın. Uzaktan aramak için kapatın. Dizini oluşturmak veya güncellemek için yenile düğmesini kullanın.",
refreshLocalTableSearchIndex: "Yerel tablo dizinini yenile",
globalLocalSearch: "Yerel arama",
- globalLocalSearchTooltip: "Uzak istek olmadan yalnızca yüklü kenar çubuğu nesnelerini arayın.",
+ globalLocalSearchTooltip: "Uzak istek olmadan yüklü nesneleri ve önbelleğe alınmış tablo dizinlerini arayın. Eksik dizinleri oluşturmak ve diğer nesneleri uzaktan aramak için kapatın. Meta verileri güncellemek için Yenile düğmesini kullanın.",
regexSearch: "Regex araması",
regexSearchTooltip: "Kenar çubuğu adlarını, açıklamaları ve takma adları JavaScript düzenli ifadeleriyle eşleştirin.",
clearFilter: "Filtreyi temizle",
diff --git a/apps/desktop/src/i18n/locales/zh-CN.ts b/apps/desktop/src/i18n/locales/zh-CN.ts
index 4f2599d8b7..68e0c9b6bc 100644
--- a/apps/desktop/src/i18n/locales/zh-CN.ts
+++ b/apps/desktop/src/i18n/locales/zh-CN.ts
@@ -1010,7 +1010,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "开启后搜索本地表索引,不发送远程请求;关闭后使用远程搜索。请使用刷新按钮构建或更新索引。",
refreshLocalTableSearchIndex: "刷新本地表索引",
globalLocalSearch: "本地搜索",
- globalLocalSearchTooltip: "开启后仅搜索已加载的侧边栏对象,不发送远程请求",
+ globalLocalSearchTooltip: "搜索已加载的对象和缓存的表索引,不发送远程请求。关闭后构建缺失的索引并远程搜索其他对象。请使用刷新更新元数据。",
regexSearch: "正则搜索",
regexSearchTooltip: "使用 JavaScript 正则表达式匹配侧边栏名称、备注和别名。",
clearFilter: "清除筛选",
diff --git a/apps/desktop/src/i18n/locales/zh-TW.ts b/apps/desktop/src/i18n/locales/zh-TW.ts
index abe23139ed..d7d21cb95b 100644
--- a/apps/desktop/src/i18n/locales/zh-TW.ts
+++ b/apps/desktop/src/i18n/locales/zh-TW.ts
@@ -918,7 +918,7 @@ export default withEnglishFallback({
localTableSearchTooltip: "使用本機資料表索引搜尋,不會發出遠端請求。關閉後改用遠端搜尋;使用重新整理建立或更新索引。",
refreshLocalTableSearchIndex: "重新整理本機資料表索引",
globalLocalSearch: "本機搜尋",
- globalLocalSearchTooltip: "只搜尋目前已載入的側邊欄物件,不會發出遠端請求。",
+ globalLocalSearchTooltip: "搜尋已載入的物件與快取的資料表索引,不會發出遠端請求。關閉後可建立缺少的索引並遠端搜尋其他物件。使用重新整理更新中繼資料。",
regexSearch: "正規表示式搜尋",
regexSearchTooltip: "使用 JavaScript 正規表示式比對側邊欄名稱、備註與別名。",
clearFilter: "清除篩選",
diff --git a/apps/desktop/src/lib/sidebar/sidebarGlobalSearchIndex.ts b/apps/desktop/src/lib/sidebar/sidebarGlobalSearchIndex.ts
new file mode 100644
index 0000000000..fbaf0e29f9
--- /dev/null
+++ b/apps/desktop/src/lib/sidebar/sidebarGlobalSearchIndex.ts
@@ -0,0 +1,70 @@
+import type { TableInfo, TreeNode, TreeNodeType } from "@/types/database";
+import { nodeMatchesRegexScopeIdentity, type SidebarRegexIndexScope } from "@/lib/sidebar/sidebarSearchTree";
+import { isSidebarSearchPrunedDatabaseNode } from "@/lib/sidebar/sidebarSearchDatabaseScope";
+
+export const sidebarIndexedObjectGroupTypes = new Set<TreeNodeType>(["group-tables", "group-dolt-system-tables", "group-views", "group-materialized-views"]);
+const containerTypes = new Set<TreeNodeType>(["database", "schema", "linked-server-schema", "doris-catalog"]);
+const linkedContainerTypes = new Set<TreeNodeType>(["linked-server-root", "linked-server", "linked-server-catalog"]);
+
+export interface SidebarGlobalSearchIndexLoader {
+ loadCachedScopes(): Promise<SidebarRegexIndexScope[]>;
+ isIndexParent(node: TreeNode): boolean;
+ discover(node: TreeNode): Promise<readonly TreeNode[]>;
+ loadIndex(node: TreeNode, options?: { refresh?: boolean }): Promise<TableInfo[] | null>;
+ onError(node: TreeNode, error: unknown): void;
+}
+
+export async function collectSidebarGlobalSearchIndexes(
+ loader: SidebarGlobalSearchIndexLoader,
+ roots: readonly TreeNode[],
+ connectionId: string,
+ databaseScope: ReadonlySet<string> | null,
+ shouldCancel: () => boolean,
+ onProgress?: (scopes: SidebarRegexIndexScope[]) => void,
+): Promise<SidebarRegexIndexScope[]> {
+ const cached = await loader.loadCachedScopes();
+ if (shouldCancel()) return [];
+ const scopedCache = cached.filter((scope) => scope.connectionId === connectionId && (!databaseScope || scope.path?.every((node) => !isSidebarSearchPrunedDatabaseNode(node as TreeNode, databaseScope))));
+ const results: SidebarRegexIndexScope[] = [...scopedCache];
+ if (results.length) onProgress?.([...results]);
+ const upsert = (scope: SidebarRegexIndexScope) => {
+ const index = results.findIndex(
+ (candidate) => candidate.parentNodeId === scope.parentNodeId && candidate.nodeType === scope.nodeType && candidate.connectionId === scope.connectionId && candidate.database === scope.database && candidate.schema === scope.schema && candidate.catalog === scope.catalog,
+ );
+ if (index >= 0) results[index] = scope;
+ else results.push(scope);
+ onProgress?.([...results]);
+ };
+ const visit = async (node: TreeNode, ancestors: TreeNode[]): Promise<void> => {
+ if (shouldCancel() || isSidebarSearchPrunedDatabaseNode(node, databaseScope)) return;
+ if (node.connectionId && node.connectionId !== connectionId) return;
+ const path = [...ancestors, node];
+ try {
+ if (loader.isIndexParent(node)) {
+ const cachedScope = scopedCache.find((scope) => nodeMatchesRegexScopeIdentity(node, scope.parentNodeId, scope));
+ const entries = cachedScope ? await loader.loadIndex(node, { refresh: true }) : await loader.loadIndex(node);
+ if (entries && !shouldCancel()) {
+ upsert({
+ parentNodeId: node.id,
+ connectionId,
+ database: node.database ?? "",
+ schema: node.schema,
+ catalog: node.catalog,
+ nodeType: node.type,
+ path: path.map(({ id, label, type, connectionId, database, schema, catalog, linkedServer, linkedCatalog, linkedSchema }) => ({ id, label, type, connectionId, database, schema, catalog, linkedServer, linkedCatalog, linkedSchema })),
+ entries,
+ });
+ }
+ return;
+ }
+ const children = node.type === "connection" || containerTypes.has(node.type) ? await loader.discover(node) : (node.children ?? []);
+ for (const child of children) {
+ if (child.type === "connection-group" || child.type === "connection" || containerTypes.has(child.type) || linkedContainerTypes.has(child.type) || sidebarIndexedObjectGroupTypes.has(child.type)) await visit(child, path);
+ }
+ } catch (error) {
+ if (!shouldCancel()) loader.onError(node, error);
+ }
+ };
+ for (const root of roots) await visit(root, []);
+ return results;
+}
diff --git a/apps/desktop/src/lib/sidebar/sidebarRegexSearchIndex.ts b/apps/desktop/src/lib/sidebar/sidebarRegexSearchIndex.ts
index 72cf5d7af1..97e900e391 100644
--- a/apps/desktop/src/lib/sidebar/sidebarRegexSearchIndex.ts
+++ b/apps/desktop/src/lib/sidebar/sidebarRegexSearchIndex.ts
@@ -1,7 +1,7 @@
import type { TableInfo, TreeNode, TreeNodeType } from "@/types/database";
import type { SidebarRegexIndexScope, SidebarRegexScopeIdentity } from "@/lib/sidebar/sidebarSearchTree";
-export const regexLocalTableParentTypes = new Set<TreeNodeType>(["database", "schema", "linked-server-schema", "group-tables"]);
+export const regexLocalTableParentTypes = new Set<TreeNodeType>(["database", "schema", "linked-server-schema", "group-tables", "group-dolt-system-tables", "group-views", "group-materialized-views"]);
export function regexTableSearchParents(nodes: readonly TreeNode[], result: TreeNode[] = []): TreeNode[] {
for (const node of nodes) {
diff --git a/apps/desktop/src/lib/sidebar/sidebarRemoteSearch.ts b/apps/desktop/src/lib/sidebar/sidebarRemoteSearch.ts
new file mode 100644
index 0000000000..4d2370ddb5
--- /dev/null
+++ b/apps/desktop/src/lib/sidebar/sidebarRemoteSearch.ts
@@ -0,0 +1,78 @@
+import type { TreeNode, TreeNodeType } from "@/types/database";
+import { objectTypesForGroupNode } from "@/lib/table/tableTree";
+import { sidebarIndexedObjectGroupTypes } from "@/lib/sidebar/sidebarGlobalSearchIndex";
+import { isSidebarSearchPrunedDatabaseNode } from "@/lib/sidebar/sidebarSearchDatabaseScope";
+import { nodeMatchesRegexScopeIdentity, type SidebarRegexScopeIdentity } from "@/lib/sidebar/sidebarSearchTree";
+
+export interface SidebarRemoteSearchResult extends SidebarRegexScopeIdentity {
+ parentNodeId: string;
+ children: TreeNode[];
+}
+
+const containerTypes = new Set<TreeNodeType>(["connection-group", "connection", "database", "schema", "doris-catalog", "linked-server-root", "linked-server", "linked-server-catalog", "linked-server-schema"]);
+
+export async function collectSidebarRemoteSearchResults(options: {
+ root: TreeNode;
+ connectionId: string;
+ databaseScope: ReadonlySet<string> | null;
+ signal: AbortSignal;
+ isSimpleParent: (node: TreeNode) => boolean;
+ discover: (node: TreeNode) => Promise<readonly TreeNode[]>;
+ search: (node: TreeNode, onProgress: (children: TreeNode[]) => void) => Promise<TreeNode[]>;
+ onProgress?: (results: SidebarRemoteSearchResult[]) => void;
+ onError: (error: unknown) => void;
+}): Promise<SidebarRemoteSearchResult[]> {
+ const results: SidebarRemoteSearchResult[] = [];
+ const resultFor = (node: TreeNode, children: TreeNode[]): SidebarRemoteSearchResult => ({ parentNodeId: node.id, nodeType: node.type, connectionId: options.connectionId, database: node.database ?? "", schema: node.schema, catalog: node.catalog, children });
+ const publish = (result: SidebarRemoteSearchResult) => {
+ const index = results.findIndex(
+ (candidate) => candidate.parentNodeId === result.parentNodeId && candidate.nodeType === result.nodeType && candidate.connectionId === result.connectionId && candidate.database === result.database && candidate.schema === result.schema && candidate.catalog === result.catalog,
+ );
+ if (index >= 0) results[index] = result;
+ else results.push(result);
+ options.onProgress?.([...results]);
+ };
+ const visit = async (node: TreeNode): Promise<void> => {
+ if (options.signal.aborted || isSidebarSearchPrunedDatabaseNode(node, options.databaseScope)) return;
+ if (node.connectionId && node.connectionId !== options.connectionId) return;
+ if (sidebarIndexedObjectGroupTypes.has(node.type)) return;
+ try {
+ if (options.isSimpleParent(node) || (!node.tableName && objectTypesForGroupNode(node.type))) {
+ let published = false;
+ const children = await options.search(node, (partial) => {
+ if (!options.signal.aborted) {
+ published = true;
+ publish(resultFor(node, partial));
+ }
+ });
+ if (!options.signal.aborted && !published) publish(resultFor(node, children));
+ return;
+ }
+ if (!containerTypes.has(node.type)) return;
+ const children = await options.discover(node);
+ for (const child of children) await visit(child);
+ } catch (error) {
+ if (!options.signal.aborted) options.onError(error);
+ }
+ };
+ await visit(options.root);
+ return results;
+}
+
+export function mergeSidebarRemoteSearchResults(nodes: TreeNode[], results: readonly SidebarRemoteSearchResult[]): TreeNode[] {
+ if (!results.length) return nodes;
+ return nodes.map((node) => {
+ const children = node.children ? mergeSidebarRemoteSearchResults(node.children, results) : undefined;
+ const result = results.find((candidate) => nodeMatchesRegexScopeIdentity(node, candidate.parentNodeId, candidate));
+ if (!result) return children === node.children ? node : { ...node, children };
+ const merged = [...(children ?? [])];
+ const identities = new Set(merged.map((child) => child.id));
+ for (const child of result.children) {
+ if (!identities.has(child.id)) {
+ merged.push(child);
+ identities.add(child.id);
+ }
+ }
+ return { ...node, children: merged };
+ });
+}
diff --git a/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts b/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts
index 87a14c4a4d..c8f72c7601 100644
--- a/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts
+++ b/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts
@@ -131,6 +131,7 @@ export interface SidebarRegexIndexScope {
schema?: string;
catalog?: string;
nodeType: string;
+ stale?: boolean;
path?: Array<Pick<TreeNode, "id" | "label" | "type" | "connectionId" | "database" | "catalog" | "schema" | "linkedServer" | "linkedCatalog" | "linkedSchema">>;
entries: TableInfo[];
}
@@ -198,7 +199,7 @@ export function buildSidebarIndexedTableNodes(scope: SidebarRegexIndexScope): Tr
// Grouped object lists include the schema in each child id; simple table
// lists do not. Search must use the same id because persisted pin keys
// include it, even when an index hit has not been loaded into the live tree.
- includeSchemaInId: scope.nodeType === "group-tables",
+ includeSchemaInId: scope.nodeType.startsWith("group-"),
});
}
@@ -254,7 +255,7 @@ function mergeRegexTreeNodes(liveNodes: readonly TreeNode[], indexedNodes: reado
*/
export function mergeSidebarRegexIndexScopes(liveNodes: readonly TreeNode[], scopes: readonly SidebarRegexIndexScope[]): TreeNode[] {
const selected = new Map<string, { rank: number; scope: SidebarRegexIndexScope; entry: TableInfo }>();
- const rankFor = (scope: SidebarRegexIndexScope) => (scope.nodeType === "group-tables" ? 3 : scope.nodeType === "schema" ? 2 : 1);
+ const rankFor = (scope: SidebarRegexIndexScope) => (scope.nodeType.startsWith("group-") ? 3 : scope.nodeType === "schema" ? 2 : 1);
for (const scope of scopes) {
for (const entry of scope.entries) {
const key = `${scope.connectionId}\0${scope.catalog || ""}\0${scope.database}\0${scope.schema || ""}\0${entry.table_type}\0${entry.name}`;
diff --git a/apps/desktop/src/stores/connectionStore.ts b/apps/desktop/src/stores/connectionStore.ts
index 1da535e665..b187573656 100644
--- a/apps/desktop/src/stores/connectionStore.ts
+++ b/apps/desktop/src/stores/connectionStore.ts
@@ -134,6 +134,8 @@ import { spannerDisplayDatabase, spannerSchemaDisplayName } from "@/lib/connecti
import { buildSqlServerDatabaseTreeNodes } from "@/lib/database/sqlServerTree";
import { collapseExpandedTreeNodes, collapseSubtreeDescendants } from "@/lib/sidebar/sidebarTreeCollapse";
import { findNodePathByIdentity, nodeMatchesRegexScopeIdentity, type SidebarRegexScopeIdentity } from "@/lib/sidebar/sidebarSearchTree";
+import { sidebarIndexedObjectGroupTypes } from "@/lib/sidebar/sidebarGlobalSearchIndex";
+import { invalidateSidebarTableSearchBuild, loadOrBuildSidebarTableSearchIndex } from "@/lib/sidebar/sidebarTableSearchIndex";
import { findDatabaseTreeNode } from "@/lib/sidebar/treeRefreshTarget";
import { simpleModeEmptyShellNeedsConfirmedLoad, treeNodeLoadedChildrenContentPresent } from "@/lib/sidebar/treeLoadedChildrenMarker";
import { shouldMarkDisconnected } from "@/lib/connection/connectionHealth";
@@ -165,7 +167,7 @@ import {
type DatabaseObjectTreeKind,
} from "@/lib/table/tableTree";
import { hasTreeNodeDatabaseContext, normalizeCataloglessDatabaseNodes, treeNodeSchemaCachePrefix } from "@/lib/sidebar/treeNodeContext";
-import { decodeSchemaTreeCache, decodeTableSearchIndexManifest, encodeSchemaTreeCache, encodeTableSearchIndexManifest, type TableSearchIndexManifestEntry } from "@/lib/metadata/schemaTreeCache";
+import { decodeSchemaTreeCache, decodeTableSearchIndexManifest, encodeSchemaTreeCache, encodeTableSearchIndexManifest, SCHEMA_TREE_CACHE_TTL_MS, type TableSearchIndexManifestEntry } from "@/lib/metadata/schemaTreeCache";
import { sortSidebarTreeChildrenByNameKeepingTableVGroups, sortSidebarTreeChildrenForParent } from "@/lib/sidebar/sidebarNodeOrdering";
import { connectionSupportsDatabaseUserAdmin } from "@/lib/database/databaseUserAdmin";
import { getTableMetadataCapabilities } from "@/lib/table/tableMetadataCapabilities";
@@ -581,11 +583,13 @@ export const useConnectionStore = defineStore("connection", () => {
const schemaListCache = ref<Record<string, string[]>>({});
const sidebarSearchQuery = ref("");
const sidebarTableSearchQueries = ref<Record<string, string>>({});
- // Local table indexes are immutable snapshots until an explicit refresh.
- // Keep both resolved values and in-flight reads here so regex typing does
- // not reread SQLite for every keypress or issue duplicate concurrent loads.
- const sidebarTableSearchIndexCache = new Map<string, TableInfo[] | null>();
- const sidebarTableSearchIndexInFlight = new Map<string, Promise<TableInfo[] | null>>();
+ type SidebarTableSearchIndexCacheEntry = { entries: TableInfo[] | null; stale: boolean; staleAt: number };
+ // Cached indexes are published immediately, then ordinary global search can
+ // revalidate them in the background. Keep resolved values and in-flight
+ // reads here so typing does not reread SQLite or duplicate cache reads.
+ const sidebarTableSearchIndexCache = new Map<string, SidebarTableSearchIndexCacheEntry>();
+ const sidebarTableSearchIndexInFlight = new Map<string, Promise<SidebarTableSearchIndexCacheEntry>>();
+ const sidebarTableSearchBuildingScopes = new Map<string, SidebarTableSearchIndexScope>();
const sidebarTableSearchIndexConnectionGenerations = new Map<string, number>();
const sidebarTableSearchIndexScopeGenerations = new Map<string, number>();
const sidebarTableSearchIndexPersistenceQueues = new Map<string, Promise<void>>();
@@ -715,6 +719,7 @@ export const useConnectionStore = defineStore("connection", () => {
ttlMs: METADATA_LIST_PAGE_CACHE_TTL_MS,
maxEntries: METADATA_LIST_PAGE_CACHE_MAX_ENTRIES,
});
+ const sidebarObjectSearchRevision = ref(0);
const metadataTraceLogger: MetadataLoadTraceLogger = (event) => {
console.debug("[DBX][metadata-load:trace]", event);
};
@@ -2661,12 +2666,15 @@ export const useConnectionStore = defineStore("connection", () => {
}
function invalidateMetadataCaches(match: MetadataCacheInvalidation): number {
+ const { tableName: _tableName, ...searchMatch } = match;
+ const invalidatedSearchPages = metadataListPageCache.invalidate({ ...searchMatch, kind: "sidebar-object-search-page" });
+ sidebarObjectSearchRevision.value += 1;
for (const config of connections.value) {
if (config.db_type === "tdengine" && (!match.connectionId || match.connectionId === config.id)) {
tdengineTreeCacheRevisions.set(config.id, (tdengineTreeCacheRevisions.get(config.id) ?? 0) + 1);
}
}
- return metadataListPageCache.invalidate(match) + invalidateTableMetadataCache(match) + invalidateObjectBrowserRowsCache(match);
+ return invalidatedSearchPages + metadataListPageCache.invalidate(match) + invalidateTableMetadataCache(match) + invalidateObjectBrowserRowsCache(match);
}
function invalidateMetadataCachesByTreePrefix(prefix: string) {
@@ -3020,6 +3028,82 @@ export const useConnectionStore = defineStore("connection", () => {
};
}
+ async function searchSidebarObjects(node: TreeNode, query: string, signal: AbortSignal, onProgress?: (children: TreeNode[]) => void): Promise<TreeNode[]> {
+ const revision = sidebarObjectSearchRevision.value;
+ const connectionId = node.connectionId;
+ if (!query.trim() || !connectionId || node.database == null || !connectedIds.value.has(connectionId) || signal.aborted) return [];
+ const config = getConfig(connectionId);
+ const groupTypes = objectTypesForGroupNode(node.type);
+ const objectTypes = (groupTypes ?? sidebarObjectTypesForScope(config, node.database, node.schema)).filter((type) => type !== "TABLE" && type !== "VIEW" && type !== "MATERIALIZED_VIEW");
+ if (!objectTypes.length) return [];
+ const querySchema = connectionObjectTreeQuerySchema(config, node.database, node.schema);
+ const effectiveSchema = connectionObjectTreeNodeSchema(config, node.database, node.schema);
+ const pageSize = sidebarObjectGroupPageSize();
+ let objects: ObjectInfo[] = [];
+ const childrenFromObjects = () => {
+ const children = groupTypes
+ ? objectGroupChildrenFromObjects({ node, parentNodeId: objectGroupRefreshParentId(node) ?? node.id, effectiveSchema, objectTypes, objects })
+ : markPackageNodesExpandable(buildSimpleObjectTreeNodes({ nodeId: node.id, connectionId, database: node.database!, schema: effectiveSchema, objects, databaseType: effectiveDatabaseTypeForConnection(config) }));
+ return children.map((child) => ({ ...child, catalog: node.catalog, linkedServer: node.linkedServer, linkedCatalog: node.linkedCatalog, linkedSchema: node.linkedSchema }));
+ };
+ const publish = () => {
+ if (!signal.aborted) onProgress?.(childrenFromObjects());
+ };
+ const pageScope = (offset: number) => metadataListCacheScope({ kind: "sidebar-object-search-page", connectionId, database: node.database!, schema: querySchema, nodeKind: node.type, objectTypes, searchFilter: query, limit: pageSize, offset, extra: { catalog: node.catalog ?? "" } });
+ const loadRemotePage = (offset: number, force: boolean) =>
+ loadCachedMetadataListPage<ObjectInfo[]>(
+ pageScope(offset),
+ async () => {
+ signal.throwIfAborted();
+ const executionId = `sidebar-search-${uuid()}`;
+ const cancel = () => {
+ void api.cancelQuery(executionId).catch(() => false);
+ };
+ signal.addEventListener("abort", cancel, { once: true });
+ try {
+ const rows = await api.listObjects(connectionId, node.database!, querySchema, objectTypes, query, pageSize, offset, node.catalog, undefined, executionId);
+ signal.throwIfAborted();
+ if (revision !== sidebarObjectSearchRevision.value) throw new DOMException("Search metadata invalidated", "AbortError");
+ return rows;
+ } finally {
+ signal.removeEventListener("abort", cancel);
+ }
+ },
+ { force },
+ );
+
+ let offset = 0;
+ let refreshFromStart = false;
+
+ // Publish consecutive cached pages immediately. A stale page remains useful
+ // as the first paint. Any cached snapshot is then revalidated from zero so
+ // a fresh-but-incomplete cache cannot hide objects added outside the app.
+ while (!signal.aborted && connectedIds.value.has(connectionId)) {
+ const cached = metadataListPageCache.get(pageScope(offset), { allowStale: true });
+ if (!cached) break;
+ refreshFromStart = true;
+ objects.push(...(cached.value as ObjectInfo[]));
+ publish();
+ if (cached.stale || cached.value.length < pageSize) break;
+ offset += cached.value.length;
+ }
+
+ if (refreshFromStart) {
+ objects = [];
+ offset = 0;
+ }
+ while (!signal.aborted && connectedIds.value.has(connectionId)) {
+ const page = await loadRemotePage(offset, refreshFromStart);
+ signal.throwIfAborted();
+ objects.push(...page);
+ publish();
+ if (page.length < pageSize) break;
+ offset += page.length;
+ }
+ if (signal.aborted || !connectedIds.value.has(connectionId)) return [];
+ return childrenFromObjects();
+ }
+
async function loadPagedSimpleTableChildren(options: {
nodeId: string;
connectionId: string;
@@ -3319,7 +3403,7 @@ export const useConnectionStore = defineStore("connection", () => {
function sidebarTableSearchTreeCacheKey(parent: TreeNode): string | null {
if (!parent.connectionId || !hasTreeNodeDatabaseContext(parent)) return null;
- if (parent.type === "group-tables") return objectGroupCacheKey(parent);
+ if (sidebarIndexedObjectGroupTypes.has(parent.type)) return objectGroupCacheKey(parent);
if (parent.type !== "database" && parent.type !== "schema" && parent.type !== "linked-server-schema") return null;
const simpleObjectDisplay = useSettingsStore().editorSettings.sidebarObjectDisplay === "simple";
const cacheVersion = ownerAwareMetadataCacheVersion(getConfig(parent.connectionId), simpleObjectDisplay ? "objects-simple-v9" : "objects-grouped-v9", parent.schema);
@@ -3338,17 +3422,18 @@ export const useConnectionStore = defineStore("connection", () => {
return `${treeCacheKey}:table-search-index-v1`;
}
- function getSidebarTableSearchIndexMemoryCache(cacheKey: string): TableInfo[] | null | undefined {
+ function getSidebarTableSearchIndexMemoryCache(cacheKey: string): SidebarTableSearchIndexCacheEntry | undefined {
if (!sidebarTableSearchIndexCache.has(cacheKey)) return undefined;
- const cached = sidebarTableSearchIndexCache.get(cacheKey) ?? null;
+ const cached = sidebarTableSearchIndexCache.get(cacheKey)!;
+ const current = cached.stale || Date.now() >= cached.staleAt ? { ...cached, stale: true } : cached;
sidebarTableSearchIndexCache.delete(cacheKey);
- sidebarTableSearchIndexCache.set(cacheKey, cached);
- return cached;
+ sidebarTableSearchIndexCache.set(cacheKey, current);
+ return current;
}
- function setSidebarTableSearchIndexMemoryCache(cacheKey: string, entries: TableInfo[] | null) {
+ function setSidebarTableSearchIndexMemoryCache(cacheKey: string, value: SidebarTableSearchIndexCacheEntry) {
sidebarTableSearchIndexCache.delete(cacheKey);
- sidebarTableSearchIndexCache.set(cacheKey, entries);
+ sidebarTableSearchIndexCache.set(cacheKey, value);
while (sidebarTableSearchIndexCache.size > SIDEBAR_TABLE_SEARCH_INDEX_CACHE_MAX_ENTRIES) {
const oldest = sidebarTableSearchIndexCache.keys().next().value;
if (oldest === undefined) break;
@@ -3470,14 +3555,13 @@ export const useConnectionStore = defineStore("connection", () => {
function manifestTableSearchScopeMatchesTreeRefresh(scope: TableSearchIndexManifestEntry, node: TreeNode): boolean {
if (!node.connectionId || scope.connectionId !== node.connectionId) return false;
- const pathMatch = scope.path?.some((pathNode) => pathNode.id === node.id && pathNode.type === node.type && pathNode.connectionId === node.connectionId);
+ const pathMatch = scope.path?.some((pathNode) => pathNode.id === node.id && pathNode.type === node.type && pathNode.connectionId === node.connectionId && pathNode.database === node.database && pathNode.schema === node.schema && pathNode.catalog === node.catalog);
if (pathMatch) return true;
if (node.type === "connection") return true;
if (node.type === "doris-catalog") return scope.catalog === node.catalog;
if (node.type === "database") return scope.database === node.database && scope.catalog === node.catalog;
if (node.type === "schema") return scope.database === node.database && scope.schema === node.schema && scope.catalog === node.catalog;
- if (node.type === "linked-server-schema") return scope.parentNodeId === node.id && scope.nodeType === node.type;
- if (node.type === "group-tables") return scope.parentNodeId === node.id && scope.nodeType === node.type;
+ if (node.type === "linked-server-schema" || sidebarIndexedObjectGroupTypes.has(node.type)) return nodeMatchesRegexScopeIdentity(node, scope.parentNodeId, scope);
return false;
}
@@ -3510,6 +3594,7 @@ export const useConnectionStore = defineStore("connection", () => {
sidebarTableSearchIndexScopeGenerations.set(cacheKey, sidebarTableSearchIndexScopeGeneration(cacheKey) + 1);
sidebarTableSearchIndexCache.delete(cacheKey);
sidebarTableSearchIndexInFlight.delete(cacheKey);
+ invalidateSidebarTableSearchBuild(cacheKey);
}
const deletePersisted = Promise.all(
@@ -3555,7 +3640,8 @@ export const useConnectionStore = defineStore("connection", () => {
}));
const connectionNode = findConnectionNode(connectionId);
const activeScopes = connectionNode ? collectActiveTableSearchScopes(connectionNode) : [];
- await invalidateSidebarTableSearchIndexScopes([...manifestScopes, ...activeScopes], (scope) => scope.connectionId === connectionId);
+ const buildingScopes = [...sidebarTableSearchBuildingScopes.values()].filter((scope) => scope.connectionId === connectionId);
+ await invalidateSidebarTableSearchIndexScopes([...manifestScopes, ...activeScopes, ...buildingScopes], (scope) => scope.connectionId === connectionId);
}
async function invalidateSidebarTableSearchIndexesForTreeRefresh(node: TreeNode): Promise<void> {
@@ -3572,7 +3658,8 @@ export const useConnectionStore = defineStore("connection", () => {
cacheKey: scope.cacheKey,
}));
const activeScopes = collectActiveTableSearchScopes(node);
- await invalidateSidebarTableSearchIndexScopes([...manifestScopes, ...activeScopes], (scope) => manifestTableSearchScopeMatchesTreeRefresh(scope, node));
+ const buildingScopes = [...sidebarTableSearchBuildingScopes.values()].filter((scope) => manifestTableSearchScopeMatchesTreeRefresh(scope, node));
+ await invalidateSidebarTableSearchIndexScopes([...manifestScopes, ...activeScopes, ...buildingScopes], (scope) => manifestTableSearchScopeMatchesTreeRefresh(scope, node));
}
async function registerSidebarTableSearchIndexScope(parent: TreeNode, cacheKey: string): Promise<void> {
@@ -3591,7 +3678,7 @@ export const useConnectionStore = defineStore("connection", () => {
await sidebarTableSearchIndexManifestWriteQueue;
}
- async function readSidebarTableSearchIndexCache(cacheKey: string, connectionId: string): Promise<TableInfo[] | null> {
+ async function readSidebarTableSearchIndexCache(cacheKey: string, connectionId: string): Promise<SidebarTableSearchIndexCacheEntry> {
const cached = getSidebarTableSearchIndexMemoryCache(cacheKey);
if (cached !== undefined) return cached;
const pending = sidebarTableSearchIndexInFlight.get(cacheKey);
@@ -3602,9 +3689,11 @@ export const useConnectionStore = defineStore("connection", () => {
const decoded = decodeSchemaTreeCache<TreeNode[]>(await api.loadSchemaCache<unknown>(cacheKey).catch(() => null));
const index = decoded?.tableSearchIndex;
const entries = index ? index.entries.map((entry) => ({ name: entry.name, table_type: entry.tableType, ...(entry.comment !== undefined ? { comment: entry.comment } : {}) })) : null;
- if (!sidebarTableSearchIndexGenerationIsCurrent(connectionId, cacheKey, connectionGeneration, scopeGeneration)) return null;
- setSidebarTableSearchIndexMemoryCache(cacheKey, entries);
- return entries;
+ const indexedAt = index ? Date.parse(index.indexedAt) : Number.NaN;
+ const value = { entries, stale: decoded?.isStale ?? false, staleAt: Number.isFinite(indexedAt) ? indexedAt + SCHEMA_TREE_CACHE_TTL_MS : Date.now() };
+ if (!sidebarTableSearchIndexGenerationIsCurrent(connectionId, cacheKey, connectionGeneration, scopeGeneration)) return { entries: null, stale: false, staleAt: Date.now() };
+ setSidebarTableSearchIndexMemoryCache(cacheKey, value);
+ return value;
})();
sidebarTableSearchIndexInFlight.set(cacheKey, read);
try {
@@ -3626,22 +3715,49 @@ export const useConnectionStore = defineStore("connection", () => {
if (!parent) return null;
const cacheKey = sidebarTableSearchIndexCacheKey(parent);
if (!cacheKey) return null;
- const entries = await readSidebarTableSearchIndexCache(cacheKey, parent.connectionId || "");
+ const { entries } = await readSidebarTableSearchIndexCache(cacheKey, parent.connectionId || "");
if (entries) await registerSidebarTableSearchIndexScope(parent, cacheKey);
return entries;
}
- async function loadSidebarTableSearchIndexScopes(): Promise<Array<{ scope: TableSearchIndexManifestEntry; entries: TableInfo[] }>> {
+ async function loadSidebarTableSearchIndexScopes(includeScope: (scope: TableSearchIndexManifestEntry) => boolean = () => true): Promise<Array<{ scope: TableSearchIndexManifestEntry; entries: TableInfo[]; stale: boolean }>> {
const manifest = await loadSidebarTableSearchIndexManifest();
- const scopes: Array<{ scope: TableSearchIndexManifestEntry; entries: TableInfo[] }> = [];
+ const scopes: Array<{ scope: TableSearchIndexManifestEntry; entries: TableInfo[]; stale: boolean }> = [];
for (const scope of manifest) {
- const entries = await readSidebarTableSearchIndexCache(scope.cacheKey, scope.connectionId);
- if (entries) scopes.push({ scope, entries });
+ if (!includeScope(scope)) continue;
+ const cached = await readSidebarTableSearchIndexCache(scope.cacheKey, scope.connectionId);
+ if (cached.entries) scopes.push({ scope, entries: cached.entries, stale: cached.stale });
}
return scopes;
}
+ async function ensureSidebarTableSearchIndex(parentNodeId: string, query: string, identity?: SidebarRegexScopeIdentity, options?: { refresh?: boolean; connectedOnly?: boolean }): Promise<TableInfo[] | null> {
+ const parent = identity ? findSidebarTreeNodeByIdentity(parentNodeId, identity) : findNode(treeNodes.value, parentNodeId);
+ if (!parent) return null;
+ const cacheKey = sidebarTableSearchIndexCacheKey(parent);
+ if (!cacheKey) return null;
+ return loadOrBuildSidebarTableSearchIndex(
+ cacheKey,
+ query,
+ () => loadSidebarTableSearchIndex(parentNodeId, identity),
+ () => (options?.connectedOnly && !connectedIds.value.has(parent.connectionId || "") ? Promise.resolve(null) : refreshSidebarTableSearchIndex(parentNodeId, identity)),
+ options?.refresh,
+ );
+ }
+
async function refreshSidebarTableSearchIndex(parentNodeId: string, identity?: SidebarRegexScopeIdentity): Promise<TableInfo[] | null> {
+ const parent = identity ? findSidebarTreeNodeByIdentity(parentNodeId, identity) : findNode(treeNodes.value, parentNodeId);
+ const scope = parent && sidebarTableSearchIndexScopeForNode(parent);
+ if (!scope) return null;
+ sidebarTableSearchBuildingScopes.set(scope.cacheKey, scope);
+ try {
+ return await buildSidebarTableSearchIndex(parentNodeId, identity);
+ } finally {
+ if (sidebarTableSearchBuildingScopes.get(scope.cacheKey) === scope) sidebarTableSearchBuildingScopes.delete(scope.cacheKey);
+ }
+ }
+
+ async function buildSidebarTableSearchIndex(parentNodeId: string, identity?: SidebarRegexScopeIdentity): Promise<TableInfo[] | null> {
const parent = identity ? findSidebarTreeNodeByIdentity(parentNodeId, identity) : findNode(treeNodes.value, parentNodeId);
if (!parent?.connectionId || !hasTreeNodeDatabaseContext(parent)) return [];
const connectionId = parent.connectionId;
@@ -3657,7 +3773,7 @@ export const useConnectionStore = defineStore("connection", () => {
await ensureConnected(connectionId);
const config = getConfig(connectionId);
const querySchema = connectionObjectTreeQuerySchema(config, parent.database, parent.schema);
- const objectTypes = parent.type === "group-tables" ? (objectTypesForGroupNode(parent.type) ?? undefined) : undefined;
+ const objectTypes = sidebarIndexedObjectGroupTypes.has(parent.type) ? (objectTypesForGroupNode(parent.type) ?? undefined) : undefined;
const pageSize = sidebarObjectGroupPageSize();
const entries: TableInfo[] = [];
for (let offset = 0; ; offset += pageSize) {
@@ -3673,18 +3789,18 @@ export const useConnectionStore = defineStore("connection", () => {
entries: deduped.map((entry) => ({ name: entry.name, tableType: entry.table_type, ...(entry.comment !== undefined ? { comment: entry.comment } : {}) })),
};
if (!sidebarTableSearchIndexGenerationIsCurrent(connectionId, cacheKey, connectionGeneration, scopeGeneration)) return [];
- let persisted = false;
+ let indexIsCurrent = false;
await serializeSidebarTableSearchIndexPersistence(cacheKey, async () => {
if (!sidebarTableSearchIndexGenerationIsCurrent(connectionId, cacheKey, connectionGeneration, scopeGeneration)) return;
- await api.saveSchemaCache(cacheKey, encodeSchemaTreeCache<TreeNode[]>([], Date.now(), tableSearchIndex));
+ await api.saveSchemaCache(cacheKey, encodeSchemaTreeCache<TreeNode[]>([], Date.now(), tableSearchIndex)).catch(() => undefined);
if (!sidebarTableSearchIndexGenerationIsCurrent(connectionId, cacheKey, connectionGeneration, scopeGeneration)) {
await api.deleteSchemaCachePrefix(cacheKey).catch(() => undefined);
return;
}
- persisted = true;
+ indexIsCurrent = true;
});
- if (!persisted || !sidebarTableSearchIndexGenerationIsCurrent(connectionId, cacheKey, connectionGeneration, scopeGeneration)) return [];
- setSidebarTableSearchIndexMemoryCache(cacheKey, deduped);
+ if (!indexIsCurrent || !sidebarTableSearchIndexGenerationIsCurrent(connectionId, cacheKey, connectionGeneration, scopeGeneration)) return [];
+ setSidebarTableSearchIndexMemoryCache(cacheKey, { entries: deduped, stale: false, staleAt: Date.now() + SCHEMA_TREE_CACHE_TTL_MS });
await registerSidebarTableSearchIndexScope(parent, cacheKey);
return deduped;
}
@@ -11056,6 +11172,9 @@ export const useConnectionStore = defineStore("connection", () => {
setSidebarTableSearchQuery,
refreshSidebarTableSearch,
loadSidebarTableSearchIndex,
+ ensureSidebarTableSearchIndex,
+ searchSidebarObjects,
+ sidebarObjectSearchRevision,
loadSidebarTableSearchIndexScopes,
refreshSidebarTableSearchIndex,
createConnectionGroup(name: string, parentGroupId?: string | null) {
diff --git a/docs/content/docs/schema-browser.mdx b/docs/content/docs/schema-browser.mdx
index b5a184acf3..3f77f49258 100644
--- a/docs/content/docs/schema-browser.mdx
+++ b/docs/content/docs/schema-browser.mdx
@@ -59,9 +59,15 @@ Object-tree search can match:
- Connections, groups, databases, catalogs, and schemas
- Tables, views, columns, indexes, and selected database-specific objects
- Object paths, column comments, and table comments
-- Loaded metadata and table lists that support server-side filtering
+- Loaded metadata and complete cached table/view indexes
-Some searches pass the keyword to the driver with debouncing, avoiding a full table load first. You can also restrict search by node type or use local table search inside the active database.
+The main sidebar search reuses table/view indexes from memory or the persisted schema cache. When an index is missing, the first search builds it for the eligible scopes in the active, connected datasource. Further edits filter the index locally without reloading the datasource tree. The “Search only opened databases” setting controls which databases are indexed; when none are open, all visible databases are eligible.
+
+Other supported object types (including procedures, functions, triggers, sequences, and packages) use debounced, on-demand remote search, even when their group has never been expanded or the object is beyond its first page. Matching pages are cached in memory by keyword and full scope; filtered results are not treated as a complete schema index. Changing the input cancels the previous remote request and discards late results. Search results do not replace the loaded browsing tree.
+
+Enable **Local search** to search loaded objects and existing cached indexes without building missing indexes or making remote requests. Regex search also uses existing indexes only. You can restrict results by node type or use the separate table search inside a database.
+
+Use **Refresh** to update cached metadata after external schema changes. Database and schema refreshes, including refreshes triggered by DBX object changes, invalidate the affected indexes.
Visible-scope settings can hide databases, schemas, system schemas, or tables with configured prefixes. Newly created databases or schemas can be added to the visible list automatically.
diff --git a/packages/app-tests/sidebarGlobalSearchIndex.test.ts b/packages/app-tests/sidebarGlobalSearchIndex.test.ts
new file mode 100644
index 0000000000..7b3180aab3
--- /dev/null
+++ b/packages/app-tests/sidebarGlobalSearchIndex.test.ts
@@ -0,0 +1,136 @@
+import { expect, test, vi } from "vitest";
+import { collectSidebarGlobalSearchIndexes, sidebarIndexedObjectGroupTypes, type SidebarGlobalSearchIndexLoader } from "../../apps/desktop/src/lib/sidebar/sidebarGlobalSearchIndex.ts";
+import type { SidebarRegexIndexScope } from "../../apps/desktop/src/lib/sidebar/sidebarSearchTree.ts";
+import type { TreeNode } from "../../apps/desktop/src/types/database.ts";
+
+function fixture() {
+ const tables: TreeNode = { id: "conn:app:public:tables", type: "group-tables", label: "Tables", connectionId: "conn", database: "app", schema: "public", children: [], isExpanded: false };
+ const schema: TreeNode = { id: "conn:app:public", type: "schema", label: "public", connectionId: "conn", database: "app", schema: "public", children: [tables], isExpanded: false };
+ const database: TreeNode = { id: "conn:app", type: "database", label: "app", connectionId: "conn", database: "app", children: [schema], isExpanded: false };
+ const root: TreeNode = { id: "conn", type: "connection", label: "Connection", connectionId: "conn", children: [database], isExpanded: false };
+ const cached: SidebarRegexIndexScope = { parentNodeId: tables.id, connectionId: "conn", database: "app", schema: "public", nodeType: "group-tables", path: [root, database, schema, tables].map(({ children: _children, ...node }) => node), entries: [{ name: "late_table", table_type: "TABLE" }] };
+ const loader = {
+ loadCachedScopes: vi.fn(async () => [] as SidebarRegexIndexScope[]),
+ isIndexParent: (node: TreeNode) => sidebarIndexedObjectGroupTypes.has(node.type),
+ discover: vi.fn(async (node: TreeNode) => node.children ?? []),
+ loadIndex: vi.fn(async () => cached.entries),
+ onError: vi.fn(),
+ } satisfies SidebarGlobalSearchIndexLoader;
+ const run = (databaseScope: ReadonlySet<string> | null = null, shouldCancel = () => false) => collectSidebarGlobalSearchIndexes(loader, [root], "conn", databaseScope, shouldCancel);
+ return { root, database, schema, tables, cached, loader, run };
+}
+
+test("builds a complete index once per leaf scope without loading table children", async () => {
+ const { root, tables, loader, run } = fixture();
+ const before = structuredClone(root);
+ const results = await run();
+ expect(results[0].entries[0].name).toBe("late_table");
+ expect(loader.loadIndex).toHaveBeenCalledExactlyOnceWith(tables);
+ expect(loader.discover).not.toHaveBeenCalledWith(tables);
+ expect(root).toEqual(before);
+});
+
+test("uses persisted scopes when a collapsed branch is absent from the live tree", async () => {
+ const { root, cached, loader, run } = fixture();
+ root.children = [];
+ loader.loadCachedScopes.mockResolvedValue([cached]);
+ const results = await run();
+ expect(results[0].entries).toEqual(cached.entries);
+ expect(loader.loadIndex).not.toHaveBeenCalled();
+ expect(root.children).toEqual([]);
+});
+
+test("publishes a cached scope first and replaces it after background revalidation", async () => {
+ const { cached, loader, run, tables } = fixture();
+ const cachedFirst = { ...cached, stale: false, entries: [{ name: "Photos", table_type: "TABLE" }] };
+ const refreshed = [
+ { name: "Photos", table_type: "TABLE" },
+ { name: "Photos_Gallery", table_type: "TABLE" },
+ ];
+ loader.loadCachedScopes.mockResolvedValue([cachedFirst]);
+ loader.loadIndex.mockResolvedValue(refreshed);
+ const progress: SidebarRegexIndexScope[][] = [];
+
+ const results = await collectSidebarGlobalSearchIndexes(
+ loader,
+ [
+ {
+ id: "conn",
+ type: "connection",
+ label: "Connection",
+ connectionId: "conn",
+ children: [{ id: "conn:app", type: "database", label: "app", connectionId: "conn", database: "app", children: [{ id: "conn:app:public", type: "schema", label: "public", connectionId: "conn", database: "app", schema: "public", children: [tables] }] }],
+ },
+ ],
+ "conn",
+ null,
+ () => false,
+ (scopes) => progress.push(scopes),
+ );
+
+ expect(progress[0][0].entries.map((entry) => entry.name)).toEqual(["Photos"]);
+ expect(loader.loadIndex).toHaveBeenCalledExactlyOnceWith(tables, { refresh: true });
+ expect(results[0].entries.map((entry) => entry.name)).toEqual(["Photos", "Photos_Gallery"]);
+ expect(progress.at(-1)?.[0].entries.map((entry) => entry.name)).toEqual(["Photos", "Photos_Gallery"]);
+});
+
+test("retains a cached scope when background revalidation fails", async () => {
+ const { cached, loader, run } = fixture();
+ const cachedFirst = { ...cached, stale: false, entries: [{ name: "Photos", table_type: "TABLE" }] };
+ loader.loadCachedScopes.mockResolvedValue([cachedFirst]);
+ loader.loadIndex.mockRejectedValue(new Error("offline"));
+
+ const results = await run();
+
+ expect(results).toEqual([cachedFirst]);
+ expect(loader.onError).toHaveBeenCalledOnce();
+});
+
+test("restricts cached and missing scopes to opened databases", async () => {
+ const { root, database, cached, loader, run } = fixture();
+ root.children?.push({ ...database, id: "conn:other", database: "other", children: [] });
+ loader.loadCachedScopes.mockResolvedValue([cached]);
+ const results = await run(new Set(["conn:other"]));
+ expect(results).toEqual([]);
+ expect(loader.discover).not.toHaveBeenCalledWith(database);
+ expect(loader.loadIndex).not.toHaveBeenCalled();
+});
+
+test("discovers a missing namespace before indexing its tables", async () => {
+ const { database, schema, tables, loader, run } = fixture();
+ database.children = [];
+ loader.discover.mockImplementation(async (node) => (node === database ? [schema] : (node.children ?? [])));
+ const results = await run();
+ expect(loader.loadIndex).toHaveBeenCalledExactlyOnceWith(tables);
+ expect(results[0].path?.map((node) => node.id)).toEqual(["conn", "conn:app", "conn:app:public", tables.id]);
+});
+
+test("does not index a different connection", async () => {
+ const { root, database, loader } = fixture();
+ const other: TreeNode = { ...root, id: "other", connectionId: "other", children: [{ ...database, connectionId: "other" }] };
+ await collectSidebarGlobalSearchIndexes(loader, [other], "conn", null, () => false);
+ expect(loader.loadIndex).not.toHaveBeenCalled();
+ expect(loader.discover).not.toHaveBeenCalled();
+});
+
+test("keeps successful scopes when another schema fails", async () => {
+ const { schema, tables, loader, run } = fixture();
+ const views: TreeNode = { ...tables, id: "conn:app:public:views", type: "group-views" };
+ schema.children?.push(views);
+ loader.loadIndex.mockRejectedValueOnce(new Error("access denied"));
+ const results = await run();
+ expect(loader.onError).toHaveBeenCalledTimes(1);
+ expect(results.map((scope) => scope.parentNodeId)).toEqual([views.id]);
+});
+
+test("stops discovery and discards results when disconnected during a build", async () => {
+ const { schema, tables, loader, run } = fixture();
+ schema.children?.push({ ...tables, id: "views", type: "group-views" });
+ let cancelled = false;
+ loader.loadIndex.mockImplementation(async () => {
+ cancelled = true;
+ return [];
+ });
+ expect(await run(null, () => cancelled)).toEqual([]);
+ expect(loader.loadIndex).toHaveBeenCalledTimes(1);
+});
diff --git a/packages/app-tests/sidebarRegexIndexStore.test.ts b/packages/app-tests/sidebarRegexIndexStore.test.ts
index f586f44ff1..13520a8ecc 100644
--- a/packages/app-tests/sidebarRegexIndexStore.test.ts
+++ b/packages/app-tests/sidebarRegexIndexStore.test.ts
@@ -1,12 +1,12 @@
import assert from "node:assert/strict";
-import { beforeEach, test, vi } from "vitest";
+import { beforeEach, expect, test, vi } from "vitest";
import { createPinia, setActivePinia } from "pinia";
import { decodeSchemaTreeCache, encodeSchemaTreeCache, encodeTableSearchIndexManifest, type TableSearchIndexManifestEntry } from "../../apps/desktop/src/lib/metadata/schemaTreeCache.ts";
import type { ConnectionConfig, TreeNode } from "../../apps/desktop/src/types/database.ts";
const MANIFEST_CACHE_KEY = "dbx:sidebar-table-search-index-manifest-v1";
-const { loadSchemaCacheMock, saveSchemaCacheMock, deleteSchemaCachePrefixMock, saveConnectionsMock, listTablesMock, checkConnectionHealthMock, persistedCache } = vi.hoisted(() => {
+const { loadSchemaCacheMock, saveSchemaCacheMock, deleteSchemaCachePrefixMock, saveConnectionsMock, listTablesMock, listObjectsMock, cancelQueryMock, checkConnectionHealthMock, persistedCache } = vi.hoisted(() => {
const persisted = new Map<string, unknown>();
return {
loadSchemaCacheMock: vi.fn(async (cacheKey: string) => persisted.get(cacheKey) ?? null),
@@ -20,6 +20,8 @@ const { loadSchemaCacheMock, saveSchemaCacheMock, deleteSchemaCachePrefixMock, s
}),
saveConnectionsMock: vi.fn(async () => undefined),
listTablesMock: vi.fn(),
+ listObjectsMock: vi.fn(),
+ cancelQueryMock: vi.fn(async () => true),
checkConnectionHealthMock: vi.fn(async () => undefined),
persistedCache: persisted,
};
@@ -34,11 +36,143 @@ vi.mock("../../apps/desktop/src/lib/backend/api", async (importOriginal) => {
deleteSchemaCachePrefix: deleteSchemaCachePrefixMock,
saveConnections: saveConnectionsMock,
listTables: listTablesMock,
+ listObjects: listObjectsMock,
+ cancelQuery: cancelQueryMock,
checkConnectionHealth: checkConnectionHealthMock,
};
});
import { useConnectionStore } from "../../apps/desktop/src/stores/connectionStore.ts";
+import { useSettingsStore } from "../../apps/desktop/src/stores/settingsStore.ts";
+import type { ObjectInfo } from "../../apps/desktop/src/types/database.ts";
+
+async function withRemoteSearchFixture(run: (store: ReturnType<typeof useConnectionStore>, group: TreeNode, controller: AbortController) => Promise<void>) {
+ const restoreStorage = installMemoryStorage();
+ try {
+ setActivePinia(createPinia());
+ const store = useConnectionStore();
+ store.addEphemeralConnection(conn("conn-1"));
+ useSettingsStore().desktopSettings.sidebar_table_page_size = 2;
+ const group: TreeNode = { id: "conn-1:app:public:__procedures", label: "Procedures", type: "group-procedures", connectionId: "conn-1", database: "app", schema: "public", children: [], isExpanded: false };
+ await run(store, group, new AbortController());
+ } finally {
+ restoreStorage();
+ }
+}
+
+test.each(["PROCEDURE", "FUNCTION"])("remote fallback finds an unexpanded %s without replacing live children", async (kind) => {
+ await withRemoteSearchFixture(async (store, group, controller) => {
+ if (kind === "FUNCTION") {
+ group.type = "group-functions";
+ group.id = "conn-1:app:public:__functions";
+ }
+ listObjectsMock.mockResolvedValue([{ name: "late_routine", object_type: kind, schema: "public" }]);
+ const result = await store.searchSidebarObjects(group, "late", controller.signal);
+ expect(result.map((node) => [node.label, node.type])).toEqual([["late_routine", kind.toLowerCase()]]);
+ expect(listObjectsMock.mock.calls[0].slice(0, 7)).toEqual(["conn-1", "app", "public", [kind], "late", 2, 0]);
+ expect(group.children).toEqual([]);
+ expect(group.isExpanded).toBe(false);
+ expect(listTablesMock).not.toHaveBeenCalled();
+ });
+});
+
+test.each(["PROCEDURE", "FUNCTION"])("remote fallback finds %s past the first page and revalidates cached filtered pages", async (kind) => {
+ await withRemoteSearchFixture(async (store, group, controller) => {
+ if (kind === "FUNCTION") {
+ group.type = "group-functions";
+ group.id = "conn-1:app:public:__functions";
+ }
+ const rows = ["target_a", "target_b", "target_z"].map((name) => ({ name, object_type: kind, schema: "public" }));
+ group.children = [{ id: "first", label: "unrelated_first_page", type: kind === "FUNCTION" ? "function" : "procedure" }];
+ listObjectsMock.mockImplementation(async (_connection, _database, _schema, _types, _query, limit, offset) => rows.slice(offset, offset + limit));
+ expect((await store.searchSidebarObjects(group, "target", controller.signal)).map((node) => node.label)).toEqual(rows.map((row) => row.name));
+ expect(listObjectsMock.mock.calls.map((call) => call[6])).toEqual([0, 2]);
+ await store.searchSidebarObjects(group, "target", controller.signal);
+ expect(listObjectsMock.mock.calls.map((call) => call[6])).toEqual([0, 2, 0, 2]);
+ expect(group.children[0].label).toBe("unrelated_first_page");
+ });
+});
+
+test("remote fallback publishes a cached first page before loading a missing page", async () => {
+ await withRemoteSearchFixture(async (store, group) => {
+ const firstPage = ["Photos", "Photos_Album"].map((name) => ({ name, object_type: "PROCEDURE", schema: "public" }));
+ const seedController = new AbortController();
+ listObjectsMock.mockResolvedValueOnce(firstPage);
+ const seeded = store.searchSidebarObjects(group, "photos", seedController.signal, () => seedController.abort());
+ await expect(seeded).resolves.toEqual([]);
+
+ let finishMissingPage!: (objects: ObjectInfo[]) => void;
+ listObjectsMock
+ .mockImplementationOnce(
+ () =>
+ new Promise<ObjectInfo[]>((resolve) => {
+ finishMissingPage = resolve;
+ }),
+ )
+ .mockResolvedValueOnce([{ name: "Photos_Gallery", object_type: "PROCEDURE", schema: "public" }]);
+ const progress: string[][] = [];
+ const pending = store.searchSidebarObjects(group, "photos", new AbortController().signal, (children) => progress.push(children.map((child) => child.label)));
+
+ expect(progress).toEqual([["Photos", "Photos_Album"]]);
+ expect(listObjectsMock.mock.calls.at(-1)?.[6]).toBe(0);
+ finishMissingPage(firstPage);
+ expect((await pending).map((child) => child.label)).toEqual(["Photos", "Photos_Album", "Photos_Gallery"]);
+ expect(progress.at(-1)).toEqual(["Photos", "Photos_Album", "Photos_Gallery"]);
+ });
+});
+
+test("filtered cache is isolated by query and catalog and invalidated after an object change", async () => {
+ await withRemoteSearchFixture(async (store, group, controller) => {
+ listObjectsMock.mockResolvedValue([]);
+ await store.searchSidebarObjects(group, "first", controller.signal);
+ await store.searchSidebarObjects(group, "second", controller.signal);
+ await store.searchSidebarObjects({ ...group, catalog: "other" }, "first", controller.signal);
+ expect(listObjectsMock).toHaveBeenCalledTimes(3);
+ store.invalidateMetadataCache("conn-1", "app", "public", "routine");
+ await store.searchSidebarObjects(group, "first", controller.signal);
+ expect(listObjectsMock).toHaveBeenCalledTimes(4);
+ });
+});
+
+test("aborts the backend execution, rejects late responses, and never caches cancelled pages", async () => {
+ await withRemoteSearchFixture(async (store, group, controller) => {
+ let resolve!: (objects: ObjectInfo[]) => void;
+ listObjectsMock.mockImplementationOnce(
+ () =>
+ new Promise<ObjectInfo[]>((complete) => {
+ resolve = complete;
+ }),
+ );
+ const pending = store.searchSidebarObjects(group, "old", controller.signal);
+ const rejected = expect(pending).rejects.toMatchObject({ name: "AbortError" });
+ controller.abort();
+ expect(cancelQueryMock).toHaveBeenCalledExactlyOnceWith(listObjectsMock.mock.calls[0][9]);
+ resolve([{ name: "old_result", object_type: "PROCEDURE" }]);
+ await rejected;
+ listObjectsMock.mockResolvedValue([]);
+ await store.searchSidebarObjects(group, "old", new AbortController().signal);
+ expect(listObjectsMock).toHaveBeenCalledTimes(2);
+ });
+});
+
+test("simple-display fallback excludes table/view types and retains routine identity", async () => {
+ await withRemoteSearchFixture(async (store, group, controller) => {
+ const schema: TreeNode = { ...group, id: "conn-1:app:public", type: "schema" };
+ listObjectsMock.mockResolvedValue([{ name: "routine", object_type: "FUNCTION", signature: "integer", schema: "public" }]);
+ const result = await store.searchSidebarObjects(schema, "routine", controller.signal);
+ expect(result[0]).toMatchObject({ type: "function", objectName: "routine", signature: "integer" });
+ expect(listObjectsMock.mock.calls[0][3]).not.toEqual(expect.arrayContaining(["TABLE"]));
+ expect(listObjectsMock.mock.calls[0][3]).not.toEqual(expect.arrayContaining(["VIEW"]));
+ });
+});
+
+test("remote fallback never reconnects an offline datasource", async () => {
+ await withRemoteSearchFixture(async (store, group, controller) => {
+ store.connectedIds.delete("conn-1");
+ expect(await store.searchSidebarObjects(group, "routine", controller.signal)).toEqual([]);
+ expect(listObjectsMock).not.toHaveBeenCalled();
+ });
+});
beforeEach(() => {
loadSchemaCacheMock.mockReset();
@@ -46,6 +180,8 @@ beforeEach(() => {
deleteSchemaCachePrefixMock.mockReset();
saveConnectionsMock.mockReset();
listTablesMock.mockReset();
+ listObjectsMock.mockReset();
+ cancelQueryMock.mockClear();
checkConnectionHealthMock.mockReset();
persistedCache.clear();
loadSchemaCacheMock.mockImplementation(async (cacheKey: string) => persistedCache.get(cacheKey) ?? null);
@@ -528,6 +664,154 @@ test("refreshSidebarTableSearchIndex is the only path that paginates listTables"
}
});
+test("global and per-database searches share one build, then reuse memory and persisted indexes", async () => {
+ const restoreStorage = installMemoryStorage();
+ try {
+ setActivePinia(createPinia());
+ const store = useConnectionStore();
+ store.addEphemeralConnection(conn("conn-1"));
+ store.connectedIds.add("conn-1");
+ const { root, groups } = connectionNodeWithTableGroups("app");
+ store.treeNodes.push(root);
+ const rows = Array.from({ length: 501 }, (_, index) => ({ name: `table_${index}`, table_type: "TABLE" }));
+ listTablesMock.mockImplementation(async (_connectionId: string, _database: string, _schema: string, _filter: unknown, limit: number, offset: number) => rows.slice(offset, offset + limit));
+ const global = store.ensureSidebarTableSearchIndex(groups.app.id, "t", undefined, { connectedOnly: true });
+ const local = store.ensureSidebarTableSearchIndex(groups.app.id, "table");
+ expect(await global).toEqual(rows);
+ expect(await local).toEqual(rows);
+ const buildRequests = listTablesMock.mock.calls.length;
+ const cacheReads = loadSchemaCacheMock.mock.calls.length;
+ expect(buildRequests).toBe(Math.ceil(rows.length / listTablesMock.mock.calls[0][4]));
+ for (const query of ["ta", "table_500", "missing", "table"]) {
+ expect(await store.ensureSidebarTableSearchIndex(groups.app.id, query)).toEqual(rows);
+ }
+ expect(listTablesMock).toHaveBeenCalledTimes(buildRequests);
+ expect(loadSchemaCacheMock).toHaveBeenCalledTimes(cacheReads);
+ expect(groups.app.children).toEqual([]);
+
+ setActivePinia(createPinia());
+ const restarted = useConnectionStore();
+ restarted.addEphemeralConnection(conn("conn-1"));
+ restarted.treeNodes.push(connectionNodeWithTableGroups("app").root);
+ expect(await restarted.ensureSidebarTableSearchIndex(groups.app.id, "table_500", undefined, { connectedOnly: true })).toEqual(rows);
+ expect(listTablesMock).toHaveBeenCalledTimes(buildRequests);
+ } finally {
+ restoreStorage();
+ }
+});
+
+test("global search never builds an index for a disconnected datasource", async () => {
+ const restoreStorage = installMemoryStorage();
+ try {
+ setActivePinia(createPinia());
+ const store = useConnectionStore();
+ store.addEphemeralConnection(conn("conn-1"));
+ store.connectedIds.delete("conn-1");
+ const { root, groups } = connectionNodeWithTableGroups("app");
+ store.treeNodes.push(root);
+ expect(await store.ensureSidebarTableSearchIndex(groups.app.id, "table", undefined, { connectedOnly: true })).toBeNull();
+ expect(listTablesMock).not.toHaveBeenCalled();
+ expect(checkConnectionHealthMock).not.toHaveBeenCalled();
+ } finally {
+ restoreStorage();
+ }
+});
+
+test("keeps a completed index in memory when persistence is unavailable", async () => {
+ const restoreStorage = installMemoryStorage();
+ try {
+ setActivePinia(createPinia());
+ const store = useConnectionStore();
+ store.addEphemeralConnection(conn("conn-1"));
+ const { root, groups } = connectionNodeWithTableGroups("app");
+ store.treeNodes.push(root);
+ const rows = [{ name: "orders", table_type: "TABLE" }];
+ listTablesMock.mockResolvedValue(rows);
+ saveSchemaCacheMock.mockRejectedValue(new Error("disk full"));
+ expect(await store.ensureSidebarTableSearchIndex(groups.app.id, "ord")).toEqual(rows);
+ expect(await store.ensureSidebarTableSearchIndex(groups.app.id, "orders")).toEqual(rows);
+ expect(listTablesMock).toHaveBeenCalledTimes(1);
+ } finally {
+ restoreStorage();
+ }
+});
+
+test.each(["group-views", "group-materialized-views"] as const)("indexes and invalidates %s independently from tables", async (nodeType) => {
+ const restoreStorage = installMemoryStorage();
+ try {
+ setActivePinia(createPinia());
+ const store = useConnectionStore();
+ store.addEphemeralConnection(conn("conn-1"));
+ const { root, groups } = connectionNodeWithTableGroups("app");
+ const views: TreeNode = { ...groups.app, id: `${groups.app.id}:views`, type: nodeType };
+ root.children![0].children!.push(views);
+ store.treeNodes.push(root);
+ listTablesMock.mockResolvedValue([{ name: "orders_view", table_type: nodeType === "group-views" ? "VIEW" : "MATERIALIZED_VIEW" }]);
+ const indexed = await store.ensureSidebarTableSearchIndex(views.id, "orders");
+ expect(indexed?.[0].name).toBe("orders_view");
+ expect(listTablesMock.mock.calls[0][6]).toEqual([nodeType === "group-views" ? "VIEW" : "MATERIALIZED_VIEW"]);
+ await store.refreshTreeNode(views);
+ expect(await store.loadSidebarTableSearchIndex(views.id)).toBeNull();
+ } finally {
+ restoreStorage();
+ }
+});
+
+test("refresh invalidates a first build before it has registered a persisted scope", async () => {
+ const restoreStorage = installMemoryStorage();
+ try {
+ setActivePinia(createPinia());
+ const store = useConnectionStore();
+ store.addEphemeralConnection(conn("conn-1"));
+ const { root, groups } = connectionNodeWithTableGroups("app");
+ store.treeNodes.push(root);
+ let finishOld!: (rows: Array<{ name: string; table_type: string }>) => void;
+ const fresh = [{ name: "renamed_orders", table_type: "TABLE" }];
+ listTablesMock
+ .mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ finishOld = resolve;
+ }),
+ )
+ .mockResolvedValue(fresh);
+ const oldBuild = store.ensureSidebarTableSearchIndex(groups.app.id, "ord");
+ await vi.waitFor(() => expect(listTablesMock).toHaveBeenCalledTimes(1));
+ await store.refreshTreeNode(groups.app);
+ const replacement = await store.ensureSidebarTableSearchIndex(groups.app.id, "renamed");
+ finishOld([{ name: "old_orders", table_type: "TABLE" }]);
+ expect(await oldBuild).toEqual([]);
+ expect(replacement).toEqual(fresh);
+ expect(await store.loadSidebarTableSearchIndex(groups.app.id)).toEqual(fresh);
+ } finally {
+ restoreStorage();
+ }
+});
+
+test("build deduplication distinguishes identical node ids in different catalogs", async () => {
+ const restoreStorage = installMemoryStorage();
+ try {
+ setActivePinia(createPinia());
+ const store = useConnectionStore();
+ store.addEphemeralConnection(conn("conn-1"));
+ const { root, groups } = connectionNodeWithTableGroups("app");
+ const first = { ...groups.app, catalog: "hive" };
+ const second = { ...groups.app, catalog: "iceberg" };
+ root.children = [first, second];
+ store.treeNodes.push(root);
+ listTablesMock.mockImplementation(async (...args: unknown[]) => [{ name: `${args[7]}_orders`, table_type: "TABLE" }]);
+ const results = await Promise.all([first, second].map((node) => store.ensureSidebarTableSearchIndex(node.id, "orders", { connectionId: "conn-1", database: "app", schema: "public", catalog: node.catalog, nodeType: node.type })));
+ expect(results.map((entries) => entries?.[0].name)).toEqual(["hive_orders", "iceberg_orders"]);
+ expect(listTablesMock).toHaveBeenCalledTimes(2);
+ await store.refreshTreeNode(first);
+ const identity = { connectionId: "conn-1", database: "app", schema: "public", nodeType: first.type };
+ expect(await store.loadSidebarTableSearchIndex(first.id, { ...identity, catalog: first.catalog })).toBeNull();
+ expect(await store.loadSidebarTableSearchIndex(second.id, { ...identity, catalog: second.catalog })).toEqual(results[1]);
+ } finally {
+ restoreStorage();
+ }
+});
+
test("updating a connection invalidates its in-memory index and manifest scopes", async () => {
const restoreStorage = installMemoryStorage();
try {
diff --git a/packages/app-tests/sidebarRemoteSearch.test.ts b/packages/app-tests/sidebarRemoteSearch.test.ts
new file mode 100644
index 0000000000..7ada662552
--- /dev/null
+++ b/packages/app-tests/sidebarRemoteSearch.test.ts
@@ -0,0 +1,136 @@
+import { expect, test, vi } from "vitest";
+import { collectSidebarRemoteSearchResults, mergeSidebarRemoteSearchResults } from "../../apps/desktop/src/lib/sidebar/sidebarRemoteSearch";
+import type { TreeNode, TreeNodeType } from "../../apps/desktop/src/types/database";
+
+const remoteObjectGroupTypes = [
+ "group-procedures",
+ "group-functions",
+ "group-internal-functions",
+ "group-udf-functions",
+ "group-triggers",
+ "group-events",
+ "group-sequences",
+ "group-database-indexes",
+ "group-synonyms",
+ "group-jobs",
+ "group-packages",
+ "group-types",
+] as const satisfies readonly TreeNodeType[];
+
+const indexedObjectGroupTypes = ["group-tables", "group-dolt-system-tables", "group-views", "group-materialized-views"] as const satisfies readonly TreeNodeType[];
+
+function fixture() {
+ const identity = { connectionId: "conn", database: "app", schema: "public" };
+ const procedures: TreeNode = { ...identity, id: "conn:app:public:__procedures", type: "group-procedures", label: "Procedures", children: [], isExpanded: false };
+ const functions: TreeNode = { ...identity, id: "conn:app:public:__functions", type: "group-functions", label: "Functions", children: [], isExpanded: false };
+ const tables: TreeNode = { ...identity, id: "conn:app:public:__tables", type: "group-tables", label: "Tables", children: [] };
+ const schema: TreeNode = { ...identity, id: "conn:app:public", type: "schema", label: "public", children: [tables, procedures, functions], isExpanded: false };
+ const root: TreeNode = { connectionId: "conn", id: "conn", type: "connection", label: "Connection", children: [schema] };
+ const controller = new AbortController();
+ const search = vi.fn(async (node: TreeNode): Promise<TreeNode[]> => [{ ...identity, id: `${node.id}:late_routine`, type: node === procedures ? "procedure" : "function", label: "late_routine" }]);
+ const options = { root, connectionId: "conn", databaseScope: null as ReadonlySet<string> | null, signal: controller.signal, isSimpleParent: (_node: TreeNode) => false, discover: vi.fn(async (node: TreeNode) => node.children ?? []), search, onError: vi.fn() };
+ return { root, schema, procedures, functions, tables, controller, options };
+}
+
+test("searches never-expanded procedure/function groups, not indexed table/view groups", async () => {
+ const { root, procedures, functions, tables, options } = fixture();
+ const before = structuredClone(root);
+ const results = await collectSidebarRemoteSearchResults(options);
+ expect(options.search.mock.calls.map(([node]) => node)).toEqual([procedures, functions]);
+ expect(options.search).not.toHaveBeenCalledWith(tables);
+ const projected = mergeSidebarRemoteSearchResults([root], results);
+ expect(projected[0].children?.[0].children?.[1].children?.[0].label).toBe("late_routine");
+ expect(projected[0].children?.[0].children?.[2].children?.[0].type).toBe("function");
+ expect(root).toEqual(before);
+});
+
+test.each(remoteObjectGroupTypes)("searches an unexpanded %s group through the stable remote projection", async (type) => {
+ const identity = { connectionId: "conn", database: "app", schema: "public" };
+ const group: TreeNode = { ...identity, id: `conn:app:public:${type}`, type, label: type, children: [], isExpanded: false };
+ const root: TreeNode = { connectionId: "conn", id: "conn", type: "connection", label: "Connection", children: [group] };
+ const search = vi.fn(async (): Promise<TreeNode[]> => [{ ...identity, id: `${group.id}:match`, type: "procedure", label: "match" }]);
+ const results = await collectSidebarRemoteSearchResults({
+ root,
+ connectionId: "conn",
+ databaseScope: null,
+ signal: new AbortController().signal,
+ isSimpleParent: () => false,
+ discover: vi.fn(async (node: TreeNode) => node.children ?? []),
+ search,
+ onError: vi.fn(),
+ });
+ expect(search).toHaveBeenCalledExactlyOnceWith(group, expect.any(Function));
+ expect(results).toHaveLength(1);
+});
+
+test.each(indexedObjectGroupTypes)("leaves %s on the local-index path", async (type) => {
+ const group: TreeNode = { connectionId: "conn", database: "app", schema: "public", id: `conn:app:public:${type}`, type, label: type, children: [], isExpanded: false };
+ const search = vi.fn(async (): Promise<TreeNode[]> => []);
+ const results = await collectSidebarRemoteSearchResults({
+ root: { connectionId: "conn", id: "conn", type: "connection", label: "Connection", children: [group] },
+ connectionId: "conn",
+ databaseScope: null,
+ signal: new AbortController().signal,
+ isSimpleParent: () => false,
+ discover: vi.fn(async (node: TreeNode) => node.children ?? []),
+ search,
+ onError: vi.fn(),
+ });
+ expect(search).not.toHaveBeenCalled();
+ expect(results).toEqual([]);
+});
+
+test("discovers missing groups rather than searching only the first loaded page", async () => {
+ const { schema, procedures, functions, options } = fixture();
+ schema.children = [];
+ options.discover.mockImplementation(async (node) => (node === schema ? [procedures, functions] : (node.children ?? [])));
+ expect(await collectSidebarRemoteSearchResults(options)).toHaveLength(2);
+});
+
+test("stops dispatching further scopes and ignores responses after cancellation", async () => {
+ const { controller, options } = fixture();
+ options.search.mockImplementation(async () => {
+ controller.abort();
+ return [];
+ });
+ expect(await collectSidebarRemoteSearchResults(options)).toEqual([]);
+ expect(options.search).toHaveBeenCalledTimes(1);
+});
+
+test("keeps successful results when another object group fails", async () => {
+ const { functions, options } = fixture();
+ options.search.mockRejectedValueOnce(new Error("denied"));
+ const results = await collectSidebarRemoteSearchResults(options);
+ expect(results.map((result) => result.parentNodeId)).toEqual([functions.id]);
+ expect(options.onError).toHaveBeenCalledOnce();
+});
+
+test("honors opened-database scope and connection identity", async () => {
+ const { root, schema, options } = fixture();
+ root.children = [{ id: "conn:app", type: "database", label: "app", database: "app", connectionId: "conn", children: [schema] }];
+ options.databaseScope = new Set(["conn:other"]);
+ await collectSidebarRemoteSearchResults(options);
+ expect(options.search).not.toHaveBeenCalled();
+ options.databaseScope = null;
+ schema.connectionId = "different";
+ await collectSidebarRemoteSearchResults(options);
+ expect(options.search).not.toHaveBeenCalled();
+});
+
+test("searches a simple-display parent without expanding its table list", async () => {
+ const { schema, options } = fixture();
+ options.isSimpleParent = (node: TreeNode) => node === schema;
+ const results = await collectSidebarRemoteSearchResults(options);
+ expect(results.map((result) => result.parentNodeId)).toEqual([schema.id]);
+ expect(options.discover).not.toHaveBeenCalledWith(schema);
+});
+
+test("merges into the correct catalog and preserves live child state", async () => {
+ const { root, schema, procedures, options } = fixture();
+ const live: TreeNode = { id: `${procedures.id}:late_routine`, type: "procedure", label: "late_routine", isExpanded: true };
+ procedures.children = [live];
+ root.children?.push({ ...schema, catalog: "other", children: [{ ...procedures, catalog: "other", children: [] }] });
+ const projected = mergeSidebarRemoteSearchResults([root], await collectSidebarRemoteSearchResults({ ...options, root: { ...root, children: [schema] } }));
+ expect(projected[0].children?.[0].children?.[1].children).toEqual([live]);
+ expect(projected[0].children?.[1].children?.[0].children).toEqual([]);
+});