Skip to content

Commit

Permalink
Added two helper functions for BackRefs
Browse files Browse the repository at this point in the history
  • Loading branch information
gammelalf committed Sep 2, 2023
1 parent 562361f commit 1805a84
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Since 0.5.1
- Select a foreign model as an entire patch using `as` keyword
- sealed traits are actually sealed now
- removed ouroboros from dependencies
- added `get_or_query` and `take_or_query` helper functions for `BackRef`

Notes for publishing
--------------------
Expand Down
50 changes: 50 additions & 0 deletions src/fields/back_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,56 @@ where
}
}

/// Returns a reference to the [`BackRef`]'s cache after populating it if not done already.
pub async fn get_or_query<'p, BRP>(
&self,
executor: impl Executor<'_>,
patch: &'p mut BRP,
) -> Result<&'p mut [FMF::Model], Error>
where
BRP: Patch<Model = BRF::Model>,
BRP: GetField<BRF>,
BRP: GetField<foreign_model::RF<FMF>>,
{
if <BRP as GetField<BRF>>::borrow_field_mut(patch)
.cached
.is_none()
{
self.populate(executor, patch).await?;
}
Ok(<BRP as GetField<BRF>>::borrow_field_mut(patch)
.cached
.as_mut()
.expect("The cache should have been populated"))
}

/// Takes the [`BackRef`]'s cache leaving it unpopulated again or just queries it.
///
/// This function is similar to [`get_or_query`](Self::get_or_query) but returns ownership
/// and therefore has to clear the cache.
pub async fn take_or_query<BRP>(
&self,
executor: impl Executor<'_>,
patch: &mut BRP,
) -> Result<Vec<FMF::Model>, Error>
where
BRP: Patch<Model = BRF::Model>,
BRP: GetField<BRF>,
BRP: GetField<foreign_model::RF<FMF>>,
{
if let Some(models) = <BRP as GetField<BRF>>::borrow_field_mut(patch)
.cached
.take()
{
Ok(models)
} else {
query!(executor, FMF::Model)
.condition(Self::model_as_condition(patch))
.all()
.await
}
}

/// Populate the [`BackRef`]'s cached field.
///
/// This method doesn't check whether it already has been populated.
Expand Down

0 comments on commit 1805a84

Please sign in to comment.