feat(sidebar): auto-load all tables per connection when expanded

Closes #9834
This commit is contained in:
t8y2 committed 2026-09-27 09:38:55 +00:00
1 parent 725b1ee252
commit 5ba0d435f0
44 files changed
+332 -20

No files matched your search

@@ -1,6 +1,7 @@
import { createPinia, setActivePinia } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { canTreeNodeShowExpander } from "@/lib/sidebar/sidebarTreeItemLayout";
import { filterLocallySearchedTables } from "@/lib/sidebar/sidebarSearchTree";
import type { ConnectionConfig, ObjectInfo, TableInfo, TreeNode } from "@/types/database";
function installLocalStorage() {
@@ -3170,6 +3171,129 @@ describe("connectionStore metadata loading", () => {
expect(tablesGroup.children?.some((child) => child.label?.startsWith("fresh_"))).toBe(true);
});
it("automatically drains only opted-in table groups", async () => {
const rows = Array.from({ length: 5 }, (_, index) => ({
name: `object_${index + 1}`,
table_type: "TABLE" as const,
comment: null,
}));
const listTables = vi.fn((_connectionId: string, _database: string, _schema: string, searchFilter?: string, limit?: number, offset?: number, objectTypes?: string[]) => {
const tableType = objectTypes?.includes("VIEW") ? "VIEW" : "TABLE";
const matches = searchFilter ? rows.filter((row) => row.name.includes(searchFilter)) : rows;
return Promise.resolve(matches.slice(offset ?? 0, (offset ?? 0) + (limit ?? matches.length)).map((row) => ({ ...row, table_type: tableType })));
});
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
listTables,
loadSchemaCache: vi.fn().mockResolvedValue(null),
saveSchemaCache: vi.fn().mockResolvedValue(undefined),
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
}));
const { useConnectionStore } = await import("@/stores/connectionStore");
const { useSettingsStore } = await import("@/stores/settingsStore");
const store = useConnectionStore();
const settingsStore = useSettingsStore();
settingsStore.editorSettings.sidebarObjectDisplay = "grouped";
settingsStore.desktopSettings.sidebar_table_page_size = 2;
const connection = { ...mysqlConnection(), sidebar_auto_load_all_tables: true };
const tablesGroup: TreeNode = {
id: `${connection.id}:app:__tables`,
label: "tree.tables",
type: "group-tables",
connectionId: connection.id,
database: "app",
isExpanded: false,
children: [],
};
const viewsGroup: TreeNode = {
...tablesGroup,
id: `${connection.id}:app:__views`,
label: "tree.views",
type: "group-views",
};
store.connections = [connection];
store.connectedIds.add(connection.id);
store.treeNodes = [{ id: connection.id, label: connection.name, type: "connection", connectionId: connection.id, children: [tablesGroup, viewsGroup] }];
await store.loadObjectGroupChildren(tablesGroup);
expect(listTables.mock.calls.map((call) => call[5])).toEqual([0, 2, 4]);
expect(tablesGroup.children?.map((child) => child.label)).toEqual(rows.map((row) => row.name));
expect(tablesGroup.children?.some((child) => child.type === "load-more")).toBe(false);
const filteredTree = filterLocallySearchedTables(store.treeNodes, {
enabled: true,
queries: { [tablesGroup.id]: "object_5" },
indexedResults: {},
});
expect(filteredTree[0].children?.[0]?.children?.map((child) => child.label)).toEqual(["object_5"]);
await store.loadObjectGroupChildren(viewsGroup);
expect(listTables).toHaveBeenCalledTimes(4);
expect(viewsGroup.children?.map((child) => child.label)).toEqual(["object_1", "object_2", "tree.loadMore"]);
const searchedTablesGroup: TreeNode = { ...tablesGroup, id: `${connection.id}:search:__tables`, database: "search", children: [] };
store.treeNodes[0].children!.push(searchedTablesGroup);
store.sidebarSearchQuery = "object_5";
await store.loadObjectGroupChildren(searchedTablesGroup, { force: true });
expect(listTables).toHaveBeenCalledTimes(5);
expect(listTables.mock.calls.at(-1)?.slice(3, 6)).toEqual(["object_5", SIDEBAR_SEARCH_RESULT_BUDGET, undefined]);
expect(searchedTablesGroup.children?.map((child) => child.label)).toEqual(["object_5"]);
});
it("keeps legacy connections on the first table page", async () => {
const rows = Array.from({ length: 3 }, (_, index) => ({
name: `table_${index + 1}`,
table_type: "TABLE" as const,
comment: null,
}));
const listTables = vi.fn((_connectionId: string, _database: string, _schema: string, _searchFilter?: string, limit?: number, offset?: number) => Promise.resolve(rows.slice(offset ?? 0, (offset ?? 0) + (limit ?? rows.length))));
vi.doMock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
vi.doMock("@/lib/backend/api", () => ({
checkConnectionHealth: vi.fn().mockResolvedValue(undefined),
deleteSchemaCachePrefix: vi.fn().mockResolvedValue(undefined),
listTables,
loadSchemaCache: vi.fn().mockResolvedValue(null),
saveSchemaCache: vi.fn().mockResolvedValue(undefined),
saveConnections: vi.fn().mockResolvedValue(undefined),
saveSidebarLayout: vi.fn().mockResolvedValue(undefined),
}));
const { useConnectionStore } = await import("@/stores/connectionStore");
const { useSettingsStore } = await import("@/stores/settingsStore");
const store = useConnectionStore();
const settingsStore = useSettingsStore();
settingsStore.editorSettings.sidebarObjectDisplay = "grouped";
settingsStore.desktopSettings.sidebar_table_page_size = 2;
const connection = mysqlConnection();
const tablesGroup: TreeNode = {
id: `${connection.id}:app:__tables`,
label: "tree.tables",
type: "group-tables",
connectionId: connection.id,
database: "app",
isExpanded: false,
children: [],
};
store.connections = [connection];
store.connectedIds.add(connection.id);
store.treeNodes = [{ id: connection.id, label: connection.name, type: "connection", connectionId: connection.id, children: [tablesGroup] }];
await store.loadObjectGroupChildren(tablesGroup);
expect(listTables).toHaveBeenCalledTimes(1);
expect(tablesGroup.children?.map((child) => child.label)).toEqual(["table_1", "table_2", "tree.loadMore"]);
});
it("restores and drains unfiltered pages when expand-all races a search clear reload", async () => {
const tables = Array.from({ length: 5 }, (_, index) => ({
name: `t_${String(index + 1).padStart(4, "0")}`,
+25 -6
View File
@@ -1721,6 +1721,7 @@ export const useConnectionStore = defineStore("connection", () => {
docs_notes_path: config.docs_notes_path?.trim() ? config.docs_notes_path.trim() : undefined,
transport_layers: Array.isArray(config.transport_layers) ? config.transport_layers : [],
show_system_schemas: config.show_system_schemas === true,
sidebar_auto_load_all_tables: config.sidebar_auto_load_all_tables === true,
connect_timeout_secs: connectTimeoutInherit ? settingsStore.editorSettings.globalConnectTimeoutSecs : config.connect_timeout_secs || 10,
connect_timeout_inherit: connectTimeoutInherit,
query_timeout_secs: queryTimeoutInherit ? settingsStore.editorSettings.globalQueryTimeoutSecs : (config.query_timeout_secs ?? DEFAULT_QUERY_TIMEOUT_SECS),
@@ -6471,9 +6472,9 @@ export const useConnectionStore = defineStore("connection", () => {
);
}
async function loadObjectGroupChildren(node: TreeNode, options?: LoadTreeOptions): Promise<void> {
async function loadObjectGroupFirstPage(node: TreeNode, options?: LoadTreeOptions): Promise<void> {
if (options?.sidebarSearch) {
return withSidebarSearchLoad(node.connectionId, () => loadObjectGroupChildren(node, { ...options, sidebarSearch: false }));
return withSidebarSearchLoad(node.connectionId, () => loadObjectGroupFirstPage(node, { ...options, sidebarSearch: false }));
}
// Queued search/refresh tasks can outlive disconnect, which removes their nodes.
if (!treeNodeInSidebarTree(node)) return;
@@ -6501,7 +6502,7 @@ export const useConnectionStore = defineStore("connection", () => {
if (!options?.force && !searchFilter && !options?.sidebarTableSearchParentId && !tableNameFilterForScope) {
if (await hydrateTreeNodeFromCache(node, objectGroupCacheKey(node))) {
forgetFilteredObjectGroupChildren(node.id);
void loadObjectGroupChildren(node, { ...options, force: true }).catch(() => undefined);
void loadObjectGroupFirstPage(node, { ...options, force: true }).catch(() => undefined);
return;
}
}
@@ -6624,6 +6625,23 @@ export const useConnectionStore = defineStore("connection", () => {
);
}
function shouldAutoLoadAllTableGroupChildren(node: TreeNode, options?: LoadTreeOptions): boolean {
if (node.type !== "group-tables" || !node.connectionId) return false;
if (getConfig(node.connectionId)?.sidebar_auto_load_all_tables !== true) return false;
// Search projections have their own bounded/paged lifecycle. Automatically
// draining those pages would turn a filter operation into an unbounded load
// and could replace the unfiltered snapshot that clearing search restores.
return !options?.sidebarSearch && !options?.sidebarTableSearchParentId && !activeTreeLoadSearchFilter(options);
}
async function loadObjectGroupChildren(node: TreeNode, options?: LoadTreeOptions): Promise<void> {
if (shouldAutoLoadAllTableGroupChildren(node, options)) {
await loadAllObjectGroupChildren(node, options);
return;
}
await loadObjectGroupFirstPage(node, options);
}
async function loadCustomTypeChildren(node: TreeNode, options?: LoadTreeOptions) {
if (node.type !== "type" || !node.connectionId || !hasTreeNodeDatabaseContext(node)) return;
let load = beginTreeNodeLoad(node);
@@ -6971,14 +6989,15 @@ export const useConnectionStore = defineStore("connection", () => {
);
}
async function loadAllObjectGroupChildren(parent: TreeNode) {
async function loadAllObjectGroupChildren(parent: TreeNode, initialLoadOptions?: LoadTreeOptions) {
if (!parent.connectionId || !hasTreeNodeDatabaseContext(parent)) return;
if (!objectTypesForGroupNode(parent.type)) return;
const liveParent = treeNodeInSidebarTree(parent);
if (!liveParent) return;
if (!isTreeNodeChildrenLoaded(liveParent.id) || filteredObjectGroupChildrenIds.has(liveParent.id)) {
if (initialLoadOptions?.force || !isTreeNodeChildrenLoaded(liveParent.id) || filteredObjectGroupChildrenIds.has(liveParent.id)) {
let isPreparedPageCurrent: (() => boolean) | undefined;
await loadObjectGroupChildren(liveParent, {
await loadObjectGroupFirstPage(liveParent, {
...initialLoadOptions,
force: true,
searchFilter: "",
allowGlobalSearchMismatch: true,