Skip to content

Commit

Permalink
add peek to relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Dec 13, 2023
1 parent f2952a5 commit b03681d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 1 deletion.
4 changes: 4 additions & 0 deletions ensemble/src/relationships/belongs_to.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ impl<Local: Model, Related: Model> Relationship for BelongsTo<Local, Related> {
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()
}
Expand Down
4 changes: 4 additions & 0 deletions ensemble/src/relationships/belongs_to_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl<Local: Model, Related: Model> Relationship for BelongsToMany<Local, Related
)
}

fn peek(&self) -> Option<&Self::Value> {
self.relation.as_ref()
}

async fn get(&mut self) -> Result<&mut Self::Value, Error> {

Check failure on line 96 in ensemble/src/relationships/belongs_to_many.rs

View workflow job for this annotation

GitHub Actions / Test Suite

functions in traits cannot be declared `async`
if self.relation.is_none() {
let relation = self.query().get().await?;
Expand Down
4 changes: 4 additions & 0 deletions ensemble/src/relationships/has_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl<Local: Model, Related: Model> Relationship for HasMany<Local, Related> {
.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
Expand Down
4 changes: 4 additions & 0 deletions ensemble/src/relationships/has_one.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ impl<Local: Model, Related: Model> Relationship for HasOne<Local, Related> {
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()
}
Expand Down
6 changes: 5 additions & 1 deletion ensemble/src/relationships/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Output = Result<&mut Self::Value, Error>> + Send;

Check failure on line 39 in ensemble/src/relationships/mod.rs

View workflow job for this annotation

GitHub Actions / Test Suite

`impl Trait` only allowed in function and inherent method return types, not in trait method return types

/// 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;

Expand All @@ -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<T> {
Initial(Option<T>),
Fetched(Option<T>),
Expand Down

0 comments on commit b03681d

Please sign in to comment.