From b03681d9bd021ce98ecce97c32cb48dac710308b Mon Sep 17 00:00:00 2001 From: Miguel Piedrafita Date: Wed, 13 Dec 2023 04:14:57 +0100 Subject: [PATCH] add peek to relationships --- ensemble/src/relationships/belongs_to.rs | 4 ++++ ensemble/src/relationships/belongs_to_many.rs | 4 ++++ ensemble/src/relationships/has_many.rs | 4 ++++ ensemble/src/relationships/has_one.rs | 4 ++++ ensemble/src/relationships/mod.rs | 6 +++++- 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ensemble/src/relationships/belongs_to.rs b/ensemble/src/relationships/belongs_to.rs index 299bf8a..3d69fe2 100644 --- a/ensemble/src/relationships/belongs_to.rs +++ b/ensemble/src/relationships/belongs_to.rs @@ -79,6 +79,10 @@ impl Relationship for BelongsTo { Ok(self.relation.as_mut().unwrap()) } + fn peek(&self) -> Option<&Self::Value> { + self.relation.as_ref() + } + fn is_loaded(&self) -> bool { self.relation.is_loaded() } diff --git a/ensemble/src/relationships/belongs_to_many.rs b/ensemble/src/relationships/belongs_to_many.rs index 023d716..91480a1 100644 --- a/ensemble/src/relationships/belongs_to_many.rs +++ b/ensemble/src/relationships/belongs_to_many.rs @@ -89,6 +89,10 @@ impl Relationship for BelongsToMany Option<&Self::Value> { + self.relation.as_ref() + } + async fn get(&mut self) -> Result<&mut Self::Value, Error> { if self.relation.is_none() { let relation = self.query().get().await?; diff --git a/ensemble/src/relationships/has_many.rs b/ensemble/src/relationships/has_many.rs index 066a68e..0698f11 100644 --- a/ensemble/src/relationships/has_many.rs +++ b/ensemble/src/relationships/has_many.rs @@ -74,6 +74,10 @@ impl Relationship for HasMany { .where_not_null(&format!("{}.{}", Related::TABLE_NAME, self.foreign_key)) } + fn peek(&self) -> Option<&Self::Value> { + self.relation.as_ref() + } + /// Get the related models. /// /// # Errors diff --git a/ensemble/src/relationships/has_one.rs b/ensemble/src/relationships/has_one.rs index d970662..234006a 100644 --- a/ensemble/src/relationships/has_one.rs +++ b/ensemble/src/relationships/has_one.rs @@ -79,6 +79,10 @@ impl Relationship for HasOne { Ok(self.relation.as_mut().unwrap()) } + fn peek(&self) -> Option<&Self::Value> { + self.relation.as_ref() + } + fn is_loaded(&self) -> bool { self.relation.is_loaded() } diff --git a/ensemble/src/relationships/mod.rs b/ensemble/src/relationships/mod.rs index 4aed929..098e9c6 100644 --- a/ensemble/src/relationships/mod.rs +++ b/ensemble/src/relationships/mod.rs @@ -38,6 +38,10 @@ pub trait Relationship { /// Returns an error if the model cannot be retrieved, or if a connection to the database cannot be established. fn get(&mut self) -> impl Future> + Send; + /// Peek at the related model. + /// Returns `None` if the relationship has not been loaded, or if the relationship is empty. Returns `Some` if the relationship has been loaded. + fn peek(&self) -> Option<&Self::Value>; + /// Whether the relationship has been loaded. fn is_loaded(&self) -> bool; @@ -61,7 +65,7 @@ pub trait Relationship { fn build(value: Self::Key, related_key: Self::RelatedKey) -> Self; } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Status { Initial(Option), Fetched(Option),