Files
dbx/apps/desktop/src/composables/useSidebarTreeToolRuntime.ts
T

210 lines
7.6 KiB
TypeScript

import type { ShallowRef } from "vue";
import type { useConnectionStore } from "@/stores/connectionStore";
import type { useQueryStore } from "@/stores/queryStore";
import type { useSettingsStore } from "@/stores/settingsStore";
import type { TreeNode } from "@/types/database";
import { allDatabasesExportSourceForNode, databaseExportSourceForNode, sidebarSameSchemaStructureTargets } from "@/lib/sidebar/sidebarExportRuntime";
import { schemaDiffRoutineKey } from "@/lib/schema/schemaDiffRoutine";
interface SidebarTreeToolRuntimeOptions {
activeNode: ShallowRef<TreeNode>;
connectionStore: ReturnType<typeof useConnectionStore>;
queryStore: ReturnType<typeof useQueryStore>;
settingsStore: ReturnType<typeof useSettingsStore>;
tableChildObjectName: (node: TreeNode) => string;
acceptedSelectionIds?: () => readonly string[] | null;
}
export function useSidebarTreeToolRuntime(options: SidebarTreeToolRuntimeOptions) {
const { activeNode, connectionStore, queryStore, settingsStore } = options;
function openTransfer() {
if (!activeNode.value.connectionId) return;
connectionStore.transferSource = {
connectionId: activeNode.value.connectionId,
database: activeNode.value.database ?? "",
catalog: activeNode.value.catalog,
};
}
function openSchemaDiff(options?: { selectedRoutines?: string[]; preferredResultTab?: "tables" | "routines" }) {
if (!activeNode.value.connectionId) return;
connectionStore.schemaDiffSource = {
connectionId: activeNode.value.connectionId,
database: activeNode.value.database ?? "",
schema: activeNode.value.schema,
selectedRoutines: options?.selectedRoutines,
preferredResultTab: options?.preferredResultTab,
};
}
function openSchemaDiffForRoutine() {
const node = activeNode.value;
if (!node.connectionId || (node.type !== "procedure" && node.type !== "function")) return;
openSchemaDiff({
selectedRoutines: [schemaDiffRoutineKey(node.objectName || node.label, node.signature ?? "")],
preferredResultTab: "routines",
});
}
function openDataCompare() {
if (!activeNode.value.connectionId) return;
connectionStore.dataCompareSource = {
connectionId: activeNode.value.connectionId,
database: activeNode.value.database ?? "",
schema: activeNode.value.schema,
tableName: activeNode.value.type === "table" ? activeNode.value.label : undefined,
};
}
function openSqlFileExecution() {
if (!activeNode.value.connectionId) return;
connectionStore.sqlFileSource = {
connectionId: activeNode.value.connectionId,
database: activeNode.value.database ?? "",
};
}
function selectedSameSchemaStructureTargets() {
return sidebarSameSchemaStructureTargets(activeNode.value, connectionStore.treeNodes, options.acceptedSelectionIds?.() ?? connectionStore.selectedTreeNodeIds);
}
function openDiagram() {
const node = activeNode.value;
if (!node.connectionId || !node.database) return;
const tables = selectedSameSchemaStructureTargets().filter((target) => target.type === "table");
const tableNames = tables.length > 1 ? tables.map((target) => target.label) : undefined;
connectionStore.diagramSource = {
connectionId: node.connectionId,
database: node.database,
schema: node.schema,
tableName: tableNames?.length ? tableNames[0] : node.type === "table" ? node.label : undefined,
tableNames,
};
}
function openDocs() {
const node = activeNode.value;
if (!node.connectionId || !node.database) return;
connectionStore.docsSource = {
connectionId: node.connectionId,
database: node.database,
// A database node has no schema, and an absent schema is what tells the
// collector to document every schema in the database.
schema: node.schema,
};
}
function openDatabaseSearch() {
const node = activeNode.value;
if (!node.connectionId || !node.database) return;
connectionStore.databaseSearchSource = {
connectionId: node.connectionId,
database: node.database,
schema: node.type === "schema" ? node.schema : undefined,
};
}
function openDatabaseExport() {
const node = activeNode.value;
const tables = selectedSameSchemaStructureTargets().filter((target) => target.type === "table");
if (tables.length > 1 && node.connectionId && node.database) {
connectionStore.databaseExportSource = {
connectionId: node.connectionId,
database: node.database,
schema: node.schema,
tableNames: tables.map((target) => target.label),
};
return;
}
connectionStore.databaseExportSource = databaseExportSourceForNode(node);
}
function openAllDatabasesExport() {
connectionStore.databaseExportSource = allDatabasesExportSourceForNode(activeNode.value);
}
function openScheduledBackups() {
settingsStore.requestSettingsNavigation("backups");
}
function openTableImport() {
const node = activeNode.value;
if (!node.connectionId || !node.database) return;
connectionStore.tableImportSource = {
connectionId: node.connectionId,
database: node.database,
schema: node.schema,
tableName: node.type === "table" ? node.label : undefined,
};
}
function openMongoImport() {
const node = activeNode.value;
if (!node.connectionId || !node.database || node.type !== "mongo-collection") return;
connectionStore.mongoImportSource = {
connectionId: node.connectionId,
database: node.database,
collection: node.label,
};
}
function openMongoDatabaseDump(mode: "dump" | "restore") {
const node = activeNode.value;
if (node.type !== "mongo-db" || !node.connectionId || !node.database) return;
connectionStore.mongoDatabaseDumpSource = { connectionId: node.connectionId, database: node.database, mode };
}
function openStructureEditor() {
const node = activeNode.value;
if (!node.connectionId || !node.database) return;
if (node.type === "table") {
queryStore.openTableStructure(node.connectionId, node.database, node.schema, node.label, undefined, undefined, node.catalog);
return;
}
if (node.type === "column" && node.tableName) {
const columnName = options.tableChildObjectName(node).trim();
if (!columnName) return;
queryStore.openTableStructure(node.connectionId, node.database, node.schema, node.tableName, "columns", { kind: "column", name: columnName }, node.catalog);
return;
}
if (node.type === "index" && node.tableName) {
const indexName = options.tableChildObjectName(node).trim();
if (!indexName) return;
queryStore.openTableStructure(node.connectionId, node.database, node.schema, node.tableName, "indexes", { kind: "index", name: indexName }, node.catalog);
}
}
function openFieldLineage() {
const node = activeNode.value;
const column = node.type === "column" && node.meta && "name" in node.meta ? node.meta.name : node.label;
if (node.type !== "column" || !node.connectionId || !node.database || !node.tableName || !column) return;
connectionStore.fieldLineageSource = {
connectionId: node.connectionId,
database: node.database,
schema: node.schema,
tableName: node.tableName,
columnName: column,
};
}
return {
openAllDatabasesExport,
openDataCompare,
openDatabaseExport,
openDatabaseSearch,
openDiagram,
openDocs,
openFieldLineage,
openMongoImport,
openMongoDatabaseDump,
openScheduledBackups,
openSchemaDiff,
openSchemaDiffForRoutine,
openSqlFileExecution,
openStructureEditor,
openTableImport,
openTransfer,
};
}