update reference
This commit is contained in:
parent
280f3cf3a0
commit
c4f2b6eb81
1 changed files with 11 additions and 1 deletions
|
@ -3,7 +3,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{col, get_mongo, id_of};
|
use crate::{col, get_mongo, id_of};
|
||||||
|
|
||||||
use super::valid::Validate;
|
use super::{valid::Validate, Model};
|
||||||
|
|
||||||
/// A `Reference` to a `Model`
|
/// A `Reference` to a `Model`
|
||||||
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
|
@ -25,6 +25,11 @@ impl Reference {
|
||||||
Self::new_raw(&format!("{model}::{id}")).await
|
Self::new_raw(&format!("{model}::{id}")).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get just the ID of the referenced `Model`
|
||||||
|
pub fn id(&self) -> &str {
|
||||||
|
self.0.split_once("::").unwrap().1
|
||||||
|
}
|
||||||
|
|
||||||
/// Checks if a reference is part of a model collection.
|
/// Checks if a reference is part of a model collection.
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn is_of_collection(&self, col: &str) -> bool {
|
pub fn is_of_collection(&self, col: &str) -> bool {
|
||||||
|
@ -60,6 +65,11 @@ impl Reference {
|
||||||
pub async fn get<T: DeserializeOwned>(&self) -> T {
|
pub async fn get<T: DeserializeOwned>(&self) -> T {
|
||||||
mongodb::bson::from_document(self.get_document().await.unwrap()).unwrap()
|
mongodb::bson::from_document(self.get_document().await.unwrap()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a partial `Model` behind the `Reference` from the database.
|
||||||
|
pub async fn get_partial<T: Model>(&self, part: serde_json::Value) -> T::Partial {
|
||||||
|
T::get_partial(self.id(), part).await.unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Validate for Reference {
|
impl Validate for Reference {
|
||||||
|
|
Loading…
Reference in a new issue