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。

refactor(agent): wrap mongo legacy calls

t8y2a8f717b3f693
diff --git a/crates/dbx-core/src/db/agent_driver.rs b/crates/dbx-core/src/db/agent_driver.rs
index 050dd906f8..af04b31e45 100644
--- a/crates/dbx-core/src/db/agent_driver.rs
+++ b/crates/dbx-core/src/db/agent_driver.rs
@@ -103,6 +103,29 @@ impl AgentMethod {
}
}
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub enum MongoAgentMethod {
+ ListDatabases,
+ ListCollections,
+ FindDocuments,
+ InsertDocument,
+ UpdateDocument,
+ DeleteDocument,
+}
+
+impl MongoAgentMethod {
+ pub fn as_str(self) -> &'static str {
+ match self {
+ Self::ListDatabases => "list_databases",
+ Self::ListCollections => "list_collections",
+ Self::FindDocuments => "find_documents",
+ Self::InsertDocument => "insert_document",
+ Self::UpdateDocument => "update_document",
+ Self::DeleteDocument => "delete_document",
+ }
+ }
+}
+
struct StderrTail {
lines: VecDeque<String>,
capacity: usize,
@@ -320,6 +343,53 @@ impl AgentDriverClient {
self.call_method(AgentMethod::FetchQueryPage, params).await
}
+ pub async fn call_mongo_method<T: DeserializeOwned + Send + 'static>(
+ &mut self,
+ method: MongoAgentMethod,
+ params: Value,
+ ) -> Result<T, String> {
+ self.call(method.as_str(), params).await
+ }
+
+ pub async fn mongo_list_databases<T: DeserializeOwned + Send + 'static>(&mut self) -> Result<T, String> {
+ self.call_mongo_method(MongoAgentMethod::ListDatabases, serde_json::json!({})).await
+ }
+
+ pub async fn mongo_list_collections<T: DeserializeOwned + Send + 'static>(
+ &mut self,
+ database: &str,
+ ) -> Result<T, String> {
+ self.call_mongo_method(MongoAgentMethod::ListCollections, mongo_database_params(database)).await
+ }
+
+ pub async fn mongo_find_documents<T: DeserializeOwned + Send + 'static>(
+ &mut self,
+ params: Value,
+ ) -> Result<T, String> {
+ self.call_mongo_method(MongoAgentMethod::FindDocuments, params).await
+ }
+
+ pub async fn mongo_insert_document<T: DeserializeOwned + Send + 'static>(
+ &mut self,
+ params: Value,
+ ) -> Result<T, String> {
+ self.call_mongo_method(MongoAgentMethod::InsertDocument, params).await
+ }
+
+ pub async fn mongo_update_document<T: DeserializeOwned + Send + 'static>(
+ &mut self,
+ params: Value,
+ ) -> Result<T, String> {
+ self.call_mongo_method(MongoAgentMethod::UpdateDocument, params).await
+ }
+
+ pub async fn mongo_delete_document<T: DeserializeOwned + Send + 'static>(
+ &mut self,
+ params: Value,
+ ) -> Result<T, String> {
+ self.call_mongo_method(MongoAgentMethod::DeleteDocument, params).await
+ }
+
pub async fn try_optional_handshake(&mut self, app_version: &str) -> Option<AgentHandshake> {
match self.call_method::<AgentHandshake>(AgentMethod::Handshake, agent_handshake_params(app_version)).await {
Ok(handshake) => {
@@ -385,6 +455,18 @@ pub fn is_unsupported_handshake_error(error: &str) -> bool {
|| error.contains("method not found: handshake")
}
+pub fn mongo_database_params(database: &str) -> Value {
+ serde_json::json!({ "database": database })
+}
+
+pub fn mongo_collection_params(database: &str, collection: &str) -> Value {
+ serde_json::json!({ "database": database, "collection": collection })
+}
+
+pub fn mongo_document_id_params(database: &str, collection: &str, id: &str) -> Value {
+ serde_json::json!({ "database": database, "collection": collection, "id": id })
+}
+
fn agent_java_args(jar_path: &str) -> Vec<String> {
[
"-Dfile.encoding=UTF-8",
@@ -493,8 +575,9 @@ impl Drop for AgentDriverClient {
mod tests {
use super::{
agent_handshake_params, agent_java_args, agent_proxy_env_vars, format_agent_process_error,
- is_unsupported_handshake_error, read_agent_line, AgentCapability, AgentDriverClient, AgentHandshake,
- AgentMethod, StderrTail, AGENT_PROTOCOL_VERSION,
+ is_unsupported_handshake_error, mongo_collection_params, mongo_database_params, mongo_document_id_params,
+ read_agent_line, AgentCapability, AgentDriverClient, AgentHandshake, AgentMethod, MongoAgentMethod, StderrTail,
+ AGENT_PROTOCOL_VERSION,
};
use std::io::Cursor;
@@ -615,6 +698,16 @@ mod tests {
assert_eq!(AgentMethod::Shutdown.as_str(), "shutdown");
}
+ #[test]
+ fn defines_mongo_agent_protocol_methods() {
+ assert_eq!(MongoAgentMethod::ListDatabases.as_str(), "list_databases");
+ assert_eq!(MongoAgentMethod::ListCollections.as_str(), "list_collections");
+ assert_eq!(MongoAgentMethod::FindDocuments.as_str(), "find_documents");
+ assert_eq!(MongoAgentMethod::InsertDocument.as_str(), "insert_document");
+ assert_eq!(MongoAgentMethod::UpdateDocument.as_str(), "update_document");
+ assert_eq!(MongoAgentMethod::DeleteDocument.as_str(), "delete_document");
+ }
+
#[test]
fn exposes_schema_and_query_protocol_wrappers() {
let _list_databases = AgentDriverClient::list_databases::<serde_json::Value>;
@@ -624,6 +717,29 @@ mod tests {
let _fetch_query_page = AgentDriverClient::fetch_query_page::<serde_json::Value>;
}
+ #[test]
+ fn exposes_mongo_protocol_wrappers() {
+ let _mongo_list_databases = AgentDriverClient::mongo_list_databases::<serde_json::Value>;
+ let _mongo_list_collections = AgentDriverClient::mongo_list_collections::<serde_json::Value>;
+ let _mongo_find_documents = AgentDriverClient::mongo_find_documents::<serde_json::Value>;
+ let _mongo_insert_document = AgentDriverClient::mongo_insert_document::<serde_json::Value>;
+ let _mongo_update_document = AgentDriverClient::mongo_update_document::<serde_json::Value>;
+ let _mongo_delete_document = AgentDriverClient::mongo_delete_document::<serde_json::Value>;
+ }
+
+ #[test]
+ fn builds_mongo_agent_request_params() {
+ assert_eq!(mongo_database_params("app"), serde_json::json!({ "database": "app" }));
+ assert_eq!(
+ mongo_collection_params("app", "orders"),
+ serde_json::json!({ "database": "app", "collection": "orders" })
+ );
+ assert_eq!(
+ mongo_document_id_params("app", "orders", "abc"),
+ serde_json::json!({ "database": "app", "collection": "orders", "id": "abc" })
+ );
+ }
+
#[test]
fn checks_handshake_capability_support() {
let handshake = AgentHandshake {
diff --git a/crates/dbx-core/src/mongo_ops.rs b/crates/dbx-core/src/mongo_ops.rs
index 3e1414bd48..7afdc0bd4f 100644
--- a/crates/dbx-core/src/mongo_ops.rs
+++ b/crates/dbx-core/src/mongo_ops.rs
@@ -1,4 +1,5 @@
use crate::connection::{AppState, PoolKind};
+use crate::db::agent_driver::mongo_document_id_params;
use crate::db::elasticsearch_driver;
use crate::db::mongo_driver::{self, MongoDocumentResult};
@@ -9,7 +10,7 @@ pub async fn mongo_list_databases_core(state: &AppState, connection_id: &str) ->
PoolKind::Elasticsearch(_) => Ok(vec!["default".to_string()]),
PoolKind::Agent(client) => {
let mut client = client.lock().await;
- let result: Vec<serde_json::Value> = client.call("list_databases", serde_json::json!({})).await?;
+ let result: Vec<serde_json::Value> = client.mongo_list_databases().await?;
Ok(result.iter().filter_map(|v| v.get("name")?.as_str().map(String::from)).collect())
}
_ => Err("Not a MongoDB/Elasticsearch connection".to_string()),
@@ -27,7 +28,7 @@ pub async fn mongo_list_collections_core(
PoolKind::Elasticsearch(client) => elasticsearch_driver::list_indices(client).await,
PoolKind::Agent(client) => {
let mut client = client.lock().await;
- client.call("list_collections", serde_json::json!({ "database": database })).await
+ client.mongo_list_collections(database).await
}
_ => Err("Not a MongoDB/Elasticsearch connection".to_string()),
}
@@ -56,17 +57,14 @@ pub async fn mongo_find_documents_core(
PoolKind::Agent(client) => {
let mut client = client.lock().await;
client
- .call(
- "find_documents",
- serde_json::json!({
- "database": database,
- "collection": collection,
- "skip": skip,
- "limit": limit,
- "filter": filter,
- "sort": sort,
- }),
- )
+ .mongo_find_documents(serde_json::json!({
+ "database": database,
+ "collection": collection,
+ "skip": skip,
+ "limit": limit,
+ "filter": filter,
+ "sort": sort,
+ }))
.await
}
_ => Err("Not a MongoDB/Elasticsearch connection".to_string()),
@@ -91,14 +89,11 @@ pub async fn mongo_insert_document_core(
PoolKind::Agent(client) => {
let mut client = client.lock().await;
let result: serde_json::Value = client
- .call(
- "insert_document",
- serde_json::json!({
- "database": database,
- "collection": collection,
- "doc_json": doc_json,
- }),
- )
+ .mongo_insert_document(serde_json::json!({
+ "database": database,
+ "collection": collection,
+ "doc_json": doc_json,
+ }))
.await?;
Ok(result.get("inserted_id").and_then(|v| v.as_str()).unwrap_or("").to_string())
}
@@ -125,15 +120,12 @@ pub async fn mongo_update_document_core(
PoolKind::Agent(client) => {
let mut client = client.lock().await;
let result: serde_json::Value = client
- .call(
- "update_document",
- serde_json::json!({
- "database": database,
- "collection": collection,
- "id": id,
- "doc_json": doc_json,
- }),
- )
+ .mongo_update_document(serde_json::json!({
+ "database": database,
+ "collection": collection,
+ "id": id,
+ "doc_json": doc_json,
+ }))
.await?;
Ok(result.get("modified_count").and_then(|v| v.as_u64()).unwrap_or(0))
}
@@ -158,16 +150,8 @@ pub async fn mongo_delete_document_core(
}
PoolKind::Agent(client) => {
let mut client = client.lock().await;
- let result: serde_json::Value = client
- .call(
- "delete_document",
- serde_json::json!({
- "database": database,
- "collection": collection,
- "id": id,
- }),
- )
- .await?;
+ let result: serde_json::Value =
+ client.mongo_delete_document(mongo_document_id_params(database, collection, id)).await?;
Ok(result.get("deleted_count").and_then(|v| v.as_u64()).unwrap_or(0))
}
_ => Err("Not a MongoDB/Elasticsearch connection".to_string()),