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。

40.3 KB
Raw Blame
use std::future::Future;
use std::sync::Arc;

use axum::extract::State;
use axum::http::HeaderMap;
use axum::Json;
use serde::Deserialize;

use crate::error::AppError;
use crate::state::WebState;

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoParseShellCommandRequest {
    source: String,
}

pub async fn parse_shell_command(
    Json(req): Json<MongoParseShellCommandRequest>,
) -> Result<Json<dbx_core::mongo_shell::MongoCommand>, AppError> {
    dbx_core::mongo_shell::parse(&req.source).map(Json).map_err(AppError::from)
}

async fn run_cancellable<T, F>(state: &Arc<WebState>, execution_id: Option<String>, future: F) -> Result<T, AppError>
where
    F: Future<Output = Result<T, String>>,
{
    let registered = execution_id
        .as_ref()
        .filter(|id| !id.trim().is_empty())
        .map(|id| state.app.running_queries.register(id.clone()));
    if let Some(query) = registered.as_ref() {
        let token = query.token();
        tokio::select! {
            biased;
            _ = token.cancelled() => Err(AppError::from(dbx_core::query::canceled_error())),
            result = future => result.map_err(AppError::from),
        }
    } else {
        future.await.map_err(AppError::from)
    }
}

/// Check if a connection is read-only and return an error if so.
async fn ensure_writable(
    app: &dbx_core::connection::AppState,
    connection_id: &str,
    action: &str,
) -> Result<(), AppError> {
    if let Some(name) = dbx_core::query::connection_readonly_name(app, connection_id).await {
        return Err(AppError::from(format!(
            "Read-only mode: connection '{}' has read-only protection enabled. {} blocked.",
            name, action
        )));
    }
    Ok(())
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoConnectionRequest {
    pub connection_id: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoCollectionRequest {
    pub connection_id: String,
    pub database: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoCollectionNameRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoRenameCollectionRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub new_name: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoCloneCollectionRequest {
    pub connection_id: String,
    pub database: String,
    pub source_collection: String,
    pub target_collection: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoFindRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub skip: Option<u64>,
    pub limit: Option<i64>,
    pub filter: Option<String>,
    pub projection: Option<String>,
    pub sort: Option<String>,
    pub collation: Option<String>,
    pub execution_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoFindExplainRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub skip: Option<u64>,
    pub limit: Option<i64>,
    pub filter: Option<String>,
    pub projection: Option<String>,
    pub sort: Option<String>,
    pub collation: Option<String>,
    pub verbosity: Option<String>,
    pub execution_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoFindOneRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub filter: Option<String>,
    pub projection: Option<String>,
    pub options: Option<String>,
    pub execution_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoCountRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub filter: Option<String>,
    pub mode: Option<String>,
    pub execution_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoServerVersionRequest {
    pub connection_id: String,
    pub database: String,
    pub execution_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoCollectionStatsRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub scale: Option<serde_json::Number>,
    pub execution_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoAggregateRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub pipeline_json: String,
    pub max_rows: Option<usize>,
    pub execution_id: Option<String>,
    pub options_json: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoDistinctRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub field: String,
    pub filter: Option<String>,
    pub execution_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoCreateIndexRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub keys_json: String,
    pub options_json: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoCreateUserRequest {
    pub connection_id: String,
    pub database: String,
    pub user_json: String,
    pub write_concern_json: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoRunCommandRequest {
    pub connection_id: String,
    pub database: String,
    pub command_json: String,
    pub execution_id: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoDropIndexesRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub indexes_json: Option<String>,
    pub single: bool,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoInsertRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub doc_json: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoInsertDocumentsRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub docs_json: String,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoUpdateDocumentsRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub filter_json: String,
    pub update_json: String,
    pub many: bool,
    pub options_json: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoReplaceDocumentRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub filter_json: String,
    pub replacement_json: String,
    pub options_json: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoBulkWriteRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub operations_json: String,
    pub options_json: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoDeleteDocumentsRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub filter_json: String,
    pub many: bool,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoFindOneAndUpdateRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub filter_json: String,
    pub update_json: String,
    pub options_json: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoFindOneAndReplaceRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub filter_json: String,
    pub replacement_json: String,
    pub options_json: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoFindOneAndDeleteRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub filter_json: String,
    pub options_json: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoUpdateRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub id: String,
    pub doc_json: String,
    pub routing: Option<String>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MongoDeleteRequest {
    pub connection_id: String,
    pub database: String,
    pub collection: String,
    pub id: String,
    pub routing: Option<String>,
}

pub async fn list_databases(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoConnectionRequest>,
) -> Result<Json<Vec<String>>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let result =
        dbx_core::mongo_ops::mongo_list_databases_core(&state.app, &req.connection_id).await.map_err(AppError::from)?;
    Ok(Json(result))
}

pub async fn list_collections(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoCollectionRequest>,
) -> Result<Json<Vec<dbx_core::document_ops::CollectionInfo>>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    let result = dbx_core::mongo_ops::mongo_list_collections_core(&state.app, &req.connection_id, &database)
        .await
        .map_err(AppError::from)?;
    Ok(Json(result))
}

pub async fn create_database(
    State(state): State<Arc<WebState>>,
    Json(req): Json<MongoCollectionRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    ensure_writable(&state.app, &req.connection_id, "Create database").await?;
    dbx_core::mongo_ops::mongo_create_database_core(&state.app, &req.connection_id, &req.database)
        .await
        .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn drop_database(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoCollectionRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Drop database").await?;
    ensure_writable(&state.app, &req.connection_id, "Drop database").await?;
    dbx_core::mongo_ops::mongo_drop_database_core(&state.app, &req.connection_id, &database)
        .await
        .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn drop_collection(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoCollectionNameRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Drop collection")
        .await?;
    ensure_writable(&state.app, &req.connection_id, "Drop collection").await?;
    dbx_core::mongo_ops::mongo_drop_collection_core(&state.app, &req.connection_id, &database, &req.collection)
        .await
        .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn rename_collection(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoRenameCollectionRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Rename collection")
        .await?;
    ensure_writable(&state.app, &req.connection_id, "Rename collection").await?;
    dbx_core::mongo_ops::mongo_rename_collection_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.new_name,
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "ok": true })))
}

pub async fn clone_collection(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoCloneCollectionRequest>,
) -> Result<Json<dbx_core::db::mongo_driver::MongoCloneCollectionResult>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Clone collection")
        .await?;
    ensure_writable(&state.app, &req.connection_id, "Clone collection").await?;
    let result = dbx_core::mongo_ops::mongo_clone_collection_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.source_collection,
        &req.target_collection,
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(result))
}

pub async fn find_documents(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoFindRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    let result = run_cancellable(
        &state,
        req.execution_id.clone(),
        dbx_core::document_ops::find_documents_core(
            &state.app,
            &req.connection_id,
            &database,
            &req.collection,
            req.skip.unwrap_or(0),
            req.limit.unwrap_or(50),
            req.filter.as_deref(),
            req.projection.as_deref(),
            req.sort.as_deref(),
            req.collation.as_deref(),
            None,
            false,
        ),
    )
    .await?;
    if result.documents.is_empty() {
        dbx_core::mongo_ops::ensure_collection_exists_for_query(
            &state.app,
            &req.connection_id,
            &database,
            &req.collection,
        )
        .await?;
    }
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn explain_find(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoFindExplainRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    let verbosity = req.verbosity.as_deref().unwrap_or("queryPlanner");
    let result = run_cancellable(
        &state,
        req.execution_id.clone(),
        dbx_core::mongo_ops::mongo_explain_find_core(
            &state.app,
            &req.connection_id,
            &database,
            &req.collection,
            req.skip.unwrap_or(0),
            req.limit.unwrap_or(100),
            req.filter.as_deref(),
            req.projection.as_deref(),
            req.sort.as_deref(),
            req.collation.as_deref(),
            verbosity,
        ),
    )
    .await?;
    Ok(Json(result))
}

pub async fn find_one(
    State(state): State<Arc<WebState>>,
    Json(req): Json<MongoFindOneRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let result = run_cancellable(
        &state,
        req.execution_id,
        dbx_core::mongo_ops::mongo_find_one_core(
            &state.app,
            &req.connection_id,
            &req.database,
            &req.collection,
            req.filter.as_deref(),
            req.projection.as_deref(),
            req.options.as_deref(),
        ),
    )
    .await?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn count_documents(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoCountRequest>,
) -> Result<Json<u64>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    let result = run_cancellable(
        &state,
        req.execution_id,
        dbx_core::mongo_ops::mongo_count_documents_core(
            &state.app,
            &req.connection_id,
            &database,
            &req.collection,
            req.filter.as_deref(),
            req.mode.as_deref(),
        ),
    )
    .await?;
    Ok(Json(result))
}

pub async fn server_version(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoServerVersionRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    let result = run_cancellable(
        &state,
        req.execution_id.clone(),
        dbx_core::mongo_ops::mongo_server_version_core(&state.app, &req.connection_id, &database),
    )
    .await?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn collection_stats(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoCollectionStatsRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    let result = run_cancellable(
        &state,
        req.execution_id.clone(),
        dbx_core::mongo_ops::mongo_collection_stats_core(
            &state.app,
            &req.connection_id,
            &database,
            &req.collection,
            req.scale,
        ),
    )
    .await?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn aggregate_documents(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoAggregateRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let database = super::mcp_policy::ensure_mongo_pipeline_target(
        &state,
        &headers,
        &req.connection_id,
        &req.database,
        &req.pipeline_json,
    )
    .await?;
    if super::mcp_policy::mongo_pipeline_has_write_stage(&req.pipeline_json) {
        super::mcp_policy::ensure_dangerous_write(
            &state,
            &headers,
            &req.connection_id,
            &database,
            "MongoDB aggregate write",
        )
        .await?;
    }
    let result = run_cancellable(
        &state,
        req.execution_id.clone(),
        dbx_core::mongo_ops::mongo_aggregate_documents_core(
            &state.app,
            &req.connection_id,
            &database,
            &req.collection,
            &req.pipeline_json,
            req.max_rows,
            req.options_json.as_deref(),
        ),
    )
    .await?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn distinct(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoDistinctRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    let result = run_cancellable(
        &state,
        req.execution_id.clone(),
        dbx_core::mongo_ops::mongo_distinct_core(
            &state.app,
            &req.connection_id,
            &database,
            &req.collection,
            &req.field,
            req.filter.as_deref(),
        ),
    )
    .await?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn list_index_specs(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoCollectionNameRequest>,
) -> Result<Json<Vec<dbx_core::db::mongo_driver::MongoIndexSpec>>, AppError> {
    super::mcp_policy::ensure_scope(&state, &headers, &req.connection_id).await?;
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    let result =
        dbx_core::mongo_ops::mongo_list_index_specs_core(&state.app, &req.connection_id, &database, &req.collection)
            .await
            .map_err(AppError::from)?;
    Ok(Json(result))
}

pub async fn create_index(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoCreateIndexRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Create index").await?;
    ensure_writable(&state.app, &req.connection_id, "Create index").await?;
    let name = dbx_core::mongo_ops::mongo_create_index_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.keys_json,
        req.options_json.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "name": name })))
}

pub async fn create_user(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoCreateUserRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Create user").await?;
    ensure_writable(&state.app, &req.connection_id, "Create user").await?;
    let affected_rows = dbx_core::mongo_ops::mongo_create_user_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.user_json,
        req.write_concern_json.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "affected_rows": affected_rows })))
}

pub async fn run_command(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoRunCommandRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Run MongoDB command")
        .await?;
    ensure_writable(&state.app, &req.connection_id, "Run MongoDB command").await?;
    let result = run_cancellable(
        &state,
        req.execution_id.clone(),
        dbx_core::mongo_ops::mongo_run_command_core(&state.app, &req.connection_id, &database, &req.command_json),
    )
    .await?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn drop_indexes(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoDropIndexesRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Drop indexes").await?;
    ensure_writable(&state.app, &req.connection_id, "Drop indexes").await?;
    let result = dbx_core::mongo_ops::mongo_drop_indexes_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        req.indexes_json.as_deref(),
        req.single,
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn insert_document(
    State(state): State<Arc<WebState>>,
    Json(req): Json<MongoInsertRequest>,
) -> Result<Json<String>, AppError> {
    ensure_writable(&state.app, &req.connection_id, "Insert").await?;
    let result = dbx_core::document_ops::insert_document_core(
        &state.app,
        &req.connection_id,
        &req.database,
        &req.collection,
        &req.doc_json,
        None,
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(result))
}

pub async fn insert_documents(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoInsertDocumentsRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &database, "Insert").await?;
    ensure_writable(&state.app, &req.connection_id, "Insert").await?;
    let result = dbx_core::mongo_ops::mongo_insert_documents_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.docs_json,
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "affected_rows": result })))
}

pub async fn update_document(
    State(state): State<Arc<WebState>>,
    Json(req): Json<MongoUpdateRequest>,
) -> Result<Json<u64>, AppError> {
    ensure_writable(&state.app, &req.connection_id, "Update").await?;
    let result = dbx_core::document_ops::update_document_core(
        &state.app,
        &req.connection_id,
        &req.database,
        &req.collection,
        &req.id,
        &req.doc_json,
        req.routing.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(result))
}

pub async fn update_documents(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoUpdateDocumentsRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    if super::mcp_policy::mongo_filter_is_effectively_unbounded(&req.filter_json) {
        super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Update").await?;
    } else {
        super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &database, "Update").await?;
    }
    ensure_writable(&state.app, &req.connection_id, "Update").await?;
    let result = dbx_core::mongo_ops::mongo_update_documents_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.filter_json,
        &req.update_json,
        req.many,
        req.options_json.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "affected_rows": result })))
}

pub async fn replace_document(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoReplaceDocumentRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    if super::mcp_policy::mongo_filter_is_effectively_unbounded(&req.filter_json) {
        super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Replace").await?;
    } else {
        super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &database, "Replace").await?;
    }
    ensure_writable(&state.app, &req.connection_id, "Replace").await?;
    let result = dbx_core::mongo_ops::mongo_replace_document_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.filter_json,
        &req.replacement_json,
        req.options_json.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "affected_rows": result })))
}

pub async fn bulk_write(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoBulkWriteRequest>,
) -> Result<Json<dbx_core::db::mongo_driver::MongoBulkWriteResult>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    let command = dbx_core::mongo_shell::MongoCommand::BulkWrite {
        collection: req.collection.clone(),
        operations: req.operations_json.clone(),
        options: None,
    };
    if command.has_effectively_unbounded_filter() {
        super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "BulkWrite").await?;
    } else {
        super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &database, "BulkWrite").await?;
    }
    ensure_writable(&state.app, &req.connection_id, "BulkWrite").await?;
    let result = dbx_core::mongo_ops::mongo_bulk_write_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.operations_json,
        req.options_json.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(result))
}

pub async fn find_one_and_update(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoFindOneAndUpdateRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    ensure_find_one_write_policy(&state, &headers, &req.connection_id, &database, &req.filter_json, "Update").await?;
    ensure_writable(&state.app, &req.connection_id, "Update").await?;
    let result = dbx_core::mongo_ops::mongo_find_one_and_update_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.filter_json,
        &req.update_json,
        req.options_json.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn find_one_and_replace(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoFindOneAndReplaceRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    ensure_find_one_write_policy(&state, &headers, &req.connection_id, &database, &req.filter_json, "Replace").await?;
    ensure_writable(&state.app, &req.connection_id, "Update").await?;
    let result = dbx_core::mongo_ops::mongo_find_one_and_replace_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.filter_json,
        &req.replacement_json,
        req.options_json.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn find_one_and_delete(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoFindOneAndDeleteRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    ensure_find_one_write_policy(&state, &headers, &req.connection_id, &database, &req.filter_json, "Delete").await?;
    ensure_writable(&state.app, &req.connection_id, "Delete").await?;
    let result = dbx_core::mongo_ops::mongo_find_one_and_delete_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.filter_json,
        req.options_json.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::to_value(result).map_err(|e| AppError::from(e.to_string()))?))
}

pub async fn delete_document(
    State(state): State<Arc<WebState>>,
    Json(req): Json<MongoDeleteRequest>,
) -> Result<Json<u64>, AppError> {
    ensure_writable(&state.app, &req.connection_id, "Delete").await?;
    let result = dbx_core::document_ops::delete_document_core(
        &state.app,
        &req.connection_id,
        &req.database,
        &req.collection,
        &req.id,
        req.routing.as_deref(),
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(result))
}

pub async fn delete_documents(
    State(state): State<Arc<WebState>>,
    headers: HeaderMap,
    Json(req): Json<MongoDeleteDocumentsRequest>,
) -> Result<Json<serde_json::Value>, AppError> {
    let database = super::mcp_policy::resolve_database(&state, &headers, &req.connection_id, &req.database).await?;
    if super::mcp_policy::mongo_filter_is_effectively_unbounded(&req.filter_json) {
        super::mcp_policy::ensure_dangerous_write(&state, &headers, &req.connection_id, &database, "Delete").await?;
    } else {
        super::mcp_policy::ensure_write(&state, &headers, &req.connection_id, &database, "Delete").await?;
    }
    ensure_writable(&state.app, &req.connection_id, "Delete").await?;
    let result = dbx_core::mongo_ops::mongo_delete_documents_core(
        &state.app,
        &req.connection_id,
        &database,
        &req.collection,
        &req.filter_json,
        req.many,
    )
    .await
    .map_err(AppError::from)?;
    Ok(Json(serde_json::json!({ "affected_rows": result })))
}

async fn ensure_find_one_write_policy(
    state: &Arc<WebState>,
    headers: &HeaderMap,
    connection_id: &str,
    database: &str,
    filter_json: &str,
    action: &str,
) -> Result<(), AppError> {
    if super::mcp_policy::mongo_filter_is_effectively_unbounded(filter_json) {
        super::mcp_policy::ensure_dangerous_write(state, headers, connection_id, database, action).await
    } else {
        super::mcp_policy::ensure_write(state, headers, connection_id, database, action).await
    }
}

#[cfg(test)]
mod tests {
    use super::{
        clone_collection, drop_database, ensure_find_one_write_policy, MongoCloneCollectionRequest,
        MongoCollectionRequest,
    };
    use crate::state::WebState;
    use axum::{
        extract::State,
        http::{HeaderMap, HeaderValue},
        Json,
    };
    use dbx_core::connection::AppState;
    use dbx_core::models::connection::ConnectionConfig;
    use dbx_core::storage::McpGlobalPolicy;
    use std::sync::Arc;

    fn mongo_config(is_production: bool) -> ConnectionConfig {
        serde_json::from_value(serde_json::json!({
            "id": "mongo-policy-test",
            "name": "Mongo policy test",
            "db_type": "mongodb",
            "host": "localhost",
            "port": 27017,
            "username": "tester",
            "password": "",
            "database": "app",
            "is_production": is_production
        }))
        .unwrap()
    }

    async fn test_web_state() -> (Arc<WebState>, std::path::PathBuf) {
        let dir = std::env::temp_dir().join(format!("dbx-web-mongo-policy-test-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let storage = dbx_core::persistence::test_storage::open(&dir.join("storage.db")).await.unwrap();
        let app = Arc::new(AppState::new_with_plugin_dir(storage, dir.join("plugins")));
        let state = Arc::new(WebState::for_tests(app, dir.clone()));
        (state, dir)
    }

    fn mcp_headers() -> HeaderMap {
        let mut headers = HeaderMap::new();
        headers.insert("x-dbx-mcp-request", HeaderValue::from_static("1"));
        headers
    }

    #[tokio::test]
    async fn find_one_writes_recheck_policy_filter_production_and_allowlist() {
        let (state, dir) = test_web_state().await;
        let connection = mongo_config(false);
        state.app.storage.save_connections(std::slice::from_ref(&connection)).await.unwrap();
        let headers = mcp_headers();
        let writable_policy = McpGlobalPolicy {
            read_only: false,
            allow_dangerous_sql: false,
            allowed_connection_ids: Some(vec![connection.id.clone()]),
            ..Default::default()
        };
        state.app.storage.save_mcp_global_policy(&writable_policy).await.unwrap();

        assert!(ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Update")
            .await
            .is_ok());

        state
            .app
            .storage
            .save_mcp_global_policy(&McpGlobalPolicy { read_only: true, ..writable_policy.clone() })
            .await
            .unwrap();
        let revoked = ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Update")
            .await
            .unwrap_err();
        assert!(revoked.message.starts_with("MCP_READ_ONLY:"), "{}", revoked.message);

        state.app.storage.save_mcp_global_policy(&writable_policy).await.unwrap();
        let empty_filter =
            ensure_find_one_write_policy(&state, &headers, &connection.id, "app", "{}", "Delete").await.unwrap_err();
        assert!(empty_filter.message.starts_with("SQL_BLOCKED:"), "{}", empty_filter.message);

        state
            .app
            .storage
            .save_mcp_global_policy(&McpGlobalPolicy { allow_dangerous_sql: true, ..writable_policy.clone() })
            .await
            .unwrap();
        assert!(ensure_find_one_write_policy(&state, &headers, &connection.id, "app", "{}", "Replace").await.is_ok());

        state.app.storage.save_connections(&[mongo_config(true)]).await.unwrap();
        let production =
            ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Update")
                .await
                .unwrap_err();
        assert!(production.message.starts_with("PRODUCTION_DATABASE_READ_ONLY:"), "{}", production.message);

        state.app.storage.save_connections(std::slice::from_ref(&connection)).await.unwrap();
        state
            .app
            .storage
            .save_mcp_global_policy(&McpGlobalPolicy {
                read_only: false,
                allow_dangerous_sql: true,
                allowed_connection_ids: Some(vec!["different-connection".to_string()]),
                ..Default::default()
            })
            .await
            .unwrap();
        let allowlist = ensure_find_one_write_policy(&state, &headers, &connection.id, "app", r#"{"_id":1}"#, "Delete")
            .await
            .unwrap_err();
        assert!(allowlist.message.starts_with("CONNECTION_OUT_OF_SCOPE:"), "{}", allowlist.message);

        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn drop_database_requires_mcp_dangerous_write_approval() {
        let (state, dir) = test_web_state().await;
        let connection = mongo_config(false);
        state.app.storage.save_connections(std::slice::from_ref(&connection)).await.unwrap();
        state
            .app
            .storage
            .save_mcp_global_policy(&McpGlobalPolicy {
                read_only: false,
                allow_dangerous_sql: false,
                allowed_connection_ids: Some(vec![connection.id.clone()]),
                ..Default::default()
            })
            .await
            .unwrap();

        let error = drop_database(
            State(state.clone()),
            mcp_headers(),
            Json(MongoCollectionRequest { connection_id: connection.id, database: "app".to_string() }),
        )
        .await
        .unwrap_err();
        assert!(error.message.starts_with("SQL_BLOCKED:"), "{}", error.message);

        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }

    #[tokio::test]
    async fn clone_collection_requires_mcp_dangerous_write_approval() {
        let (state, dir) = test_web_state().await;
        let connection = mongo_config(false);
        state.app.storage.save_connections(std::slice::from_ref(&connection)).await.unwrap();
        state
            .app
            .storage
            .save_mcp_global_policy(&McpGlobalPolicy {
                read_only: false,
                allow_dangerous_sql: false,
                allowed_connection_ids: Some(vec![connection.id.clone()]),
                ..Default::default()
            })
            .await
            .unwrap();

        let error = clone_collection(
            State(state.clone()),
            mcp_headers(),
            Json(MongoCloneCollectionRequest {
                connection_id: connection.id,
                database: "app".to_string(),
                source_collection: "users".to_string(),
                target_collection: "users_copy".to_string(),
            }),
        )
        .await
        .unwrap_err();
        assert!(error.message.starts_with("SQL_BLOCKED:"), "{}", error.message);

        drop(state);
        let _ = std::fs::remove_dir_all(dir);
    }
}