feat(mongodb): support createIndex shell command (#2187)

This commit is contained in:
onenewcode authored and GitHub committed 2026-06-30 21:34:58 +08:00
1 parent 4e012a9dd9
commit b90e4fd64e
17 files changed
+374 -18

No files matched your search

+43
View File
@@ -70,6 +70,15 @@ struct MongoAggregateDocumentsRequest {
max_rows: Option<usize>,
}
#[derive(Deserialize)]
struct MongoCreateIndexRequest {
connection_name: String,
database: Option<String>,
collection: String,
keys_json: String,
options_json: Option<String>,
}
#[derive(Deserialize)]
struct MongoInsertDocumentsRequest {
connection_name: String,
@@ -168,6 +177,8 @@ pub fn start(app_handle: AppHandle, state: Arc<AppState>) {
handle_mongo_server_version_data(&st, body, &mut stream).await;
} else if first_line.starts_with("POST /data/mongo/aggregate-documents") {
handle_mongo_aggregate_documents_data(&st, body, &mut stream).await;
} else if first_line.starts_with("POST /data/mongo/create-index") {
handle_mongo_create_index_data(&st, body, &mut stream).await;
} else if first_line.starts_with("POST /data/mongo/insert-documents") {
handle_mongo_insert_documents_data(&st, body, &mut stream).await;
} else if first_line.starts_with("POST /data/mongo/update-documents") {
@@ -476,6 +487,38 @@ async fn handle_mongo_aggregate_documents_data(state: &Arc<AppState>, body: &str
}
}
async fn handle_mongo_create_index_data(state: &Arc<AppState>, body: &str, stream: &mut tokio::net::TcpStream) {
let req: MongoCreateIndexRequest = match serde_json::from_str(body) {
Ok(r) => r,
Err(_) => {
respond_error(stream, "400 Bad Request", "Invalid JSON").await;
return;
}
};
let Some((pool_key, database, connection_id)) =
resolve_mongo_pool_key(state, &req.connection_name, req.database, stream).await
else {
return;
};
if let Err(e) = ensure_connection_writable(state, &connection_id, "Create index").await {
respond_error(stream, "403 Forbidden", &e).await;
return;
}
match dbx_core::mongo_ops::mongo_create_index_core(
state,
&pool_key,
&database,
&req.collection,
&req.keys_json,
req.options_json.as_deref(),
)
.await
{
Ok(name) => respond_json(stream, &serde_json::json!({ "name": name })).await,
Err(e) => respond_error(stream, "500 Internal Server Error", &e).await,
}
}
async fn handle_mongo_insert_documents_data(state: &Arc<AppState>, body: &str, stream: &mut tokio::net::TcpStream) {
let req: MongoInsertDocumentsRequest = match serde_json::from_str(body) {
Ok(r) => r,
+22
View File
@@ -175,6 +175,28 @@ pub async fn mongo_aggregate_documents(
.await
}
#[tauri::command]
pub async fn mongo_create_index(
state: State<'_, Arc<AppState>>,
connection_id: String,
database: String,
collection: String,
keys_json: String,
options_json: Option<String>,
) -> Result<serde_json::Value, String> {
ensure_connection_writable(&state, &connection_id, "Create index").await?;
let name = dbx_core::mongo_ops::mongo_create_index_core(
&state,
&connection_id,
&database,
&collection,
&keys_json,
options_json.as_deref(),
)
.await?;
Ok(serde_json::json!({ "name": name }))
}
#[tauri::command]
pub async fn mongo_insert_document(
state: State<'_, Arc<AppState>>,