Skip to main content

Module eager_loading

Module eager_loading 

Source
Expand description

Eager loading utilities for avoiding N+1 query problems

Provides batch loading of related entities to avoid the N+1 query problem.

§Example

use ferro_rs::database::BatchLoad;

// Load animals with their shelter in 2 queries instead of N+1
let animals = Animal::query().all().await?;
let shelters = Shelter::batch_load(animals.iter().map(|a| a.shelter_id)).await?;

// Access related data
for animal in &animals {
    if let Some(shelter) = shelters.get(&animal.shelter_id) {
        println!("{} is at {}", animal.name, shelter.name);
    }
}

Macros§

impl_batch_load
Macro to implement BatchLoad for an entity with a primary key
impl_batch_load_many
Macro to implement BatchLoadMany for has_many relationships

Traits§

BatchLoad
Trait for batch loading entities by their primary key
BatchLoadMany
Trait for loading has_many relationships in batch

Functions§

batch_load_by_id
Helper function to batch load entities by primary key
batch_load_has_many
Helper function to batch load has_many relations