Skip to main content

backbone_core/
repository.rs

1//! Repository traits and common implementations
2
3use async_trait::async_trait;
4use anyhow::Result;
5use uuid::Uuid;
6use std::collections::HashMap;
7
8// ─── Domain repository pagination types ──────────────────────────────────────
9
10/// Pagination parameters used by generated domain repository traits.
11///
12/// Generated files use a type alias:
13/// `pub type OrderPaginationParams = DomainPaginationParams;`
14#[derive(Debug, Clone, Default)]
15pub struct DomainPaginationParams {
16    pub page: u32,
17    pub per_page: u32,
18}
19
20impl DomainPaginationParams {
21    pub fn new(page: u32, per_page: u32) -> Self {
22        Self { page, per_page }
23    }
24
25    pub fn offset(&self) -> u64 {
26        ((self.page.saturating_sub(1)) * self.per_page) as u64
27    }
28
29    pub fn limit(&self) -> u64 {
30        self.per_page as u64
31    }
32}
33
34/// Paginated result returned by generated domain repository traits.
35///
36/// Generated files use a type alias:
37/// `pub type OrderPaginatedResult = DomainPaginatedResult<Order>;`
38#[derive(Debug, Clone)]
39pub struct DomainPaginatedResult<E> {
40    pub data: Vec<E>,
41    pub total: u64,
42    pub page: u32,
43    pub per_page: u32,
44}
45
46/// Generic repository trait with basic CRUD operations
47#[async_trait]
48pub trait Repository<T> {
49    async fn create(&self, entity: &T) -> Result<T>;
50    async fn find_by_id(&self, id: &Uuid) -> Result<Option<T>>;
51    async fn update(&self, entity: &T) -> Result<T>;
52    async fn delete(&self, id: &Uuid) -> Result<bool>;
53    async fn list(&self, page: u32, limit: u32) -> Result<Vec<T>>;
54}
55
56/// Searchable repository trait
57#[async_trait]
58pub trait SearchableRepository<T>: Repository<T> {
59    async fn search(&self, criteria: HashMap<String, String>, page: u32, limit: u32) -> Result<Vec<T>>;
60    async fn count(&self, criteria: HashMap<String, String>) -> Result<u64>;
61}
62
63/// Soft deletable repository trait
64#[async_trait]
65pub trait SoftDeletableRepository<T>: Repository<T> {
66    async fn soft_delete(&self, id: &Uuid) -> Result<bool>;
67    async fn restore(&self, id: &Uuid) -> Result<bool>;
68    async fn list_deleted(&self, page: u32, limit: u32) -> Result<Vec<T>>;
69    async fn permanent_delete_all(&self) -> Result<u64>;
70}
71
72/// Paginated repository trait
73#[async_trait]
74pub trait PaginatedRepository<T>: Repository<T> {
75    async fn paginate(&self, page: u32, limit: u32) -> Result<(Vec<T>, u64)>;
76}
77
78/// Bulk operations repository trait
79#[async_trait]
80pub trait BulkRepository<T>: Repository<T> {
81    async fn bulk_create(&self, entities: Vec<T>) -> Result<Vec<T>>;
82    async fn bulk_update(&self, ids: &[Uuid], updates: HashMap<String, String>) -> Result<usize>;
83    async fn bulk_delete(&self, ids: &[Uuid]) -> Result<u64>;
84}
85
86/// Combination trait for full CRUD functionality
87#[async_trait]
88pub trait CrudRepository<T>:
89    Repository<T> +
90    SearchableRepository<T> +
91    SoftDeletableRepository<T> +
92    PaginatedRepository<T> +
93    BulkRepository<T> +
94    Send + Sync
95{
96}