use crate::models::{MangoQuery, SortRule, UseIndex};
use serde_json::{Map, Value};
use std::collections::HashMap;
pub struct MangoQueryBuilder {
query: MangoQuery,
}
impl Default for MangoQueryBuilder {
fn default() -> Self {
Self::new()
}
}
impl MangoQueryBuilder {
pub fn new() -> Self {
Self {
query: MangoQuery {
selector: Value::Object(Map::new()),
..Default::default()
},
}
}
pub fn r#where(mut self, field: &str, val: Value) -> Self {
if let Value::Object(ref mut map) = self.query.selector {
map.insert(field.to_string(), val);
}
self
}
pub fn where_op(mut self, field: &str, op: &str, val: Value) -> Self {
if let Value::Object(ref mut map) = self.query.selector {
let entry = map
.entry(field.to_string())
.or_insert_with(|| Value::Object(Map::new()));
if let Value::Object(op_map) = entry {
op_map.insert(op.to_string(), val);
} else {
let mut new_op_map = Map::new();
new_op_map.insert(op.to_string(), val);
*entry = Value::Object(new_op_map);
}
}
self
}
pub fn and(mut self, selectors: Vec<Value>) -> Self {
if let Value::Object(ref mut map) = self.query.selector {
map.insert("$and".to_string(), Value::Array(selectors));
}
self
}
pub fn or(mut self, selectors: Vec<Value>) -> Self {
if let Value::Object(ref mut map) = self.query.selector {
map.insert("$or".to_string(), Value::Array(selectors));
}
self
}
pub fn nor(mut self, selectors: Vec<Value>) -> Self {
if let Value::Object(ref mut map) = self.query.selector {
map.insert("$nor".to_string(), Value::Array(selectors));
}
self
}
pub fn not(mut self, selector: Value) -> Self {
if let Value::Object(ref mut map) = self.query.selector {
map.insert("$not".to_string(), selector);
}
self
}
pub fn text(mut self, query_str: &str) -> Self {
if let Value::Object(ref mut map) = self.query.selector {
map.insert("$text".to_string(), Value::String(query_str.to_string()));
}
self
}
pub fn fields(mut self, fields: Vec<String>) -> Self {
self.query.fields = Some(fields);
self
}
pub fn sort(mut self, sort_rules: Vec<SortRule>) -> Self {
self.query.sort = Some(sort_rules);
self
}
pub fn sort_field(mut self, field: &str, direction: Option<&str>) -> Self {
let dir = direction.unwrap_or("asc").to_string();
let rule = if dir == "asc" {
SortRule::Field(field.to_string())
} else {
let mut map = HashMap::new();
map.insert(field.to_string(), dir);
SortRule::FieldDirection(map)
};
if let Some(ref mut existing_sort) = self.query.sort {
existing_sort.push(rule);
} else {
self.query.sort = Some(vec![rule]);
}
self
}
pub fn limit(mut self, limit: usize) -> Self {
self.query.limit = Some(limit);
self
}
pub fn skip(mut self, skip: usize) -> Self {
self.query.skip = Some(skip);
self
}
pub fn use_index(mut self, index: UseIndex) -> Self {
self.query.use_index = Some(index);
self
}
pub fn allow_fallback(mut self, allow: bool) -> Self {
self.query.allow_fallback = Some(allow);
self
}
pub fn conflicts(mut self, conflicts: bool) -> Self {
self.query.conflicts = Some(conflicts);
self
}
pub fn r(mut self, quorum: usize) -> Self {
self.query.r = Some(quorum);
self
}
pub fn bookmark(mut self, bookmark_str: &str) -> Self {
self.query.bookmark = Some(bookmark_str.to_string());
self
}
pub fn update(mut self, update: bool) -> Self {
self.query.update = Some(update);
self
}
pub fn stable(mut self, stable: bool) -> Self {
self.query.stable = Some(stable);
self
}
pub fn execution_stats(mut self, stats: bool) -> Self {
self.query.execution_stats = Some(stats);
self
}
pub fn build(self) -> MangoQuery {
self.query
}
}