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。

3.2 KB
Raw Blame
import { computed, onBeforeUnmount, ref, watch, type Ref } from "vue";
import * as api from "@/lib/backend/api";
import { useConnectionStore } from "@/stores/connectionStore";
import { uuid } from "@/lib/common/utils";
import { objectDependencyRows, objectDependencySql, type ObjectDependencyRow, type ObjectDependencyTarget } from "@/lib/database/objectDependencyDetails";
import type { ObjectRelationKind } from "@/lib/database/oracleObjectDependencies";
import type { QueryResult } from "@/types/database";
import { isQueryExecutionErrorResult } from "@/lib/query/queryResultError";

export function useObjectDependencies<Row = ObjectDependencyRow>(target: Readonly<Ref<ObjectDependencyTarget | undefined>>, enabled: Readonly<Ref<boolean>>, options?: { kind: ObjectRelationKind; parse: (result: QueryResult) => Row[] }) {
  const connectionStore = useConnectionStore();
  const rows = ref<Row[]>([]) as Ref<Row[]>;
  const loading = ref(false);
  const error = ref("");
  const loaded = ref(false);
  const truncated = ref(false);
  const key = computed(() => (target.value ? JSON.stringify(target.value) : ""));
  let generation = 0;
  let executionId: string | undefined;

  function invalidate() {
    ++generation;
    if (executionId) void api.cancelQuery(executionId).catch(() => false);
    executionId = undefined;
  }

  async function refresh() {
    const identity = target.value;
    if (!identity || !enabled.value) return;
    invalidate();
    const serial = generation;
    const requestKey = key.value;
    const id = `object-dependencies-${uuid()}`;
    executionId = id;
    loading.value = true;
    error.value = "";
    try {
      await connectionStore.ensureConnected(identity.connectionId);
      if (serial !== generation || requestKey !== key.value) return;
      const result = await api.executeQuery(identity.connectionId, identity.database, objectDependencySql(identity, options?.kind), identity.schema, id, { catalog: identity.catalog });
      if (serial !== generation || requestKey !== key.value) return;
      if (isQueryExecutionErrorResult(result)) throw new Error(String(result.rows[0]?.[0] ?? "Unable to read object relations."));
      rows.value = options ? options.parse(result) : (objectDependencyRows(result) as Row[]);
      truncated.value = !!(result.truncated || result.has_more);
      loaded.value = true;
    } catch (e: unknown) {
      if (serial === generation && requestKey === key.value) error.value = e instanceof Error ? e.message : String(e);
    } finally {
      if (serial === generation) {
        loading.value = false;
        executionId = undefined;
      }
    }
  }

  watch(
    key,
    () => {
      invalidate();
      rows.value = [];
      error.value = "";
      loaded.value = false;
      truncated.value = false;
      loading.value = false;
      if (enabled.value && target.value) void refresh();
    },
    { flush: "sync" },
  );
  watch(
    enabled,
    (value) => {
      if (!value) {
        invalidate();
        loading.value = false;
      }
      if (value && !loaded.value && !loading.value && target.value) void refresh();
    },
    { immediate: true },
  );
  onBeforeUnmount(invalidate);
  return { rows, loading, error, loaded, truncated, refresh };
}