pub trait BatchLoadMany: EntityTrait + Sized{
type ForeignKey: Clone + Eq + Hash + Send + Sync + 'static;
// Required methods
fn extract_fk(model: &Self::Model) -> Self::ForeignKey;
fn batch_load_many<'async_trait, I>(
fk_values: I,
fk_column: Self::Column,
) -> Pin<Box<dyn Future<Output = Result<HashMap<Self::ForeignKey, Vec<Self::Model>>, FrameworkError>> + Send + 'async_trait>>
where I: IntoIterator<Item = Self::ForeignKey> + Send + 'async_trait,
I::IntoIter: Send,
Self::Column: ColumnTrait + Send + Sync,
Value: From<Self::ForeignKey>,
Self: 'async_trait;
}Expand description
Trait for loading has_many relationships in batch
Use this for one-to-many relationships where you want to load all related entities grouped by their foreign key.
§Example
ⓘ
// Load all photos for multiple animals in a single query
let animal_ids: Vec<_> = animals.iter().map(|a| a.id).collect();
let photos = AnimalPhoto::batch_load_many(animal_ids, Column::AnimalId).await?;
for animal in &animals {
let animal_photos = photos.get(&animal.id).unwrap_or(&vec![]);
println!("{} has {} photos", animal.name, animal_photos.len());
}Required Associated Types§
Required Methods§
Sourcefn extract_fk(model: &Self::Model) -> Self::ForeignKey
fn extract_fk(model: &Self::Model) -> Self::ForeignKey
Extract the foreign key value from a model for grouping
Sourcefn batch_load_many<'async_trait, I>(
fk_values: I,
fk_column: Self::Column,
) -> Pin<Box<dyn Future<Output = Result<HashMap<Self::ForeignKey, Vec<Self::Model>>, FrameworkError>> + Send + 'async_trait>>where
I: IntoIterator<Item = Self::ForeignKey> + Send + 'async_trait,
I::IntoIter: Send,
Self::Column: ColumnTrait + Send + Sync,
Value: From<Self::ForeignKey>,
Self: 'async_trait,
fn batch_load_many<'async_trait, I>(
fk_values: I,
fk_column: Self::Column,
) -> Pin<Box<dyn Future<Output = Result<HashMap<Self::ForeignKey, Vec<Self::Model>>, FrameworkError>> + Send + 'async_trait>>where
I: IntoIterator<Item = Self::ForeignKey> + Send + 'async_trait,
I::IntoIter: Send,
Self::Column: ColumnTrait + Send + Sync,
Value: From<Self::ForeignKey>,
Self: 'async_trait,
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.