perf(sidebar): reduce saved SQL tree and export metadata work

This commit is contained in:
t8y2 committed 2026-09-08 18:47:13 +08:00
1 parent cd6ee718b6
commit 755f7b31bd
4 files changed
+47 -13

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { buildDatabaseSavedSqlRootNode, decorateDatabaseSavedSqlTreeNodes, indexSavedSqlFilesByDatabase, savedSqlFilesForDatabase, stripDatabaseSavedSqlTreeNodes } from "@/lib/savedSql/savedSqlDatabaseTree";
import { buildDatabaseSavedSqlRootNode, decorateDatabaseSavedSqlTreeNodes, indexSavedSqlFilesByDatabase, savedSqlFilesForDatabase, stripDatabaseSavedSqlTreeNodes, withDatabaseSavedSqlRoot } from "@/lib/savedSql/savedSqlDatabaseTree";
import type { SavedSqlFile, TreeNode } from "@/types/database";
function file(input: Partial<SavedSqlFile> & Pick<SavedSqlFile, "id" | "name" | "connectionId" | "database">): SavedSqlFile {
@@ -87,6 +87,16 @@ describe("database saved SQL tree", () => {
expect(decorated[0].children?.at(-1)).toMatchObject({ isExpanded: false, children: [{ savedSqlId: "orders-2" }, { savedSqlId: "orders-10" }] });
});
it("reuses unchanged database children when the saved SQL index changes elsewhere", () => {
const existingChildren = [{ id: "conn-1:app:tables", label: "tree.tables", type: "group-tables" as const }];
const existing = { ...database, children: [...existingChildren, buildDatabaseSavedSqlRootNode(database, files)!] };
const next = withDatabaseSavedSqlRoot({ ...database, children: existing.children }, existing.children, indexSavedSqlFilesByDatabase([...files, file({ id: "analytics-2", name: "analytics-2.sql", connectionId: "conn-1", database: "analytics" })]));
expect(next).toEqual(existing.children);
expect(next[0]).toBe(existingChildren[0]);
expect(next[1]).toBe(existing.children[1]);
});
it("removes runtime saved SQL nodes before metadata is cached", () => {
const decorated = decorateDatabaseSavedSqlTreeNodes([database], files);
expect(stripDatabaseSavedSqlTreeNodes(decorated)).toEqual([{ ...database, children: [] }]);
@@ -42,8 +42,8 @@ export function indexSavedSqlFilesByDatabase(files: readonly SavedSqlFile[]): Sa
return index;
}
export function savedSqlFilesForDatabase(source: SavedSqlDatabaseSource, scope: SavedSqlDatabaseScope): SavedSqlFile[] {
if (!Array.isArray(source)) return [...((source as SavedSqlDatabaseIndex).get(savedSqlDatabaseScopeKey(scope)) ?? [])];
export function savedSqlFilesForDatabase(source: SavedSqlDatabaseSource, scope: SavedSqlDatabaseScope): readonly SavedSqlFile[] {
if (!Array.isArray(source)) return (source as SavedSqlDatabaseIndex).get(savedSqlDatabaseScopeKey(scope)) ?? [];
return source.filter((file) => savedSqlDatabaseScopeKey(file) === savedSqlDatabaseScopeKey(scope)).sort(compareSavedSqlFiles);
}
@@ -84,8 +84,27 @@ export function buildDatabaseSavedSqlRootNode(databaseNode: Pick<TreeNode, "id"
export function withDatabaseSavedSqlRoot(databaseNode: Pick<TreeNode, "id" | "connectionId" | "catalog" | "database" | "children">, children: readonly TreeNode[], source: SavedSqlDatabaseSource): TreeNode[] {
const existingRoot = databaseNode.children?.find((child) => child.type === "saved-sql-root");
const root = buildDatabaseSavedSqlRootNode(databaseNode, source, existingRoot);
const existingMetadataChildren = databaseNode.children?.filter((child) => child.type !== "saved-sql-root") ?? [];
const metadataChildren = children.filter((child) => child.type !== "saved-sql-root");
const files =
databaseNode.connectionId && databaseNode.database !== undefined
? savedSqlFilesForDatabase(source, {
connectionId: databaseNode.connectionId,
catalog: databaseNode.catalog,
database: databaseNode.database,
})
: [];
const sameMetadata = metadataChildren.length === existingMetadataChildren.length && metadataChildren.every((node, index) => node === existingMetadataChildren[index]);
const sameFiles =
!!existingRoot &&
existingRoot.children?.length === files.length &&
files.every((file, index) => {
const node = existingRoot.children?.[index];
return node?.type === "saved-sql-file" && node.savedSqlId === file.id && node.label === file.name && node.connectionId === file.connectionId && node.catalog === file.catalog && node.database === file.database && node.schema === file.schema;
});
if (existingRoot && sameMetadata && sameFiles) return databaseNode.children ? [...databaseNode.children] : metadataChildren;
const root = buildDatabaseSavedSqlRootNode(databaseNode, source, existingRoot);
return root ? [...metadataChildren, root] : metadataChildren;
}
+1 -1
View File
@@ -1813,7 +1813,7 @@ export const useConnectionStore = defineStore("connection", () => {
savedSqlFilesByDatabase = indexSavedSqlFilesByDatabase(savedSqlStore.allFiles);
refreshDatabaseSavedSqlTrees();
},
{ flush: "sync" },
{ flush: "post" },
);
function schemaCacheKey(...parts: string[]): string {