use mongodb::bson::{Bson, DateTime, Document, doc};
pub struct UpdateBuilder {
set_doc: Document,
set_on_insert_doc: Document,
unset_doc: Document,
inc_doc: Document,
mul_doc: Document,
min_doc: Document,
max_doc: Document,
push_doc: Document,
pull_doc: Document,
add_to_set_doc: Document,
pop_doc: Document,
rename_doc: Document,
current_date_doc: Document,
}
impl UpdateBuilder {
pub fn new() -> Self {
Self {
set_doc: doc! {},
set_on_insert_doc: doc! {},
unset_doc: doc! {},
inc_doc: doc! {},
mul_doc: doc! {},
min_doc: doc! {},
max_doc: doc! {},
push_doc: doc! {},
pull_doc: doc! {},
add_to_set_doc: doc! {},
pop_doc: doc! {},
rename_doc: doc! {},
current_date_doc: doc! {},
}
}
pub fn set_if_some(mut self, field: &str, value: Option<impl Into<Bson>>) -> Self {
if let Some(v) = value {
self.set_doc.insert(field, v.into());
}
self
}
pub fn set(mut self, field: &str, value: impl Into<Bson>) -> Self {
self.set_doc.insert(field, value.into());
self
}
pub fn unset(mut self, field: &str) -> Self {
self.unset_doc.insert(field, "");
self
}
pub fn with_timestamp(mut self) -> Self {
self.set_doc.insert("updated_at", DateTime::now());
self
}
pub fn set_on_insert(mut self, field: &str, value: impl Into<Bson>) -> Self {
self.set_on_insert_doc.insert(field, value.into());
self
}
pub fn set_on_insert_if_some(mut self, field: &str, value: Option<impl Into<Bson>>) -> Self {
if let Some(v) = value {
self.set_on_insert_doc.insert(field, v.into());
}
self
}
pub fn set_on_insert_many(mut self, fields: Document) -> Self {
for (key, value) in fields {
self.set_on_insert_doc.insert(key, value);
}
self
}
pub fn inc(mut self, field: &str, amount: impl Into<Bson>) -> Self {
self.inc_doc.insert(field, amount.into());
self
}
pub fn mul(mut self, field: &str, multiplier: impl Into<Bson>) -> Self {
self.mul_doc.insert(field, multiplier.into());
self
}
pub fn min(mut self, field: &str, value: impl Into<Bson>) -> Self {
self.min_doc.insert(field, value.into());
self
}
pub fn max(mut self, field: &str, value: impl Into<Bson>) -> Self {
self.max_doc.insert(field, value.into());
self
}
pub fn push(mut self, field: &str, value: impl Into<Bson>) -> Self {
self.push_doc.insert(field, value.into());
self
}
pub fn push_each<T: Into<Bson>>(mut self, field: &str, values: Vec<T>) -> Self {
let bson_values: Vec<Bson> = values.into_iter().map(|v| v.into()).collect();
self.push_doc.insert(field, doc! { "$each": bson_values });
self
}
pub fn pull(mut self, field: &str, condition: impl Into<Bson>) -> Self {
self.pull_doc.insert(field, condition.into());
self
}
pub fn add_to_set(mut self, field: &str, value: impl Into<Bson>) -> Self {
self.add_to_set_doc.insert(field, value.into());
self
}
pub fn add_to_set_each<T: Into<Bson>>(mut self, field: &str, values: Vec<T>) -> Self {
let bson_values: Vec<Bson> = values.into_iter().map(|v| v.into()).collect();
self.add_to_set_doc
.insert(field, doc! { "$each": bson_values });
self
}
pub fn pop_first(mut self, field: &str) -> Self {
self.pop_doc.insert(field, -1);
self
}
pub fn pop_last(mut self, field: &str) -> Self {
self.pop_doc.insert(field, 1);
self
}
pub fn rename(mut self, old_field: &str, new_field: &str) -> Self {
self.rename_doc.insert(old_field, new_field);
self
}
pub fn set_current_date(mut self, field: &str) -> Self {
self.current_date_doc.insert(field, true);
self
}
pub fn set_current_timestamp(mut self, field: &str) -> Self {
self.current_date_doc
.insert(field, doc! { "$type": "timestamp" });
self
}
pub fn set_many(mut self, fields: Document) -> Self {
for (key, value) in fields {
self.set_doc.insert(key, value);
}
self
}
pub fn unset_many(mut self, fields: Vec<&str>) -> Self {
for field in fields {
self.unset_doc.insert(field, "");
}
self
}
pub fn build(self) -> Document {
let mut update = doc! {};
if !self.set_doc.is_empty() {
update.insert("$set", self.set_doc);
}
if !self.set_on_insert_doc.is_empty() {
update.insert("$setOnInsert", self.set_on_insert_doc);
}
if !self.unset_doc.is_empty() {
update.insert("$unset", self.unset_doc);
}
if !self.inc_doc.is_empty() {
update.insert("$inc", self.inc_doc);
}
if !self.mul_doc.is_empty() {
update.insert("$mul", self.mul_doc);
}
if !self.min_doc.is_empty() {
update.insert("$min", self.min_doc);
}
if !self.max_doc.is_empty() {
update.insert("$max", self.max_doc);
}
if !self.push_doc.is_empty() {
update.insert("$push", self.push_doc);
}
if !self.pull_doc.is_empty() {
update.insert("$pull", self.pull_doc);
}
if !self.add_to_set_doc.is_empty() {
update.insert("$addToSet", self.add_to_set_doc);
}
if !self.pop_doc.is_empty() {
update.insert("$pop", self.pop_doc);
}
if !self.rename_doc.is_empty() {
update.insert("$rename", self.rename_doc);
}
if !self.current_date_doc.is_empty() {
update.insert("$currentDate", self.current_date_doc);
}
update
}
}
impl Default for UpdateBuilder {
fn default() -> Self {
Self::new()
}
}