fix(mongodb): support shell projection and db.version
This commit is contained in:
1 parent
44011cb9a4
commit
7a5984a1fd
24 files changed
+390
-38
No files matched your search
@@ -243,16 +243,18 @@ pub enum MongoAgentMethod {
|
||||
ListDatabases,
|
||||
ListCollections,
|
||||
FindDocuments,
|
||||
ServerVersion,
|
||||
InsertDocument,
|
||||
UpdateDocument,
|
||||
DeleteDocument,
|
||||
}
|
||||
|
||||
impl MongoAgentMethod {
|
||||
pub const ALL: [Self; 6] = [
|
||||
pub const ALL: [Self; 7] = [
|
||||
Self::ListDatabases,
|
||||
Self::ListCollections,
|
||||
Self::FindDocuments,
|
||||
Self::ServerVersion,
|
||||
Self::InsertDocument,
|
||||
Self::UpdateDocument,
|
||||
Self::DeleteDocument,
|
||||
@@ -263,6 +265,7 @@ impl MongoAgentMethod {
|
||||
Self::ListDatabases => "list_databases",
|
||||
Self::ListCollections => "list_collections",
|
||||
Self::FindDocuments => "find_documents",
|
||||
Self::ServerVersion => "server_version",
|
||||
Self::InsertDocument => "insert_document",
|
||||
Self::UpdateDocument => "update_document",
|
||||
Self::DeleteDocument => "delete_document",
|
||||
@@ -921,6 +924,13 @@ impl AgentDriverClient {
|
||||
self.call_mongo_method(MongoAgentMethod::FindDocuments, params).await
|
||||
}
|
||||
|
||||
pub async fn mongo_server_version<T: DeserializeOwned + Send + 'static>(
|
||||
&mut self,
|
||||
database: &str,
|
||||
) -> Result<T, String> {
|
||||
self.call_mongo_method(MongoAgentMethod::ServerVersion, mongo_database_params(database)).await
|
||||
}
|
||||
|
||||
pub async fn mongo_insert_document<T: DeserializeOwned + Send + 'static>(
|
||||
&mut self,
|
||||
params: Value,
|
||||
@@ -1461,6 +1471,7 @@ mod tests {
|
||||
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::ServerVersion.as_str(), "server_version");
|
||||
assert_eq!(MongoAgentMethod::InsertDocument.as_str(), "insert_document");
|
||||
assert_eq!(MongoAgentMethod::UpdateDocument.as_str(), "update_document");
|
||||
assert_eq!(MongoAgentMethod::DeleteDocument.as_str(), "delete_document");
|
||||
@@ -1500,6 +1511,7 @@ mod tests {
|
||||
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_server_version = AgentDriverClient::mongo_server_version::<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>;
|
||||
|
||||
@@ -113,6 +113,17 @@ pub async fn test_connection(client: &Client, timeout: Duration, database: Optio
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn server_version(client: &Client, database: &str) -> Result<String, String> {
|
||||
let database = database.trim();
|
||||
let database = if database.is_empty() { "admin" } else { database };
|
||||
let result = client.database(database).run_command(doc! { "buildInfo": 1 }).await.map_err(|e| e.to_string())?;
|
||||
server_version_from_build_info(&result)
|
||||
}
|
||||
|
||||
fn server_version_from_build_info(result: &Document) -> Result<String, String> {
|
||||
result.get_str("version").map(str::to_string).map_err(|e| format!("MongoDB server version not found: {e}"))
|
||||
}
|
||||
|
||||
pub async fn list_databases(client: &Client) -> Result<Vec<String>, String> {
|
||||
client.list_database_names().await.map_err(|e| e.to_string())
|
||||
}
|
||||
@@ -193,6 +204,7 @@ pub async fn find_documents(
|
||||
skip: u64,
|
||||
limit: i64,
|
||||
filter: Option<&str>,
|
||||
projection: Option<&str>,
|
||||
sort: Option<&str>,
|
||||
) -> Result<MongoDocumentResult, String> {
|
||||
let col = client.database(database).collection::<Document>(collection);
|
||||
@@ -212,6 +224,14 @@ pub async fn find_documents(
|
||||
};
|
||||
|
||||
let mut find = col.find(filter_doc).skip(skip).limit(limit);
|
||||
if let Some(p) = projection {
|
||||
if !p.trim().is_empty() {
|
||||
let json: serde_json::Value =
|
||||
serde_json::from_str(p).map_err(|e| format!("Invalid projection JSON: {e}"))?;
|
||||
let projection_doc = json_object_to_document(&json).map_err(|e| format!("Invalid projection: {e}"))?;
|
||||
find = find.projection(projection_doc);
|
||||
}
|
||||
}
|
||||
if let Some(s) = sort {
|
||||
if !s.trim().is_empty() {
|
||||
let json: serde_json::Value = serde_json::from_str(s).map_err(|e| format!("Invalid sort JSON: {e}"))?;
|
||||
@@ -844,6 +864,32 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_object_to_document_parses_find_projection() {
|
||||
let value = serde_json::json!({
|
||||
"title": 1,
|
||||
"_id": 0,
|
||||
});
|
||||
let doc = json_object_to_document(&value).unwrap();
|
||||
|
||||
assert!(matches!(doc.get("title"), Some(Bson::Int64(1))));
|
||||
assert!(matches!(doc.get("_id"), Some(Bson::Int64(0))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_version_from_build_info_reads_version_field() {
|
||||
let version = server_version_from_build_info(&doc! { "version": "4.4.29" }).unwrap();
|
||||
|
||||
assert_eq!(version, "4.4.29");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_version_from_build_info_rejects_missing_version() {
|
||||
let error = server_version_from_build_info(&doc! { "ok": 1 }).unwrap_err();
|
||||
|
||||
assert!(error.contains("MongoDB server version not found"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn json_object_to_document_parses_mongo_shell_isodate_strings() {
|
||||
let value = serde_json::json!({
|
||||
|
||||
@@ -123,6 +123,23 @@ pub async fn mongo_drop_collection_core(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn mongo_server_version_core(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
database: &str,
|
||||
) -> Result<String, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => mongo_driver::server_version(client, database).await,
|
||||
PoolKind::Agent(client) => {
|
||||
let mut client = client.lock().await;
|
||||
client.mongo_server_version(database).await
|
||||
}
|
||||
_ => Err("Not a MongoDB connection".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn document_find_documents_core(
|
||||
state: &AppState,
|
||||
@@ -132,13 +149,14 @@ pub async fn document_find_documents_core(
|
||||
skip: u64,
|
||||
limit: i64,
|
||||
filter: Option<&str>,
|
||||
projection: Option<&str>,
|
||||
sort: Option<&str>,
|
||||
) -> Result<MongoDocumentResult, String> {
|
||||
ensure_document_pool(state, connection_id).await?;
|
||||
let connections = state.connections.read().await;
|
||||
match connections.get(connection_id).ok_or("Not found")? {
|
||||
PoolKind::MongoDb(client) => {
|
||||
mongo_driver::find_documents(client, database, collection, skip, limit, filter, sort).await
|
||||
mongo_driver::find_documents(client, database, collection, skip, limit, filter, projection, sort).await
|
||||
}
|
||||
PoolKind::Elasticsearch(client) => {
|
||||
let client = client.clone();
|
||||
@@ -153,16 +171,18 @@ pub async fn document_find_documents_core(
|
||||
}
|
||||
PoolKind::Agent(client) => {
|
||||
let mut client = client.lock().await;
|
||||
client
|
||||
.mongo_find_documents(serde_json::json!({
|
||||
"database": database,
|
||||
"collection": collection,
|
||||
"skip": skip,
|
||||
"limit": limit,
|
||||
"filter": filter,
|
||||
"sort": sort,
|
||||
}))
|
||||
.await
|
||||
let mut params = serde_json::json!({
|
||||
"database": database,
|
||||
"collection": collection,
|
||||
"skip": skip,
|
||||
"limit": limit,
|
||||
"filter": filter,
|
||||
"sort": sort,
|
||||
});
|
||||
if let Some(projection) = projection {
|
||||
params["projection"] = serde_json::json!(projection);
|
||||
}
|
||||
client.mongo_find_documents(params).await
|
||||
}
|
||||
_ => Err("Not a MongoDB/Elasticsearch/vector connection".to_string()),
|
||||
}
|
||||
@@ -177,9 +197,11 @@ pub async fn mongo_find_documents_core(
|
||||
skip: u64,
|
||||
limit: i64,
|
||||
filter: Option<&str>,
|
||||
projection: Option<&str>,
|
||||
sort: Option<&str>,
|
||||
) -> Result<MongoDocumentResult, String> {
|
||||
document_find_documents_core(state, connection_id, database, collection, skip, limit, filter, sort).await
|
||||
document_find_documents_core(state, connection_id, database, collection, skip, limit, filter, projection, sort)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn mongo_aggregate_documents_core(
|
||||
|
||||
@@ -1967,6 +1967,7 @@ async fn find_mongo_documents_for_transfer(
|
||||
offset,
|
||||
batch_size as i64,
|
||||
None,
|
||||
None,
|
||||
Some(r#"{"_id":1}"#),
|
||||
)
|
||||
.await
|
||||
|
||||
Reference in new issue
Block a user