ref validate performance
This commit is contained in:
parent
0d23326aa2
commit
0cb1e28c20
1 changed files with 19 additions and 1 deletions
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue