This commit is contained in:
JMARyA 2024-08-16 20:38:16 +02:00
parent 37cca40192
commit 228f113dbf
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
3 changed files with 21 additions and 24 deletions

2
Cargo.lock generated
View file

@ -698,7 +698,7 @@ dependencies = [
[[package]] [[package]]
name = "mongod" name = "mongod"
version = "0.2.0" version = "0.2.1"
dependencies = [ dependencies = [
"chrono", "chrono",
"futures", "futures",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "mongod" name = "mongod"
version = "0.2.0" version = "0.2.1"
edition = "2021" edition = "2021"
[features] [features]

View file

@ -8,7 +8,10 @@ use serde::de::DeserializeOwned;
use serde_json::{Map, Value}; use serde_json::{Map, Value};
use valid::Validate; use valid::Validate;
use crate::{cache_read, cache_write, col, collect_results, get_mongo, id_of}; #[cfg(feature = "cache")]
use crate::{cache_read, cache_write};
use crate::{col, collect_results, get_mongo, id_of};
pub mod historic; pub mod historic;
pub mod reference; pub mod reference;
@ -215,7 +218,7 @@ pub trait Model:
/// Get all `Model`s from the database /// Get all `Model`s from the database
#[must_use] #[must_use]
fn find_all() -> impl std::future::Future<Output = Option<Vec<Self>>> { fn find_all() -> impl std::future::Future<Output = Option<Vec<Self>>> {
Self::find(doc! {}, None) Self::find(doc! {}, None, None)
} }
/// Get all `Model`s partial from the database /// Get all `Model`s partial from the database
@ -223,7 +226,7 @@ pub trait Model:
fn find_all_partial( fn find_all_partial(
part: serde_json::Value, part: serde_json::Value,
) -> impl std::future::Future<Output = Option<Vec<Self::Partial>>> { ) -> impl std::future::Future<Output = Option<Vec<Self::Partial>>> {
Self::find_partial(doc! {}, part, None) Self::find_partial(doc! {}, part, None, None)
} }
/// Get multiple `Model`s by using a filter from the database. Pass a `limit` parameter to limit the amount of `Model`s returned. /// Get multiple `Model`s by using a filter from the database. Pass a `limit` parameter to limit the amount of `Model`s returned.
@ -231,17 +234,15 @@ pub trait Model:
fn find( fn find(
filter: mongodb::bson::Document, filter: mongodb::bson::Document,
limit: Option<i64>, limit: Option<i64>,
sort: Option<mongodb::bson::Document>,
) -> impl std::future::Future<Output = Option<Vec<Self>>> { ) -> impl std::future::Future<Output = Option<Vec<Self>>> {
async move { async move {
let db = get_mongo!(); let db = get_mongo!();
let collection = col!(db, Self::collection_name()); let collection = col!(db, Self::collection_name());
let mut results = collection
.find( let options = FindOptions::builder().limit(limit).sort(sort).build();
filter,
limit.map(|x| FindOptions::builder().limit(x).build()), let mut results = collection.find(filter, options).await.ok()?;
)
.await
.ok()?;
let docs = collect_results!(results); let docs = collect_results!(results);
docs.into_iter() docs.into_iter()
.map(|x| mongodb::bson::from_document(x).unwrap()) .map(|x| mongodb::bson::from_document(x).unwrap())
@ -255,6 +256,7 @@ pub trait Model:
filter: mongodb::bson::Document, filter: mongodb::bson::Document,
mut part: serde_json::Value, mut part: serde_json::Value,
limit: Option<i64>, limit: Option<i64>,
sort: Option<mongodb::bson::Document>,
) -> impl std::future::Future<Output = Option<Vec<Self::Partial>>> { ) -> impl std::future::Future<Output = Option<Vec<Self::Partial>>> {
async move { async move {
let db = get_mongo!(); let db = get_mongo!();
@ -262,18 +264,13 @@ pub trait Model:
part.as_object_mut()?.insert("_id".into(), 1.into()); part.as_object_mut()?.insert("_id".into(), 1.into());
let mut results = collection let options = FindOptions::builder()
.find(
filter,
Some(
FindOptions::builder()
.projection(Some(mongodb::bson::to_document(&part).unwrap()))
.limit(limit) .limit(limit)
.build(), .sort(sort)
), .projection(Some(mongodb::bson::to_document(&part).unwrap()))
) .build();
.await
.ok()?; let mut results = collection.find(filter, Some(options)).await.ok()?;
let docs = collect_results!(results); let docs = collect_results!(results);
docs.into_iter() docs.into_iter()
.map(|x| mongodb::bson::from_document(x).unwrap()) .map(|x| mongodb::bson::from_document(x).unwrap())