feat(config): support selective connection import and export
This commit is contained in:
1 parent
89e0f1734b
commit
e4577abeed
21 files changed
+1171
-175
No files matched your search
@@ -0,0 +1,102 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { ConnectionConfig, SidebarLayout, TunnelProfile } from "@/types/database";
|
||||
import { buildConnectionConfigBundle, parseConnectionConfigObject, selectConnectionConfigBundle, snapshotConnectionsForExport } from "./connectionConfigTransfer";
|
||||
|
||||
function conn(id: string, name: string, extras: Partial<ConnectionConfig> = {}): ConnectionConfig {
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
db_type: "mysql",
|
||||
host: "127.0.0.1",
|
||||
port: 3306,
|
||||
username: "root",
|
||||
password: "secret",
|
||||
...extras,
|
||||
};
|
||||
}
|
||||
|
||||
const layout: SidebarLayout = {
|
||||
groups: [
|
||||
{ id: "prod", name: "Prod", collapsed: false },
|
||||
{ id: "dev", name: "Dev", collapsed: false },
|
||||
],
|
||||
order: [
|
||||
{
|
||||
type: "group",
|
||||
id: "prod",
|
||||
children: [
|
||||
{ type: "connection", id: "a" },
|
||||
{ type: "connection", id: "b" },
|
||||
],
|
||||
},
|
||||
{
|
||||
type: "group",
|
||||
id: "dev",
|
||||
children: [{ type: "connection", id: "c" }],
|
||||
},
|
||||
{ type: "connection", id: "d" },
|
||||
],
|
||||
};
|
||||
|
||||
const tunnel1 = { type: "ssh", id: "tunnel-1", host: "bastion-1", port: 22, user: "root" } as TunnelProfile;
|
||||
const tunnel2 = { type: "ssh", id: "tunnel-2", host: "bastion-2", port: 22, user: "root" } as TunnelProfile;
|
||||
|
||||
describe("connectionConfigTransfer", () => {
|
||||
it("filters connections, empty groups, and unused tunnel profiles", () => {
|
||||
const connections = [
|
||||
conn("a", "A", { transport_layers: [{ type: "ssh", id: "layer-a", host: "", port: 22, user: "", profile_id: "tunnel-1" }] }),
|
||||
conn("b", "B", { transport_layers: [{ type: "ssh", id: "layer-b", host: "", port: 22, user: "", profile_id: "tunnel-2" }] }),
|
||||
conn("c", "C", { transport_layers: [{ type: "ssh", id: "layer-c", host: "", port: 22, user: "", profile_id: "tunnel-1" }] }),
|
||||
conn("d", "D"),
|
||||
];
|
||||
|
||||
const bundle = buildConnectionConfigBundle(connections, layout, [tunnel1, tunnel2], ["a", "c"]);
|
||||
expect(bundle.connections.map((connection) => connection.id)).toEqual(["a", "c"]);
|
||||
expect(bundle.layout?.groups.map((group) => group.name)).toEqual(["Prod", "Dev"]);
|
||||
expect(bundle.tunnelProfiles?.map((profile) => profile.id)).toEqual(["tunnel-1"]);
|
||||
expect(bundle.connections.some((connection) => connection.id === "b")).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps a shared tunnel profile only once", () => {
|
||||
const connections = [conn("a", "A", { transport_layers: [{ type: "ssh", id: "layer-a", host: "", port: 22, user: "", profile_id: "tunnel-1" }] }), conn("c", "C", { transport_layers: [{ type: "ssh", id: "layer-c", host: "", port: 22, user: "", profile_id: "tunnel-1" }] })];
|
||||
const bundle = buildConnectionConfigBundle(connections, null, [tunnel1, tunnel1, tunnel2], ["a", "c"]);
|
||||
expect(bundle.tunnelProfiles).toEqual([tunnel1]);
|
||||
});
|
||||
|
||||
it("exports the full set when no selection is provided", () => {
|
||||
const connections = [conn("a", "A"), conn("b", "B"), conn("c", "C"), conn("d", "D")];
|
||||
const bundle = buildConnectionConfigBundle(connections, layout, [tunnel1], undefined);
|
||||
expect(bundle.connections.map((connection) => connection.id)).toEqual(["a", "b", "c", "d"]);
|
||||
expect(bundle.layout?.order.some((entry) => entry.type === "connection" && entry.id === "d")).toBe(true);
|
||||
});
|
||||
|
||||
it("snapshots inherited timeouts before filtering", () => {
|
||||
const exported = snapshotConnectionsForExport([conn("a", "A", { connect_timeout_inherit: true, connect_timeout_secs: 99, query_timeout_inherit: true, query_timeout_secs: 99 })], {
|
||||
connectTimeoutSecs: () => 7,
|
||||
queryTimeoutSecs: () => 12,
|
||||
});
|
||||
expect(exported[0]).toMatchObject({
|
||||
connect_timeout_secs: 7,
|
||||
connect_timeout_inherit: true,
|
||||
query_timeout_secs: 12,
|
||||
query_timeout_inherit: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("parses legacy arrays and dbx-config payloads without inventing a layout", () => {
|
||||
expect(parseConnectionConfigObject([conn("a", "A")])).toEqual({ connections: [conn("a", "A")] });
|
||||
expect(parseConnectionConfigObject({ format: "dbx-config", connections: [conn("a", "A")] })).toEqual({ connections: [conn("a", "A")] });
|
||||
});
|
||||
|
||||
it("selects a preview subset without mutating the original preview", () => {
|
||||
const preview = {
|
||||
connections: [conn("a", "A"), conn("b", "B"), conn("c", "C")],
|
||||
layout,
|
||||
tunnelProfiles: [tunnel1, tunnel2],
|
||||
};
|
||||
const selected = selectConnectionConfigBundle(preview, ["a", "c"]);
|
||||
expect(selected.connections.map((connection) => connection.id)).toEqual(["a", "c"]);
|
||||
expect(preview.connections).toHaveLength(3);
|
||||
expect(selected.layout?.order.some((entry) => entry.type === "connection" && entry.id === "d")).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
import type { ConnectionConfig, SidebarLayout, TunnelProfile } from "@/types/database";
|
||||
import { filterSidebarLayoutByConnectionIds as filterLayoutByConnectionIds } from "@/lib/sidebar/sidebarLayout";
|
||||
|
||||
export interface ConnectionConfigBundle {
|
||||
connections: ConnectionConfig[];
|
||||
layout?: SidebarLayout;
|
||||
tunnelProfiles?: TunnelProfile[];
|
||||
}
|
||||
|
||||
export interface ConnectionConfigSnapshotOptions {
|
||||
connectTimeoutSecs: (connection: ConnectionConfig) => number;
|
||||
queryTimeoutSecs: (connection: ConnectionConfig) => number;
|
||||
}
|
||||
|
||||
export function selectedConnectionIdSet(connectionIds: Iterable<string> | undefined): Set<string> | undefined {
|
||||
if (connectionIds == null) return undefined;
|
||||
return new Set(Array.from(connectionIds).filter((id) => typeof id === "string" && id.length > 0));
|
||||
}
|
||||
|
||||
export function filterConnectionsByIds(connections: ConnectionConfig[], selectedIds?: Iterable<string>): ConnectionConfig[] {
|
||||
const selected = selectedConnectionIdSet(selectedIds);
|
||||
if (!selected) return [...connections];
|
||||
return connections.filter((connection) => selected.has(connection.id));
|
||||
}
|
||||
|
||||
export function filterSidebarLayoutByConnectionIds(layout: SidebarLayout | null | undefined, selectedIds: Iterable<string>): SidebarLayout {
|
||||
return filterLayoutByConnectionIds(layout, Array.from(selectedConnectionIdSet(selectedIds) ?? []));
|
||||
}
|
||||
|
||||
export function referencedTunnelProfileIds(connections: Iterable<Pick<ConnectionConfig, "transport_layers">>): Set<string> {
|
||||
const ids = new Set<string>();
|
||||
for (const connection of connections) {
|
||||
for (const layer of connection.transport_layers ?? []) {
|
||||
if (typeof layer.profile_id === "string" && layer.profile_id) ids.add(layer.profile_id);
|
||||
}
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
export function filterTunnelProfilesByIds(profiles: TunnelProfile[], selectedIds: Iterable<string>): TunnelProfile[] {
|
||||
const selected = selectedConnectionIdSet(selectedIds);
|
||||
if (!selected) return [...profiles];
|
||||
const seen = new Set<string>();
|
||||
const filtered: TunnelProfile[] = [];
|
||||
for (const profile of profiles) {
|
||||
if (!selected.has(profile.id) || seen.has(profile.id)) continue;
|
||||
seen.add(profile.id);
|
||||
filtered.push(profile);
|
||||
}
|
||||
return filtered;
|
||||
}
|
||||
|
||||
export function snapshotConnectionsForExport(connections: ConnectionConfig[], options: ConnectionConfigSnapshotOptions): ConnectionConfig[] {
|
||||
return connections.map((connection) => ({
|
||||
...connection,
|
||||
connect_timeout_secs: connection.connect_timeout_inherit === true ? options.connectTimeoutSecs(connection) : connection.connect_timeout_secs,
|
||||
query_timeout_secs: connection.query_timeout_inherit === true ? options.queryTimeoutSecs(connection) : connection.query_timeout_secs,
|
||||
}));
|
||||
}
|
||||
|
||||
export function buildConnectionConfigBundle(connections: ConnectionConfig[], layout: SidebarLayout | null | undefined, tunnelProfiles: TunnelProfile[], selectedIds?: Iterable<string>): ConnectionConfigBundle {
|
||||
const selectedConnections = filterConnectionsByIds(connections, selectedIds);
|
||||
const selectedConnectionIds = selectedConnections.map((connection) => connection.id);
|
||||
return {
|
||||
connections: selectedConnections,
|
||||
layout: filterSidebarLayoutByConnectionIds(layout, selectedConnectionIds),
|
||||
tunnelProfiles: filterTunnelProfilesByIds(tunnelProfiles, referencedTunnelProfileIds(selectedConnections)),
|
||||
};
|
||||
}
|
||||
|
||||
export function parseConnectionConfigObject(value: unknown): ConnectionConfigBundle {
|
||||
if (Array.isArray(value)) {
|
||||
return { connections: value as ConnectionConfig[] };
|
||||
}
|
||||
|
||||
if (!value || typeof value !== "object") {
|
||||
return { connections: [] };
|
||||
}
|
||||
|
||||
const parsed = value as {
|
||||
format?: unknown;
|
||||
connections?: unknown;
|
||||
layout?: SidebarLayout;
|
||||
tunnelProfiles?: unknown;
|
||||
};
|
||||
|
||||
if (parsed.format === "dbx-config" && Array.isArray(parsed.connections)) {
|
||||
return { connections: parsed.connections as ConnectionConfig[] };
|
||||
}
|
||||
|
||||
if (Array.isArray(parsed.connections)) {
|
||||
return {
|
||||
connections: parsed.connections as ConnectionConfig[],
|
||||
layout: parsed.layout?.groups && parsed.layout?.order ? parsed.layout : undefined,
|
||||
tunnelProfiles: Array.isArray(parsed.tunnelProfiles) ? (parsed.tunnelProfiles as TunnelProfile[]) : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
return { connections: [] };
|
||||
}
|
||||
|
||||
export function selectConnectionConfigBundle(bundle: ConnectionConfigBundle, selectedIds?: Iterable<string>): ConnectionConfigBundle {
|
||||
const selectedConnections = filterConnectionsByIds(bundle.connections, selectedIds);
|
||||
const selectedConnectionIds = selectedConnections.map((connection) => connection.id);
|
||||
return {
|
||||
connections: selectedConnections,
|
||||
layout: bundle.layout ? filterSidebarLayoutByConnectionIds(bundle.layout, selectedConnectionIds) : undefined,
|
||||
tunnelProfiles: filterTunnelProfilesByIds(bundle.tunnelProfiles ?? [], referencedTunnelProfileIds(selectedConnections)),
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user