дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
feat(plugins): expose database table menus and read-only schema (#7372)
Plugin API 1.20 adds wit/database.wit with describe-table behind the database.schema capability: a read-only view of a connected source's table (columns, keys, indexes) through DbManager::connected_table_details. Plugins can contribute database.table.context menu entries (needs_api 20) that receive the non-secret table identity and the when keys databaseDriver, databaseObjectKind, databaseSchema, databaseTable and databaseSelectionCount. Co-authored-by: gejun123456 <[email protected]>
Цей коміт міститься в:
1 джерело
abc5b2b2c6
коміт
b7c2d4eacb
201 files changed
+1180
-176
No files matched your search
@@ -0,0 +1,99 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||
import type { PluginInfo } from "@/bindings";
|
||||
import { registerAction } from "@/platform/actions";
|
||||
import { nodeMenu } from "@/features/database/treeMenu";
|
||||
import { useDatabase } from "@/features/database/store";
|
||||
import type { DbNode } from "@/features/database/nodes";
|
||||
import { defineBuiltinUiPoints } from "../points";
|
||||
import { clearUiContributions, syncUiContributions } from "../projection";
|
||||
import { pluginDatabaseTableMenuItems, type DatabaseTableContext } from "../uiPoints";
|
||||
|
||||
const initialDatabase = useDatabase.getState();
|
||||
let stop: () => void;
|
||||
let offs: (() => void)[];
|
||||
beforeEach(() => {
|
||||
stop = defineBuiltinUiPoints();
|
||||
offs = [];
|
||||
});
|
||||
afterEach(() => {
|
||||
useDatabase.setState(initialDatabase, true);
|
||||
clearUiContributions();
|
||||
stop();
|
||||
offs.forEach((off) => off());
|
||||
});
|
||||
const plugin = { id: "mapper", name: "Mapper generator", enabled: true, status: "active", icon: null } as PluginInfo;
|
||||
const table: DatabaseTableContext = {
|
||||
sourceId: "pg:inventory",
|
||||
catalog: null,
|
||||
schema: "public",
|
||||
table: "users",
|
||||
kind: "table",
|
||||
driver: "postgres",
|
||||
};
|
||||
function contribute(when?: string) {
|
||||
offs.push(registerAction({ id: "plugin.mapper.generate", title: "Generate mapper", run: () => {} }));
|
||||
syncUiContributions(
|
||||
[
|
||||
{
|
||||
pluginId: "mapper",
|
||||
point: "menus",
|
||||
key: "database.table.context/generate",
|
||||
decl: {
|
||||
menu: "database.table.context",
|
||||
command: "generate",
|
||||
submenu: "Generate",
|
||||
when,
|
||||
},
|
||||
},
|
||||
],
|
||||
[plugin],
|
||||
);
|
||||
}
|
||||
describe("database table plugin menu", () => {
|
||||
it("passes only non-secret table identities and checks when for every selected table", () => {
|
||||
contribute("databaseDriver == postgres && databaseObjectKind == table && databaseSelectionCount == 1");
|
||||
const [entry] = pluginDatabaseTableMenuItems([table]);
|
||||
expect(entry).toEqual({
|
||||
kind: "action",
|
||||
action: "plugin.mapper.generate",
|
||||
arg: JSON.stringify({ tables: [table] }),
|
||||
});
|
||||
expect(pluginDatabaseTableMenuItems([])).toEqual([]);
|
||||
expect(pluginDatabaseTableMenuItems([table, { ...table, table: "orders" }])).toEqual([]);
|
||||
expect(pluginDatabaseTableMenuItems([{ ...table, kind: "view" }])).toEqual([]);
|
||||
expect(pluginDatabaseTableMenuItems([{ ...table, driver: "mysql" }])).toEqual([]);
|
||||
});
|
||||
it("strips extra fields instead of serializing connection settings", () => {
|
||||
contribute();
|
||||
const injected = { ...table, password: "do-not-send", host: "private-host", url: "private-url" };
|
||||
const [entry] = pluginDatabaseTableMenuItems([injected]);
|
||||
expect(entry).toMatchObject({ arg: JSON.stringify({ tables: [table] }) });
|
||||
});
|
||||
it("appends commands to built-in table menus and retains a multi-catalog selection", () => {
|
||||
contribute();
|
||||
useDatabase.setState({ sources: [{ id: "duck", name: "Local", driver: "jdbc", vendor: "duckdb" }] });
|
||||
const a: DbNode = {
|
||||
kind: "table",
|
||||
key: "duck/a/main/users",
|
||||
id: "duck",
|
||||
catalog: "a",
|
||||
schema: "main",
|
||||
table: { name: "users", kind: "table" },
|
||||
};
|
||||
const b: DbNode = { ...a, key: "duck/b/main/users", catalog: "b" };
|
||||
const items = nodeMenu(a, [a, b]);
|
||||
expect(items.length).toBeGreaterThan(1);
|
||||
const entry = items.at(-1)!;
|
||||
expect(entry).toMatchObject({ action: "plugin.mapper.generate" });
|
||||
if (entry.kind !== "action") throw new Error("expected command");
|
||||
expect(JSON.parse(entry.arg as string).tables).toEqual([
|
||||
{ sourceId: "duck", catalog: "a", schema: "main", table: "users", kind: "table", driver: "jdbc" },
|
||||
{ sourceId: "duck", catalog: "b", schema: "main", table: "users", kind: "table", driver: "jdbc" },
|
||||
]);
|
||||
expect(
|
||||
nodeMenu(a, [{ kind: "schema", key: "duck/main", id: "duck", schema: "main" }, a]).some(
|
||||
(item) => item.kind === "action" && item.action === "plugin.mapper.generate",
|
||||
),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -96,6 +96,7 @@ interface MenuDecl {
|
||||
| "commit.message"
|
||||
| "services.context"
|
||||
| "services.toolbar"
|
||||
| "database.table.context"
|
||||
| "openIn";
|
||||
view?: string | null;
|
||||
command: string;
|
||||
@@ -187,6 +188,53 @@ export function pluginViewItemMenuItems(pluginId: string, view: string, key: str
|
||||
return flattenSingleGroups(out);
|
||||
}
|
||||
|
||||
/** Non-secret table context passed to Database table menu commands (API 1.20). */
|
||||
export interface DatabaseTableContext {
|
||||
sourceId: string;
|
||||
catalog: string | null;
|
||||
schema: string;
|
||||
table: string;
|
||||
kind: string;
|
||||
driver: string;
|
||||
}
|
||||
|
||||
/** The right-clicked table is included in selection by the Database tree. */
|
||||
export function pluginDatabaseTableMenuItems(tables: readonly DatabaseTableContext[]): MenuItem[] {
|
||||
if (!tables.length) return [];
|
||||
const out: MenuItem[] = [];
|
||||
const groups = new Map<string, MenuItem[]>();
|
||||
// Explicitly pick non-secret fields; never forward a DataSource or DbNode.
|
||||
const arg = JSON.stringify({
|
||||
tables: tables.map(({ sourceId, catalog, schema, table, kind, driver }) => ({
|
||||
sourceId,
|
||||
catalog,
|
||||
schema,
|
||||
table,
|
||||
kind,
|
||||
driver,
|
||||
})),
|
||||
});
|
||||
for (const { pluginId, decl } of contextItems.values()) {
|
||||
if (decl.menu !== "database.table.context") continue;
|
||||
if (
|
||||
!tables.every((table) =>
|
||||
evaluateWhen(decl.when ?? undefined, {
|
||||
...getContext(),
|
||||
databaseDriver: table.driver,
|
||||
databaseObjectKind: table.kind,
|
||||
databaseSchema: table.schema,
|
||||
databaseTable: table.table,
|
||||
databaseSelectionCount: tables.length,
|
||||
}),
|
||||
)
|
||||
)
|
||||
continue;
|
||||
const action = pluginActionId(pluginId, decl.command);
|
||||
if (getAction(action)) addGrouped(out, groups, pluginId, decl.submenu, { kind: "action", action, arg });
|
||||
}
|
||||
return flattenSingleGroups(out);
|
||||
}
|
||||
|
||||
/** The when keys of one selected service. */
|
||||
export function serviceWhenContext(service: ServiceContext, count: number) {
|
||||
return {
|
||||
|
||||
Reference in new issue
Заблокувати користувача