ref validate performance

This commit is contained in:
JMARyA 2024-07-18 14:20:59 +02:00
parent 0d23326aa2
commit 0cb1e28c20
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -1,3 +1,4 @@
use mongodb::options::FindOneOptions;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::{col, get_mongo, id_of};
@ -30,6 +31,23 @@ impl Reference {
self.0.starts_with(col)
}
/// Check for existence of reference
pub async fn exists(&self) -> Option<bool> {
let (col, id) = self.0.split_once("::")?;
let db = get_mongo!();
let col = col!(db, col);
let res = col
.find_one(
id_of!(id),
FindOneOptions::builder()
.projection(mongodb::bson::doc! { "_id": 1})
.build(),
)
.await
.ok()?;
Some(res.is_some())
}
/// Get the raw `Document` behind the `Reference` from the database.
pub async fn get_document(&self) -> Option<mongodb::bson::Document> {
let (col, id) = self.0.split_once("::")?;
@ -50,7 +68,7 @@ impl Validate for Reference {
//self.0.split_once("::").is_some()
// right
self.get_document().await.is_some()
self.exists().await.unwrap_or(false)
}
}