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。

2.3 KB
Raw Blame
import { computed, ref } from "vue";
import { loadHistoryRetentionLimit, saveHistoryRetentionLimit } from "@/lib/backend/api";

export const HISTORY_RETENTION_LIMITS = [200, 1000, 5000, 10000, 0] as const;
export const DEFAULT_HISTORY_RETENTION_LIMIT = 1000;

export function isHistoryRetentionLimit(value: unknown): value is number {
  return typeof value === "number" && HISTORY_RETENTION_LIMITS.some((limit) => limit === value);
}

/** A server-owned setting: browser localStorage cannot configure MCP retention. */
export function useHistoryRetentionSetting(loadLimit: () => Promise<number> = loadHistoryRetentionLimit, saveLimit: (limit: number) => Promise<void> = saveHistoryRetentionLimit) {
  const draft = ref(DEFAULT_HISTORY_RETENTION_LIMIT);
  const persisted = ref(DEFAULT_HISTORY_RETENTION_LIMIT);
  const loaded = ref(false);
  const loading = ref(false);
  const saving = ref(false);
  const loadError = ref("");
  let generation = 0;
  const changed = computed(() => loaded.value && draft.value !== persisted.value);
  const invalid = computed(() => loaded.value && !isHistoryRetentionLimit(draft.value));

  async function load() {
    const request = ++generation;
    loaded.value = false;
    loading.value = true;
    loadError.value = "";
    try {
      const value = await loadLimit();
      if (!isHistoryRetentionLimit(value)) throw new Error("Invalid query history retention limit");
      if (request !== generation) return;
      draft.value = persisted.value = value;
      loaded.value = true;
    } catch (error) {
      if (request === generation) loadError.value = error instanceof Error ? error.message : String(error);
    } finally {
      if (request === generation) loading.value = false;
    }
  }

  async function save() {
    if (!changed.value || saving.value) return;
    if (invalid.value) throw new Error("Invalid query history retention limit");
    const value = draft.value;
    saving.value = true;
    try {
      await saveLimit(value);
      persisted.value = value;
    } finally {
      saving.value = false;
    }
  }

  function discard() {
    generation += 1;
    loading.value = false;
    draft.value = persisted.value;
  }

  function reset() {
    if (loaded.value) draft.value = DEFAULT_HISTORY_RETENTION_LIMIT;
  }

  return { draft, loaded, loading, saving, loadError, changed, invalid, load, save, discard, reset };
}