Skip to content

Commit

Permalink
Add input collection as an option for native queries (#46)
Browse files Browse the repository at this point in the history
* Add collection name as an option for native queries

* Reviews feedback, name change, explain handler

* Fix comment
  • Loading branch information
codedmart committed Apr 20, 2024
1 parent 2067659 commit 89ec531
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions crates/configuration/src/native_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{schema::ObjectField, serialized};
#[derive(Clone, Debug)]
pub struct NativeQuery {
pub representation: NativeQueryRepresentation,
pub input_collection: Option<String>,
pub arguments: BTreeMap<String, ObjectField>,
pub result_document_type: String,
pub pipeline: Vec<bson::Document>,
Expand All @@ -25,6 +26,7 @@ impl From<serialized::NativeQuery> for NativeQuery {
fn from(value: serialized::NativeQuery) -> Self {
NativeQuery {
representation: value.representation,
input_collection: value.input_collection,
arguments: value.arguments,
result_document_type: value.result_document_type,
pipeline: value.pipeline,
Expand Down
4 changes: 4 additions & 0 deletions crates/configuration/src/serialized/native_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub struct NativeQuery {
/// a "function" in your ddn configuration.
pub representation: NativeQueryRepresentation,

/// Use `input_collection` when you want to start an aggregation pipeline off of the specified
/// `input_collection` db.<input_collection>.aggregate.
pub input_collection: Option<String>,

/// Arguments to be supplied for each query invocation. These will be available to the given
/// pipeline as variables. For information about variables in MongoDB aggregation expressions
/// see https://www.mongodb.com/docs/manual/reference/aggregation-variables/
Expand Down
11 changes: 8 additions & 3 deletions crates/mongodb-agent-common/src/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ pub async fn explain_query(

let aggregate_target = match QueryTarget::for_request(config, &query_request) {
QueryTarget::Collection(collection_name) => Bson::String(collection_name),
// 1 means aggregation without a collection target - as in `db.aggregate()` instead of
// `db.<collection>.aggregate()`
QueryTarget::NativeQuery { .. } => Bson::Int32(1),
QueryTarget::NativeQuery { native_query, .. } => {
match &native_query.input_collection {
Some(collection_name) => Bson::String(collection_name.to_string()),
// 1 means aggregation without a collection target - as in `db.aggregate()` instead of
// `db.<collection>.aggregate()`
None => Bson::Int32(1)
}
}
};

let query_command = doc! {
Expand Down
10 changes: 8 additions & 2 deletions crates/mongodb-agent-common/src/query/execute_query_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ pub async fn execute_query_request(
let collection = database.collection(&collection_name);
collect_from_cursor(collection.aggregate(pipeline, None).await?).await
}
QueryTarget::NativeQuery { .. } => {
collect_from_cursor(database.aggregate(pipeline, None).await?).await
QueryTarget::NativeQuery { native_query, .. } => {
match &native_query.input_collection {
Some(collection_name) => {
let collection = database.collection(collection_name);
collect_from_cursor(collection.aggregate(pipeline, None).await?).await
},
None => collect_from_cursor(database.aggregate(pipeline, None).await?).await
}
}
}?;

Expand Down
1 change: 1 addition & 0 deletions crates/mongodb-agent-common/src/query/native_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ mod tests {
async fn executes_native_query() -> Result<(), anyhow::Error> {
let native_query = NativeQuery {
representation: NativeQueryRepresentation::Collection,
input_collection: None,
arguments: [
(
"filter".to_string(),
Expand Down

0 comments on commit 89ec531

Please sign in to comment.