Skip to main content

BatchLoadMany

Trait BatchLoadMany 

Source
pub trait BatchLoadMany: EntityTrait + Sized
where Self::Model: Send + Sync + Clone,
{ 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§

Source

type ForeignKey: Clone + Eq + Hash + Send + Sync + 'static

The type of the foreign key used for grouping

Required Methods§

Source

fn extract_fk(model: &Self::Model) -> Self::ForeignKey

Extract the foreign key value from a model for grouping

Source

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,

Batch load multiple entities grouped by foreign key

Returns a HashMap where each key maps to a Vec of related entities.

§Example
let photos = AnimalPhoto::batch_load_many(animal_ids).await?;
let animal_photos = photos.get(&animal.id).unwrap_or(&vec![]);

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.

Implementors§