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§
Sourceasync fn fetch_by_id(&self, id: &T::Key) -> Result<Option<T>>
async fn fetch_by_id(&self, id: &T::Key) -> Result<Option<T>>
Provided Methods§
Sourceasync fn fetch_by_ids(&self, ids: &[T::Key]) -> Result<Vec<Option<T>>>
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
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.