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。

1.9 KB
Raw Blame
import type { Ref } from "vue";
import type { ConnectionConfig } from "@/types/database";
import { executeWithProductionContextGuard } from "@/lib/database/productionExecutionGuard";

export type RunMongoSidebarMutationOptions<T> = {
  connection?: ConnectionConfig;
  database: string;
  reviewText: string;
  source: string;
  loading: Ref<boolean>;
  /** Runs after production confirmation and after loading is set (e.g. ensureConnected). */
  beforeExecute?: () => Promise<void>;
  execute: () => Promise<T>;
  onSuccess: (result: T) => void | Promise<void>;
  onError?: (error: unknown) => void;
};

/**
 * Shared production-gated mutation shell for Mongo sidebar writes.
 * Production confirmation runs before loading so cancel never shows a busy state.
 *
 * Success is boxed as `{ result }` so void executes are not confused with cancel
 * (`executeWithProductionContextGuard` returns `undefined` only when cancelled).
 */
export async function runMongoSidebarMutation<T>(options: RunMongoSidebarMutationOptions<T>): Promise<void> {
  if (options.loading.value) return;
  try {
    const executed = await executeWithProductionContextGuard({
      connection: options.connection,
      database: options.database,
      reviewText: options.reviewText,
      source: options.source,
      execute: async () => {
        options.loading.value = true;
        if (options.beforeExecute) {
          await options.beforeExecute();
        }
        const result = await options.execute();
        return { result };
      },
    });
    // Cancel only: guard returns undefined when the user declines production confirmation.
    if (executed === undefined) return;
    await options.onSuccess(executed.result);
  } catch (error: unknown) {
    options.onError?.(error);
  } finally {
    options.loading.value = false;
  }
}

// DataGrid and sidebar mutations intentionally share the same production-gated lifecycle.
export const runMongoMutation = runMongoSidebarMutation;