feat: add Ctrl+P quick-open for database objects

This commit is contained in:
Wakanlolz authored and GitHub committed 2026-06-20 00:13:09 +08:00
1 parent 88a4f89584
commit ebde8b4fa1
13 files changed
+1314 -1

No files matched your search

@@ -0,0 +1,485 @@
import { describe, expect, it, beforeEach, vi } from "vitest";
import { useQuickOpen, type QuickOpenItem } from "@/composables/useQuickOpen";
import { useConnectionStore } from "@/stores/connectionStore";
vi.mock("@/stores/connectionStore", () => ({
useConnectionStore: vi.fn(),
}));
describe("useQuickOpen", () => {
describe("fuzzyMatch function", () => {
it("should return exact substring match with score 1", () => {
// Mock store with test data
const mockStore = {
connections: [{ id: "conn1", name: "MyConnection", type: "mssql" }],
treeNodes: [
{
connectionId: "conn1",
type: "database",
database: "MyDatabase",
label: "MyDatabase",
},
],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("MyDatabase");
// After search, we should find the exact match
expect(filteredItems.value.length).toBeGreaterThan(0);
const result = filteredItems.value.find((item) => item.label === "MyDatabase");
expect(result).toBeDefined();
if (result) {
expect(result.matchScore).toBe(1); // Exact substring match score
}
});
it("should handle empty query by returning all items", () => {
const mockStore = {
connections: [
{ id: "conn1", name: "Connection1", type: "mssql" },
{ id: "conn2", name: "Connection2", type: "postgres" },
],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("");
// Empty query should return all items
expect(filteredItems.value.length).toBe(2);
filteredItems.value.forEach((item) => {
expect(item.matchScore).toBe(Infinity);
});
});
it("should perform fuzzy matching for non-consecutive characters", () => {
const mockStore = {
connections: [{ id: "conn1", name: "MyConnection", type: "mssql" }],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("MyCo");
// Fuzzy match should find "MyConnection"
const result = filteredItems.value.find((item) => item.label === "MyConnection");
expect(result).toBeDefined();
});
it("should return null for non-matching query", () => {
const mockStore = {
connections: [{ id: "conn1", name: "MyConnection", type: "mssql" }],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("XYZ");
// No match should return empty results
expect(filteredItems.value.length).toBe(0);
});
it("should score consecutive characters higher than non-consecutive", () => {
const mockStore = {
connections: [
{ id: "conn1", name: "user_login_table", type: "mssql" },
{ id: "conn2", name: "user_data_login", type: "mssql" },
],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("login");
// "user_login_table" has consecutive "login" match (better score)
// "user_data_login" has consecutive "login" match too
expect(filteredItems.value.length).toBe(2);
// Both should have score 1.0 (consecutive match: login appears consecutively)
});
});
describe("filtering and searching", () => {
it("should filter items based on search query", () => {
const mockStore = {
connections: [
{ id: "conn1", name: "ProdDB", type: "mssql" },
{ id: "conn2", name: "DevDB", type: "mssql" },
],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("Prod");
expect(filteredItems.value.length).toBe(1);
expect(filteredItems.value[0].label).toBe("ProdDB");
});
it("should be case-insensitive", () => {
const mockStore = {
connections: [{ id: "conn1", name: "MyConnection", type: "mssql" }],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("myconnection");
expect(filteredItems.value.length).toBe(1);
expect(filteredItems.value[0].label).toBe("MyConnection");
});
it("should search across connection name and database name", () => {
const mockStore = {
connections: [{ id: "conn1", name: "ProdConnection", type: "mssql" }],
treeNodes: [
{
connectionId: "conn1",
type: "database",
database: "UserDB",
label: "UserDB",
},
],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
// Search by connection name
setQuery("Prod");
expect(filteredItems.value.length).toBeGreaterThan(0);
});
it("should sort by match score (lower scores first)", () => {
const mockStore = {
connections: [
{ id: "conn1", name: "Database", type: "mssql" },
{ id: "conn2", name: "MyDB", type: "mssql" },
],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("db");
expect(filteredItems.value.length).toBe(2);
// First result should have better (lower) score
expect(filteredItems.value[0].matchScore).toBeLessThanOrEqual(filteredItems.value[1].matchScore);
});
it("should sort by type for equal match scores", () => {
const mockStore = {
connections: [{ id: "conn1", name: "test", type: "mssql" }],
treeNodes: [
{
connectionId: "conn1",
type: "database",
database: "test_db",
label: "test_db",
},
],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("test");
// Connections should come before databases for the same query
if (filteredItems.value.length >= 2) {
const connectionItem = filteredItems.value.find((item) => item.type === "connection");
const databaseItem = filteredItems.value.find((item) => item.type === "database");
if (connectionItem && databaseItem) {
expect(filteredItems.value.indexOf(connectionItem)).toBeLessThan(filteredItems.value.indexOf(databaseItem));
}
}
});
});
describe("item selection navigation", () => {
it("should initialize with selectedIndex at 0", () => {
const mockStore = {
connections: [],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { selectedIndex } = useQuickOpen();
expect(selectedIndex.value).toBe(0);
});
it("should select next item", () => {
const mockStore = {
connections: [
{ id: "conn1", name: "Conn1", type: "mssql" },
{ id: "conn2", name: "Conn2", type: "mssql" },
],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { selectNext, selectedIndex, setQuery } = useQuickOpen();
setQuery("");
expect(selectedIndex.value).toBe(0);
selectNext();
expect(selectedIndex.value).toBe(1);
});
it("should not exceed max index when selecting next", () => {
const mockStore = {
connections: [{ id: "conn1", name: "Conn1", type: "mssql" }],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { selectNext, selectedIndex, setQuery } = useQuickOpen();
setQuery("");
selectNext();
selectNext(); // Attempt to go beyond max
expect(selectedIndex.value).toBe(0); // Should stay at 0 (only 1 item)
});
it("should select previous item", () => {
const mockStore = {
connections: [
{ id: "conn1", name: "Conn1", type: "mssql" },
{ id: "conn2", name: "Conn2", type: "mssql" },
],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { selectNext, selectPrevious, selectedIndex, setQuery } = useQuickOpen();
setQuery("");
selectNext();
expect(selectedIndex.value).toBe(1);
selectPrevious();
expect(selectedIndex.value).toBe(0);
});
it("should not go below 0 when selecting previous", () => {
const mockStore = {
connections: [],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { selectPrevious, selectedIndex } = useQuickOpen();
// Verify initial state
expect(selectedIndex.value).toBe(0);
selectPrevious();
expect(selectedIndex.value).toBe(0);
});
it("should return correct selectedItem", () => {
const mockStore = {
connections: [
{ id: "conn1", name: "Conn1", type: "mssql" },
{ id: "conn2", name: "Conn2", type: "mssql" },
],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { selectNext, selectedItem, setQuery } = useQuickOpen();
setQuery("");
expect(selectedItem.value?.label).toBe("Conn1");
selectNext();
expect(selectedItem.value?.label).toBe("Conn2");
});
it("should return null selectedItem when index is out of bounds", () => {
const mockStore = {
connections: [{ id: "conn1", name: "Conn1", type: "mssql" }],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { selectedItem, selectedIndex } = useQuickOpen();
// Manually set invalid index
selectedIndex.value = 999;
expect(selectedItem.value).toBeNull();
});
});
describe("reset and query setting", () => {
it("should reset selection to 0 when setQuery is called", () => {
const mockStore = {
connections: [
{ id: "conn1", name: "Conn1", type: "mssql" },
{ id: "conn2", name: "Conn2", type: "mssql" },
],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { selectNext, setQuery, selectedIndex } = useQuickOpen();
selectNext();
expect(selectedIndex.value).toBe(1);
setQuery("test");
expect(selectedIndex.value).toBe(0);
});
it("should update searchQuery when setQuery is called", () => {
const mockStore = {
connections: [],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { searchQuery, setQuery } = useQuickOpen();
setQuery("NewQuery");
expect(searchQuery.value).toBe("NewQuery");
});
it("should resetSelection to 0", () => {
const mockStore = {
connections: [
{ id: "conn1", name: "Conn1", type: "mssql" },
{ id: "conn2", name: "Conn2", type: "mssql" },
],
treeNodes: [],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { resetSelection, selectNext, selectedIndex, setQuery } = useQuickOpen();
setQuery("");
selectNext();
expect(selectedIndex.value).toBe(1);
resetSelection();
expect(selectedIndex.value).toBe(0);
});
});
describe("allItems with different database object types", () => {
it("should include tables from tree nodes", () => {
const mockStore = {
connections: [{ id: "conn1", name: "MyConn", type: "mssql" }],
treeNodes: [
{
connectionId: "conn1",
type: "table",
database: "MyDB",
schema: "dbo",
label: "Users",
},
],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("");
const tableItem = filteredItems.value.find((item) => item.type === "table");
expect(tableItem).toBeDefined();
expect(tableItem?.label).toBe("Users");
});
it("should include views from tree nodes", () => {
const mockStore = {
connections: [{ id: "conn1", name: "MyConn", type: "mssql" }],
treeNodes: [
{
connectionId: "conn1",
type: "view",
database: "MyDB",
schema: "dbo",
label: "UserView",
},
],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("");
const viewItem = filteredItems.value.find((item) => item.type === "view");
expect(viewItem).toBeDefined();
expect(viewItem?.label).toBe("UserView");
});
it("should include procedures from tree nodes", () => {
const mockStore = {
connections: [{ id: "conn1", name: "MyConn", type: "mssql" }],
treeNodes: [
{
connectionId: "conn1",
type: "procedure",
database: "MyDB",
schema: "dbo",
label: "GetUsers",
},
],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("");
const procItem = filteredItems.value.find((item) => item.type === "procedure");
expect(procItem).toBeDefined();
expect(procItem?.label).toBe("GetUsers");
});
it("should include functions from tree nodes", () => {
const mockStore = {
connections: [{ id: "conn1", name: "MyConn", type: "mssql" }],
treeNodes: [
{
connectionId: "conn1",
type: "function",
database: "MyDB",
schema: "dbo",
label: "ComputeAge",
},
],
};
vi.mocked(useConnectionStore).mockReturnValue(mockStore as any);
const { filteredItems, setQuery } = useQuickOpen();
setQuery("");
const funcItem = filteredItems.value.find((item) => item.type === "function");
expect(funcItem).toBeDefined();
expect(funcItem?.label).toBe("ComputeAge");
});
});
});
@@ -0,0 +1,349 @@
import { computed, ref } from "vue";
import type { ConnectionConfig } from "@/types/database";
import { useConnectionStore } from "@/stores/connectionStore";
export interface QuickOpenItem {
id: string;
type: "connection" | "database" | "table" | "view" | "materialized_view" | "procedure" | "function" | "sequence" | "package" | "package-body";
label: string;
description?: string;
connectionId: string;
database?: string;
schema?: string;
objectName?: string; // For non-table objects (views, procedures, functions, sequences, packages)
tableName?: string; // Kept for backward compatibility
connectionName?: string;
searchText: string; // Lowercase text for searching
}
/**
* Fuzzy match function that checks if query matches text
* Returns the matched indices for highlighting
*/
function fuzzyMatch(query: string, text: string): { score: number; indices: number[] } | null {
const lowerQuery = query.toLowerCase();
const lowerText = text.toLowerCase();
if (!lowerQuery) return { score: Infinity, indices: [] };
if (lowerText.includes(lowerQuery)) {
// Exact substring match gets highest score
const startIdx = lowerText.indexOf(lowerQuery);
return {
score: 1,
indices: Array.from({ length: lowerQuery.length }, (_, i) => startIdx + i),
};
}
// Fuzzy match: find all characters in order
let queryIdx = 0;
const indices: number[] = [];
let score = 0;
let lastMatchIdx = -1;
for (let i = 0; i < lowerText.length && queryIdx < lowerQuery.length; i++) {
if (lowerText[i] === lowerQuery[queryIdx]) {
indices.push(i);
// Score based on proximity (consecutive chars score better)
score += lastMatchIdx === i - 1 ? 2 : 1;
lastMatchIdx = i;
queryIdx++;
}
}
if (queryIdx === lowerQuery.length) {
return { score: score / lowerQuery.length, indices };
}
return null;
}
interface MatchedItem extends QuickOpenItem {
matchScore: number;
matchIndices: number[];
}
export function useQuickOpen() {
const connectionStore = useConnectionStore();
const searchQuery = ref("");
const selectedIndex = ref(0);
const allItems = computed((): QuickOpenItem[] => {
const items: QuickOpenItem[] = [];
const connections = connectionStore.connections;
const treeNodes = connectionStore.treeNodes;
// Add connections
for (const conn of connections) {
items.push({
id: `conn-${conn.id}`,
type: "connection",
label: conn.name,
connectionId: conn.id,
connectionName: conn.name,
searchText: `${conn.name}`,
});
}
// Add databases and tables from tree nodes
// Filter tree nodes by connection
for (const conn of connections) {
const connectionTreeNodes = treeNodes.filter((node) => node.connectionId === conn.id);
if (connectionTreeNodes.length === 0) continue;
// Process tree nodes to extract databases and tables
processDatabaseTreeNodes(connectionTreeNodes, conn, items);
}
return items;
});
function processDatabaseTreeNodes(nodes: any[], conn: ConnectionConfig, items: QuickOpenItem[]): void {
for (const node of nodes) {
// Skip certain node types
if (node.type === "group" || node.type === "linked-server-root") {
if (node.children) {
processDatabaseTreeNodes(node.children, conn, items);
}
continue;
}
// Database nodes
if (node.type === "database" && node.database) {
items.push({
id: `db-${conn.id}-${node.database}`,
type: "database",
label: node.label || node.database,
description: conn.name,
connectionId: conn.id,
database: node.database,
connectionName: conn.name,
searchText: `${conn.name} ${node.database}`,
});
}
// Schema nodes - skip them but process their children
if (node.type === "schema" && node.children) {
processDatabaseTreeNodes(node.children, conn, items);
continue;
}
// Table nodes
if (node.type === "table" && node.database && node.label) {
items.push({
id: `table-${conn.id}-${node.database}-${node.schema || ""}-${node.label}`,
type: "table",
label: node.label,
description: `${conn.name} / ${node.database}${node.schema ? " / " + node.schema : ""}`,
connectionId: conn.id,
database: node.database,
schema: node.schema,
tableName: node.label,
connectionName: conn.name,
searchText: `${conn.name} ${node.database} ${node.schema || ""} ${node.label}`,
});
}
// View nodes
if (node.type === "view" && node.database && node.label) {
items.push({
id: `view-${conn.id}-${node.database}-${node.schema || ""}-${node.label}`,
type: "view",
label: node.label,
description: `${conn.name} / ${node.database}${node.schema ? " / " + node.schema : ""}`,
connectionId: conn.id,
database: node.database,
schema: node.schema,
objectName: node.label,
connectionName: conn.name,
searchText: `${conn.name} ${node.database} ${node.schema || ""} ${node.label}`,
});
}
// Materialized view nodes
if (node.type === "materialized_view" && node.database && node.label) {
items.push({
id: `mview-${conn.id}-${node.database}-${node.schema || ""}-${node.label}`,
type: "materialized_view",
label: node.label,
description: `${conn.name} / ${node.database}${node.schema ? " / " + node.schema : ""}`,
connectionId: conn.id,
database: node.database,
schema: node.schema,
objectName: node.label,
connectionName: conn.name,
searchText: `${conn.name} ${node.database} ${node.schema || ""} ${node.label}`,
});
}
// Procedure nodes
if (node.type === "procedure" && node.database && node.label) {
items.push({
id: `proc-${conn.id}-${node.database}-${node.schema || ""}-${node.label}`,
type: "procedure",
label: node.label,
description: `${conn.name} / ${node.database}${node.schema ? " / " + node.schema : ""}`,
connectionId: conn.id,
database: node.database,
schema: node.schema,
objectName: node.label,
connectionName: conn.name,
searchText: `${conn.name} ${node.database} ${node.schema || ""} ${node.label}`,
});
}
// Function nodes
if (node.type === "function" && node.database && node.label) {
items.push({
id: `func-${conn.id}-${node.database}-${node.schema || ""}-${node.label}`,
type: "function",
label: node.label,
description: `${conn.name} / ${node.database}${node.schema ? " / " + node.schema : ""}`,
connectionId: conn.id,
database: node.database,
schema: node.schema,
objectName: node.label,
connectionName: conn.name,
searchText: `${conn.name} ${node.database} ${node.schema || ""} ${node.label}`,
});
}
// Sequence nodes
if (node.type === "sequence" && node.database && node.label) {
items.push({
id: `seq-${conn.id}-${node.database}-${node.schema || ""}-${node.label}`,
type: "sequence",
label: node.label,
description: `${conn.name} / ${node.database}${node.schema ? " / " + node.schema : ""}`,
connectionId: conn.id,
database: node.database,
schema: node.schema,
objectName: node.label,
connectionName: conn.name,
searchText: `${conn.name} ${node.database} ${node.schema || ""} ${node.label}`,
});
}
// Package nodes
if (node.type === "package" && node.database && node.label) {
items.push({
id: `pkg-${conn.id}-${node.database}-${node.schema || ""}-${node.label}`,
type: "package",
label: node.label,
description: `${conn.name} / ${node.database}${node.schema ? " / " + node.schema : ""}`,
connectionId: conn.id,
database: node.database,
schema: node.schema,
objectName: node.label,
connectionName: conn.name,
searchText: `${conn.name} ${node.database} ${node.schema || ""} ${node.label}`,
});
}
// Package-body nodes
if (node.type === "package-body" && node.database && node.label) {
items.push({
id: `pkgbody-${conn.id}-${node.database}-${node.schema || ""}-${node.label}`,
type: "package-body",
label: node.label,
description: `${conn.name} / ${node.database}${node.schema ? " / " + node.schema : ""}`,
connectionId: conn.id,
database: node.database,
schema: node.schema,
objectName: node.label,
connectionName: conn.name,
searchText: `${conn.name} ${node.database} ${node.schema || ""} ${node.label}`,
});
}
// Process children recursively
if (node.children) {
processDatabaseTreeNodes(node.children, conn, items);
}
}
}
const filteredItems = computed((): MatchedItem[] => {
if (!searchQuery.value.trim()) {
return allItems.value.map((item) => ({
...item,
matchScore: Infinity,
matchIndices: [],
}));
}
const matched: MatchedItem[] = [];
for (const item of allItems.value) {
const result = fuzzyMatch(searchQuery.value, item.searchText);
if (result) {
matched.push({
...item,
matchScore: result.score,
matchIndices: result.indices,
});
}
}
// Sort by score and type (connections > databases > tables > other objects for equal scores)
matched.sort((a, b) => {
if (a.matchScore !== b.matchScore) {
return a.matchScore - b.matchScore; // Lower scores (better matches) come first
}
const typeOrder = {
connection: 0,
database: 1,
table: 2,
view: 3,
materialized_view: 4,
procedure: 5,
function: 6,
sequence: 7,
package: 8,
"package-body": 9,
};
return (typeOrder[a.type] ?? 10) - (typeOrder[b.type] ?? 10);
});
return matched;
});
const selectedItem = computed((): MatchedItem | null => {
if (selectedIndex.value < 0 || selectedIndex.value >= filteredItems.value.length) {
return null;
}
return filteredItems.value[selectedIndex.value];
});
function selectNext(): void {
if (selectedIndex.value < filteredItems.value.length - 1) {
selectedIndex.value++;
}
}
function selectPrevious(): void {
if (selectedIndex.value > 0) {
selectedIndex.value--;
}
}
function resetSelection(): void {
selectedIndex.value = 0;
}
function setQuery(query: string): void {
searchQuery.value = query;
resetSelection();
}
return {
searchQuery,
filteredItems,
selectedIndex,
selectedItem,
selectNext,
selectPrevious,
resetSelection,
setQuery,
};
}