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。

feat(sidebar): support creating table group from container context menu (#11193) (#12781)

lxk955298626954424
diff --git a/apps/desktop/src/components/sidebar/SidebarTableVGroupDialog.vue b/apps/desktop/src/components/sidebar/SidebarTableVGroupDialog.vue
index 97c310a501..c0eba08e71 100644
--- a/apps/desktop/src/components/sidebar/SidebarTableVGroupDialog.vue
+++ b/apps/desktop/src/components/sidebar/SidebarTableVGroupDialog.vue
@@ -17,12 +17,22 @@ const emit = defineEmits<{ created: [groupId: string] }>();
const deleteConfirmMessage = computed(() => t("tableVGroup.deleteGroupConfirmMessage", { name: tableVGroupDeleteTarget.value?.name ?? "" }));
+const dialogTitle = computed(() => {
+ if (tableVGroupDialogTableNames.value.length) {
+ return t("tableVGroup.moveToNewGroup");
+ }
+ return tableVGroupDialogParentGroupId.value ? t("tableVGroup.newSubgroup") : t("connectionGroup.createGroup");
+});
+
function confirmCreate() {
const scope = tableVGroupDialogScope.value;
const name = tableVGroupName.value.trim();
if (!scope || !name) return;
const groupId = connectionStore.createTableVGroup(scope, name, tableVGroupDialogParentGroupId.value);
if (groupId) {
+ if (scope && "isExpanded" in scope) {
+ scope.isExpanded = true;
+ }
// scope 即右键的源行节点,其 type 就是这批待移入行的类别(多选保证同类型)。
const rowType = scope.type;
for (const tableName of tableVGroupDialogTableNames.value) {
@@ -48,7 +58,7 @@ function confirmDelete() {
<Dialog v-model:open="showTableVGroupDialog">
<DialogContent class="max-w-sm">
<DialogHeader>
- <DialogTitle>{{ tableVGroupDialogTableNames.length ? t("tableVGroup.moveToNewGroup") : t("tableVGroup.newSubgroup") }}</DialogTitle>
+ <DialogTitle>{{ dialogTitle }}</DialogTitle>
</DialogHeader>
<Input v-model="tableVGroupName" :placeholder="t('connectionGroup.groupNamePlaceholder')" @keydown.enter.prevent="confirmCreate" />
<DialogFooter>
diff --git a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue
index b750bdd09d..557a67538a 100644
--- a/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue
+++ b/apps/desktop/src/components/sidebar/SidebarTreeRuntimeHost.vue
@@ -6899,6 +6899,14 @@ function buildConnectionSidebarMenu(context: SidebarMenuFactoryContext): boolean
return false;
}
+function openCreateTableVGroupDialog(node: TreeNode) {
+ tableVGroupDialogScope.value = node;
+ tableVGroupDialogParentGroupId.value = null;
+ tableVGroupDialogTableNames.value = [];
+ tableVGroupName.value = "";
+ showTableVGroupDialog.value = true;
+}
+
function buildDatabaseSidebarMenu(context: SidebarMenuFactoryContext): boolean {
const { node, items } = context;
appendTableVGroupContainerItems(node, items);
@@ -7000,6 +7008,14 @@ function buildDatabaseSidebarMenu(context: SidebarMenuFactoryContext): boolean {
if (canCreateTable.value) {
items.push({ sidebarActionId: "contextMenu.createTable", label: t("contextMenu.createTable"), action: createTable, icon: Plus });
}
+ if (canCreateTable.value && isTableVGroupContainerNode(node) && !node.children?.some(isGroupLabel)) {
+ items.push({
+ sidebarActionId: "contextMenu.createGroup",
+ label: t("connectionGroup.createGroup"),
+ action: () => openCreateTableVGroupDialog(node),
+ icon: FolderPlus,
+ });
+ }
if (canOpenTableImport.value) {
items.push({ sidebarActionId: "contextMenu.importData", label: t("contextMenu.importData"), action: openTableImport, icon: Download });
}
@@ -7131,6 +7147,16 @@ function buildSpecialSidebarMenu(context: SidebarMenuFactoryContext): boolean {
if (currentDatabaseType() === "hbase" && node.type === "group-tables") {
if (canCreateTable.value) {
items.push({ label: t("contextMenu.createTable"), action: createTable, icon: Plus });
+ }
+ if (isTableVGroupContainerNode(node)) {
+ items.push({
+ sidebarActionId: "contextMenu.createGroup",
+ label: t("connectionGroup.createGroup"),
+ action: () => openCreateTableVGroupDialog(node),
+ icon: FolderPlus,
+ });
+ }
+ if (canCreateTable.value || isTableVGroupContainerNode(node)) {
items.push({ label: "", separator: true });
}
items.push({ label: t("contextMenu.copyName"), action: copyName, icon: Copy, shortcut: shortcutCopyName.value });
@@ -7785,16 +7811,11 @@ function buildObjectGroupSidebarMenu(context: SidebarMenuFactoryContext): boolea
const hasMongoCreateIndexAction = node.type === "group-indexes" && canCreateMongoIndex.value;
const hasMongoDropAllIndexesAction = node.type === "group-indexes" && canDropAllMongoIndexes.value;
const canCreateGroupView = node.type === "group-views" && !!node.connectionId && !!node.database && !["neo4j", "nebula"].includes(currentDatabaseType() || "");
- const hasGroupAction = (node.type === "group-tables" && canCreateTable.value) || canCreateGroupView || !!objectTemplate || hasMongoCreateIndexAction || hasMongoDropAllIndexesAction;
+ const canCreateTableGroup = isTableVGroupContainerNode(node);
+ const hasGroupAction = (node.type === "group-tables" && canCreateTable.value) || canCreateTableGroup || canCreateGroupView || !!objectTemplate || hasMongoCreateIndexAction || hasMongoDropAllIndexesAction;
const canLoadAllObjectGroup = node.type === "group-tables" || node.type === "group-dolt-system-tables" || node.type === "group-views" || node.type === "group-materialized-views";
if (node.type === "group-tables" && canCreateTable.value) {
items.push({ label: t("contextMenu.createTable"), action: createTable, icon: Plus });
- if (canOpenTableImport.value) {
- items.push({ label: t("contextMenu.importData"), action: openTableImport, icon: Upload });
- }
- if (canPasteTreeClipboardToCurrentNode() || canTransferTreeClipboardToCurrentNode()) {
- items.push({ label: t("contextMenu.pasteTable"), action: openPasteTableDialog, icon: Clipboard });
- }
}
if (canCreateGroupView) {
items.push({ label: t("contextMenu.createView"), action: createView, icon: Plus });
@@ -7805,6 +7826,22 @@ function buildObjectGroupSidebarMenu(context: SidebarMenuFactoryContext): boolea
if (objectTemplate) {
items.push({ label: t(objectTemplate.titleKey), action: createSidebarObjectTemplate, icon: Plus });
}
+ if (canCreateTableGroup) {
+ items.push({
+ sidebarActionId: "contextMenu.createGroup",
+ label: t("connectionGroup.createGroup"),
+ action: () => openCreateTableVGroupDialog(node),
+ icon: FolderPlus,
+ });
+ }
+ if (node.type === "group-tables" && canCreateTable.value) {
+ if (canOpenTableImport.value) {
+ items.push({ label: t("contextMenu.importData"), action: openTableImport, icon: Upload });
+ }
+ if (canPasteTreeClipboardToCurrentNode() || canTransferTreeClipboardToCurrentNode()) {
+ items.push({ label: t("contextMenu.pasteTable"), action: openPasteTableDialog, icon: Clipboard });
+ }
+ }
if (hasMongoCreateIndexAction) {
items.push({ label: t("contextMenu.createMongoIndex"), action: openCreateMongoIndexDialog, icon: Plus });
}
diff --git a/apps/desktop/src/components/sidebar/__tests__/SidebarTreeRuntimeHost.tableVGroupContextMenu.spec.ts b/apps/desktop/src/components/sidebar/__tests__/SidebarTreeRuntimeHost.tableVGroupContextMenu.spec.ts
new file mode 100644
index 0000000000..4c4c801933
--- /dev/null
+++ b/apps/desktop/src/components/sidebar/__tests__/SidebarTreeRuntimeHost.tableVGroupContextMenu.spec.ts
@@ -0,0 +1,153 @@
+// @vitest-environment happy-dom
+import { createApp, defineComponent, h, nextTick, ref, type App } from "vue";
+import { createPinia, setActivePinia } from "pinia";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import i18n from "@/i18n";
+import type { ContextMenuItem } from "@/components/ui/CustomContextMenu.vue";
+import type { TreeNode } from "@/types/database";
+
+vi.mock("@/lib/backend/api", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("@/lib/backend/api")>();
+ return {
+ ...actual,
+ listPlugins: vi.fn().mockResolvedValue([]),
+ saveTableVGroups: vi.fn().mockResolvedValue(undefined),
+ };
+});
+
+import SidebarTreeRuntimeHost from "@/components/sidebar/SidebarTreeRuntimeHost.vue";
+import SidebarTableVGroupDialog from "@/components/sidebar/SidebarTableVGroupDialog.vue";
+import { useConnectionStore } from "@/stores/connectionStore";
+import { showTableVGroupDialog, tableVGroupName, tableVGroupDialogScope, tableVGroupDialogParentGroupId, tableVGroupDialogTableNames } from "@/components/sidebar/sidebarTreeDialogState";
+
+const connection = { id: "pg-1", name: "PostgreSQL", db_type: "postgres", driver_profile: "postgresql", host: "localhost", port: 5432, username: "postgres", password: "" };
+const mountedApps: App<Element>[] = [];
+const tr = (key: string) => i18n.global.t(key);
+
+async function mountHost() {
+ const pinia = createPinia();
+ setActivePinia(pinia);
+ const connectionStore = useConnectionStore();
+ connectionStore.connections = [connection];
+ const host = ref<InstanceType<typeof SidebarTreeRuntimeHost> | null>(null);
+ const root = defineComponent({
+ setup: () => () => h(SidebarTreeRuntimeHost, { ref: host, node: { id: `pg-1`, type: "connection", label: connection.name, connectionId: connection.id }, depth: 0 }),
+ });
+ const container = document.createElement("div");
+ document.body.appendChild(container);
+ const app = createApp(root);
+ app.use(pinia);
+ app.use(i18n);
+ app.mount(container);
+ mountedApps.push(app);
+ await nextTick();
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ await nextTick();
+ return host.value as { buildContextMenu(node: TreeNode): ContextMenuItem[] };
+}
+
+async function mountDialog() {
+ const pinia = createPinia();
+ setActivePinia(pinia);
+ const container = document.createElement("div");
+ document.body.appendChild(container);
+ const app = createApp(SidebarTableVGroupDialog);
+ app.use(pinia);
+ app.use(i18n);
+ app.mount(container);
+ mountedApps.push(app);
+ await nextTick();
+ return container;
+}
+
+describe("table virtual group context menus and dialog", () => {
+ beforeEach(() => {
+ showTableVGroupDialog.value = false;
+ tableVGroupName.value = "";
+ tableVGroupDialogScope.value = null;
+ tableVGroupDialogParentGroupId.value = null;
+ tableVGroupDialogTableNames.value = [];
+ });
+
+ afterEach(() => {
+ for (const app of mountedApps.splice(0)) app.unmount();
+ document.body.innerHTML = "";
+ });
+
+ it("provides 'New Group' on group-tables container and triggers dialog with empty selection", async () => {
+ const host = await mountHost();
+ const tablesGroupNode: TreeNode = {
+ id: "pg-1:app:public:group-tables",
+ type: "group-tables",
+ label: "Tables",
+ connectionId: connection.id,
+ database: "app",
+ schema: "public",
+ };
+ const items = host.buildContextMenu(tablesGroupNode);
+ const createGroupItem = items.find((item) => item.label === tr("connectionGroup.createGroup"));
+ expect(createGroupItem).toBeDefined();
+
+ createGroupItem?.action?.();
+ expect(showTableVGroupDialog.value).toBe(true);
+ expect(tableVGroupDialogScope.value).toBe(tablesGroupNode);
+ expect(tableVGroupDialogParentGroupId.value).toBeNull();
+ expect(tableVGroupDialogTableNames.value).toEqual([]);
+ });
+
+ it("provides 'New Group' on group-views container", async () => {
+ const host = await mountHost();
+ const viewsGroupNode: TreeNode = {
+ id: "pg-1:app:public:group-views",
+ type: "group-views",
+ label: "Views",
+ connectionId: connection.id,
+ database: "app",
+ schema: "public",
+ };
+ const items = host.buildContextMenu(viewsGroupNode);
+ const createGroupItem = items.find((item) => item.label === tr("connectionGroup.createGroup"));
+ expect(createGroupItem).toBeDefined();
+ });
+
+ it("provides 'New Subgroup' on table-vgroup nodes", async () => {
+ const host = await mountHost();
+ const vgroupNode: TreeNode = {
+ id: "table-vgroup:grp-1",
+ type: "table-vgroup",
+ label: "User Group",
+ vgroupId: "grp-1",
+ connectionId: connection.id,
+ database: "app",
+ schema: "public",
+ };
+ const items = host.buildContextMenu(vgroupNode);
+ const newSubgroupItem = items.find((item) => item.label === tr("tableVGroup.newSubgroup"));
+ expect(newSubgroupItem).toBeDefined();
+
+ newSubgroupItem?.action?.();
+ expect(showTableVGroupDialog.value).toBe(true);
+ expect(tableVGroupDialogScope.value).toBe(vgroupNode);
+ expect(tableVGroupDialogParentGroupId.value).toBe("grp-1");
+ expect(tableVGroupDialogTableNames.value).toEqual([]);
+ });
+
+ it("dynamically sets the dialog title for container group creation, subgroup, and move-in", async () => {
+ await mountDialog();
+ showTableVGroupDialog.value = true;
+ tableVGroupDialogTableNames.value = [];
+ tableVGroupDialogParentGroupId.value = null;
+ await nextTick();
+
+ const getTitle = () => document.body.querySelector("[data-slot='dialog-title']")?.textContent?.trim();
+ expect(getTitle()).toBe(tr("connectionGroup.createGroup"));
+
+ tableVGroupDialogParentGroupId.value = "grp-1";
+ await nextTick();
+ expect(getTitle()).toBe(tr("tableVGroup.newSubgroup"));
+
+ tableVGroupDialogTableNames.value = ["orders", "users"];
+ await nextTick();
+ expect(getTitle()).toBe(tr("tableVGroup.moveToNewGroup"));
+ });
+});
diff --git a/apps/desktop/src/lib/table/__tests__/tableVGroup.spec.ts b/apps/desktop/src/lib/table/__tests__/tableVGroup.spec.ts
index 866b28a831..7972a945cb 100644
--- a/apps/desktop/src/lib/table/__tests__/tableVGroup.spec.ts
+++ b/apps/desktop/src/lib/table/__tests__/tableVGroup.spec.ts
@@ -83,6 +83,12 @@ describe("tableVGroup layout ops", () => {
expect(toggleTableVGroupCollapsed(toggled, created.groupId).groups[0]?.collapsed).toBe(false);
});
+ it("ensures layout is enabled when creating a new group", () => {
+ const disabledLayout: TableVGroupLayout = { version: 1, enabled: false, groups: [], order: [] };
+ const created = createTableVGroup(disabledLayout, "新分组");
+ expect(created.layout.enabled).toBe(true);
+ });
+
it("reorders a group into another group and refuses cycles", () => {
const a = createTableVGroup(emptyTableVGroupLayout(), "A");
const b = createTableVGroup(a.layout, "B");
@@ -172,6 +178,15 @@ describe("applyTableVGroupsToChildren", () => {
expect(applyTableVGroupsToChildren(children, undefined, SCOPE)).toBe(children);
});
+ it("projects empty groups even when container has no tables", () => {
+ const created = createTableVGroup(emptyTableVGroupLayout(), "空组");
+ const projected = applyTableVGroupsToChildren([], created.layout, SCOPE);
+ expect(projected).toHaveLength(1);
+ expect(projected[0]?.label).toBe("空组");
+ expect(projected[0]?.type).toBe("table-vgroup");
+ expect(projected[0]?.children).toEqual([]);
+ });
+
it("strips projected groups back into a flat list preserving table identity", () => {
const created = createTableVGroup(emptyTableVGroupLayout(), "订单域");
const layout = moveTableToVGroup(created.layout, "t_order", created.groupId);
diff --git a/apps/desktop/src/lib/table/tableVGroup.ts b/apps/desktop/src/lib/table/tableVGroup.ts
index 741fb589a2..40ccc024c0 100644
--- a/apps/desktop/src/lib/table/tableVGroup.ts
+++ b/apps/desktop/src/lib/table/tableVGroup.ts
@@ -297,7 +297,7 @@ export function createTableVGroup(layout: TableVGroupLayout, name: string, paren
return {
groupId,
layout: {
- enabled: layout.enabled,
+ enabled: true,
version: layout.version,
groups: [...layout.groups, group].map((current) => (parentFound && current.id === parentGroupId ? { ...current, collapsed: false } : current)),
order,
@@ -505,7 +505,7 @@ function buildVGroupRootNodes(entries: TableVGroupOrderEntry[], layout: TableVGr
* simply stay absent (sidebar pagination); stale names are skipped.
*/
export function applyTableVGroupsToChildren(children: TreeNode[], layout: TableVGroupLayout | null | undefined, scope: TableVGroupScope): TreeNode[] {
- if (!children.length || !layout || !tableVGroupsEnabled(layout)) return children;
+ if (!layout || !tableVGroupsEnabled(layout) || (!children.length && !layout.groups.length)) return children;
// Idempotence: merge/refresh flows re-feed already-projected children back
// into setChildren, so always strip existing group containers first —
// otherwise stale group copies survive alongside the freshly built ones.
@@ -524,7 +524,7 @@ export function applyTableVGroupsToChildren(children: TreeNode[], layout: TableV
if (!rows.byLabel.has(node.label)) rows.byLabel.set(node.label, { node, flatIndex: i });
} else passthrough.push({ node, sortIndex: i });
}
- if (!rows.byKey.size) return flatChildren;
+ if (!rows.byKey.size && !activeLayout.groups.length) return flatChildren;
const rootBuilt = buildVGroupRootNodes(activeLayout.order, activeLayout, rows, scope);
// Rows the layout does not reference stay ungrouped at their original slot.