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(structure): prioritize exact and prefix matches in option search

lxk955e32ff2ed5a1c
diff --git a/apps/desktop/src/components/ui/searchable-select/__tests__/SearchableSelect.trigger.spec.ts b/apps/desktop/src/components/ui/searchable-select/__tests__/SearchableSelect.trigger.spec.ts
index 2ad7268c76..2c1b8583f6 100644
--- a/apps/desktop/src/components/ui/searchable-select/__tests__/SearchableSelect.trigger.spec.ts
+++ b/apps/desktop/src/components/ui/searchable-select/__tests__/SearchableSelect.trigger.spec.ts
@@ -284,4 +284,28 @@ describe("SearchableSelect trigger", () => {
expect(emitted.modelValue).toEqual(["int"]);
expect(document.activeElement).toBe(nextInput);
});
+
+ it("prioritizes exact match over preceding substring matches when typing in search input", async () => {
+ const { root, emitted } = mountSelect({
+ options: ["tinyint", "smallint", "int", "bigint"],
+ modelValue: "",
+ });
+
+ const trigger = root.querySelector("button")!;
+ trigger.dispatchEvent(new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true, cancelable: true }));
+ await nextTick();
+
+ const input = document.body.querySelector<HTMLInputElement>("input")!;
+ expect(input).not.toBeNull();
+ input.value = "int";
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ await nextTick();
+
+ const tabEvent = new KeyboardEvent("keydown", { key: "Tab", bubbles: true, cancelable: true });
+ input.dispatchEvent(tabEvent);
+ await nextTick();
+
+ expect(tabEvent.defaultPrevented).toBe(true);
+ expect(emitted.modelValue).toEqual(["int"]);
+ });
});
diff --git a/apps/desktop/src/lib/database/__tests__/databaseOptionSearch.spec.ts b/apps/desktop/src/lib/database/__tests__/databaseOptionSearch.spec.ts
new file mode 100644
index 0000000000..d5869d3606
--- /dev/null
+++ b/apps/desktop/src/lib/database/__tests__/databaseOptionSearch.spec.ts
@@ -0,0 +1,71 @@
+import { describe, expect, it } from "vitest";
+import { filterDatabaseOptions } from "../databaseOptionSearch";
+
+describe("filterDatabaseOptions", () => {
+ it("returns original options when query is empty or whitespace", () => {
+ const options = ["tinyint", "int", "bigint"];
+ expect(filterDatabaseOptions(options, "")).toEqual(options);
+ expect(filterDatabaseOptions(options, " ")).toEqual(options);
+ });
+
+ it("prioritizes exact matches ahead of prefix and substring matches", () => {
+ const options = ["tinyint", "tinyint unsigned", "smallint", "smallint unsigned", "mediumint", "mediumint unsigned", "int", "int unsigned", "integer", "integer unsigned", "bigint", "bigint unsigned", "point"];
+
+ const result = filterDatabaseOptions(options, "int");
+
+ // Exact match "int" must be first
+ expect(result[0]).toBe("int");
+ // Prefix matches come next, preserving relative order
+ expect(result.slice(1, 4)).toEqual(["int unsigned", "integer", "integer unsigned"]);
+ // Substring matches come last, preserving relative order
+ expect(result.slice(4)).toEqual(["tinyint", "tinyint unsigned", "smallint", "smallint unsigned", "mediumint", "mediumint unsigned", "bigint", "bigint unsigned", "point"]);
+ });
+
+ it("handles case-insensitivity in exact matches", () => {
+ const options = ["TINYINT", "INT", "BIGINT"];
+ const result = filterDatabaseOptions(options, "int");
+ expect(result[0]).toBe("INT");
+ });
+
+ it("prioritizes prefix matches ahead of substring matches", () => {
+ const options = ["smallserial", "serial", "bigserial"];
+ const result = filterDatabaseOptions(options, "ser");
+ expect(result).toEqual(["serial", "smallserial", "bigserial"]);
+ });
+
+ it("prioritizes word-boundary matches ahead of substring matches", () => {
+ const options = ["nonunsigned_value", "int unsigned", "sununsigned"];
+ const result = filterDatabaseOptions(options, "unsigned");
+ expect(result[0]).toBe("int unsigned");
+ expect(result).toEqual(["int unsigned", "nonunsigned_value", "sununsigned"]);
+ });
+
+ it("preserves relative order for items within the same match tier", () => {
+ const options = ["tinytext", "text", "mediumtext", "longtext"];
+ const result = filterDatabaseOptions(options, "text");
+ expect(result[0]).toBe("text");
+ expect(result.slice(1)).toEqual(["tinytext", "mediumtext", "longtext"]);
+ });
+
+ it("supports custom displayName mappings", () => {
+ const options = ["col_a", "col_b", "col_c"];
+ const displayMap: Record<string, string> = {
+ col_a: "Identifier",
+ col_b: "Integer Count",
+ col_c: "Int",
+ };
+
+ const result = filterDatabaseOptions(options, "int", (opt) => displayMap[opt] ?? opt);
+ // col_c has label "Int", exact match
+ expect(result[0]).toBe("col_c");
+ // col_b has label "Integer Count", word-boundary prefix match
+ expect(result[1]).toBe("col_b");
+ });
+
+ it("handles special characters without regex errors", () => {
+ const options = ["int[]", "integer[]", "point", "geometry(point, 4326)"];
+ expect(filterDatabaseOptions(options, "int[]")).toEqual(["int[]"]);
+ expect(filterDatabaseOptions(options, "point")[0]).toBe("point");
+ expect(filterDatabaseOptions(options, "geometry(")[0]).toBe("geometry(point, 4326)");
+ });
+});
diff --git a/apps/desktop/src/lib/database/databaseOptionSearch.ts b/apps/desktop/src/lib/database/databaseOptionSearch.ts
index 93ac176e77..18d8a4560d 100644
--- a/apps/desktop/src/lib/database/databaseOptionSearch.ts
+++ b/apps/desktop/src/lib/database/databaseOptionSearch.ts
@@ -2,9 +2,44 @@ export function filterDatabaseOptions(options: string[], query: string, displayN
const normalizedQuery = query.trim().toLowerCase();
if (!normalizedQuery) return options;
- return options.filter((option) => {
+ interface RankedOption {
+ option: string;
+ tier: number;
+ index: number;
+ }
+
+ const isWordBoundaryMatch = (text: string): boolean => {
+ const words = text.split(/[\s_\-(),/]+/);
+ return words.some((word) => word.startsWith(normalizedQuery));
+ };
+
+ const matches: RankedOption[] = [];
+
+ for (let index = 0; index < options.length; index++) {
+ const option = options[index];
const raw = option.toLowerCase();
const label = displayName(option).toLowerCase();
- return raw.includes(normalizedQuery) || label.includes(normalizedQuery);
+
+ let tier = -1;
+ if (raw === normalizedQuery || label === normalizedQuery) {
+ tier = 0;
+ } else if (raw.startsWith(normalizedQuery) || label.startsWith(normalizedQuery)) {
+ tier = 1;
+ } else if (isWordBoundaryMatch(raw) || isWordBoundaryMatch(label)) {
+ tier = 2;
+ } else if (raw.includes(normalizedQuery) || label.includes(normalizedQuery)) {
+ tier = 3;
+ }
+
+ if (tier >= 0) {
+ matches.push({ option, tier, index });
+ }
+ }
+
+ matches.sort((a, b) => {
+ if (a.tier !== b.tier) return a.tier - b.tier;
+ return a.index - b.index;
});
+
+ return matches.map((m) => m.option);
}