DataRepository

Trait DataRepository 

Source
pub trait DataRepository<T: CacheEntity>: Send + Sync {
    // Required method
    async fn fetch_by_id(&self, id: &T::Key) -> Result<Option<T>>;

    // Provided methods
    async fn fetch_by_ids(&self, ids: &[T::Key]) -> Result<Vec<Option<T>>> { ... }
    async fn count(&self) -> Result<u64> { ... }
    async fn fetch_all(&self) -> Result<Vec<T>> { ... }
}
Expand description

Trait for data repository implementations.

Abstracts database operations, decoupling cache from specific DB client. Implementations: SQLx, tokio-postgres, Diesel, custom ORM, in-memory, etc.

§Design for Testability

This trait is designed to be mockable. Implement it with your database client, or use InMemoryRepository provided in this module for testing.

Required Methods§

Source

async fn fetch_by_id(&self, id: &T::Key) -> Result<Option<T>>

Fetch entity by ID from primary data source.

Called when cache miss occurs or on explicit refresh.

§Returns
  • Ok(Some(entity)) - Entity found
  • Ok(None) - Entity not found (not an error)
  • Err(e) - Database error
§Errors

Returns Err if data source is unavailable or fetch fails

Provided Methods§

Source

async fn fetch_by_ids(&self, ids: &[T::Key]) -> Result<Vec<Option<T>>>

Batch fetch entities by IDs (optional optimization).

Default implementation calls fetch_by_id() for each key. Override for efficiency (e.g., SQL WHERE id IN (...))

§Errors

Returns Err if data source is unavailable or fetch fails

Source

async fn count(&self) -> Result<u64>

Count total entities (optional, for statistics).

§Errors

Returns Err if not implemented or if data source operation fails

Source

async fn fetch_all(&self) -> Result<Vec<T>>

Optional: Get all entities (use sparingly, potentially large result).

§Errors

Returns Err if not implemented or if data source operation fails

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§