use crate::error::Error;
use mongodb::{
bson::{doc, Document},
options::*,
ClientSession, Collection, IndexModel,
};
use serde::{Deserialize, Serialize};
use std::time::Duration;
pub async fn drop_index<D>(
collection: &Collection<D>,
name: &str,
options: Option<DropIndexOptions>,
session: Option<&mut ClientSession>,
) -> Result<(), Error>
where
D: Serialize + for<'a> Deserialize<'a> + Send + Sync,
{
if let Some(session) = session {
collection
.drop_index(name)
.with_options(options)
.session(session)
.await
.map_err(Error::Mongo)?;
} else {
collection
.drop_index(name)
.with_options(options)
.await
.map_err(Error::Mongo)?;
}
Ok(())
}
pub async fn apply_index<D>(
collection: &Collection<D>,
index_model: IndexModel,
options: Option<CreateIndexOptions>,
session: Option<&mut ClientSession>,
) -> Result<(), Error>
where
D: Serialize + for<'a> Deserialize<'a> + Send + Sync,
{
if let Some(session) = session {
collection
.create_index(index_model)
.with_options(options)
.session(session)
.await
.map_err(Error::Mongo)?;
} else {
collection
.create_index(index_model)
.with_options(options)
.await
.map_err(Error::Mongo)?;
}
Ok(())
}
pub fn build_ttl_index(secs: u64, field_name: &str, name: Option<&str>) -> IndexModel {
let name = name.map(|name| name.to_string());
let index_options = IndexOptions::builder()
.name(name)
.expire_after(Duration::new(secs, 0))
.build();
IndexModel::builder()
.keys(doc! { field_name: 1 })
.options(index_options)
.build()
}
pub fn build_blank_index(fields: Document, name: Option<&str>) -> IndexModel {
let name = name.map(|name| name.to_string());
let index_options = IndexOptions::builder().name(name).build();
IndexModel::builder()
.keys(fields)
.options(Some(index_options))
.build()
}
pub fn build_unique_index(fields: Document, name: Option<&str>) -> IndexModel {
let name = name.map(|name| name.to_string());
let index_options = IndexOptions::builder().unique(true).name(name).build();
IndexModel::builder()
.keys(fields)
.options(index_options)
.build()
}
#[cfg(test)]
mod tests {
use super::*;
use mongodb::bson::doc;
#[test]
fn test_build_ttl_index() {
build_ttl_index(1337, "created_at", Some("ttl_index"));
}
#[test]
fn test_build_blank_index() {
build_blank_index(
doc! {
"name": 1,
"year": 1,
},
None,
);
}
#[test]
fn test_build_unique_index() {
build_unique_index(
doc! {
"name": 1,
"year": 1,
},
Some("unique_index"),
);
}
}