Skip to main content

ferrox_database_core/
lib.rs

1//! # Ferrox Database Core (`ferrox-database-core`)
2//!
3//! `ferrox-database-core` defines the abstract persistence layer for Ferrox applications, introducing the generic `Repository<T, Id>` trait.
4//!
5//! ## Inversion of Control & Persistence Decoupling
6//! By depending on `Repository<T, Id>` traits rather than concrete database drivers (SQL, Mongo, Redis), domain logic and services stay
7//! completely decoupled from database engine details. This enables seamless unit testing via in-memory mock repositories.
8//!
9//! ## Key Features
10//! - 📥 **Generic `Repository<Entity, Id>` Trait**: Standard `find_by_id`, `find_all`, `save`, `update`, and `delete` methods.
11//! - 🧪 **Testing Mocks**: Simplify unit tests without launching database containers.
12
13use async_trait::async_trait;
14use ferrox_errors::AppError;
15
16/// Core Repository Trait
17/// This is the equivalent of the base repository in TypeORM/NestJS.
18/// Implementations (like SeaORM or Mongo) will implement this trait for specific entities.
19#[async_trait]
20pub trait Repository<Entity, Id> {
21    /// Finds a single entity by its primary key
22    async fn find_by_id(&self, id: Id) -> Result<Option<Entity>, AppError>;
23    
24    /// Finds all entities
25    async fn find_all(&self) -> Result<Vec<Entity>, AppError>;
26    
27    /// Inserts a new entity
28    async fn insert(&self, entity: Entity) -> Result<Entity, AppError>;
29    
30    /// Updates an existing entity
31    async fn update(&self, id: Id, entity: Entity) -> Result<Entity, AppError>;
32    
33    /// Deletes an entity by its primary key
34    async fn delete(&self, id: Id) -> Result<(), AppError>;
35}
36
37pub fn setup() {
38    println!("ferrox-database-core initialized: Provides Repository traits.");
39}