backbone_core/persistence/mod.rs
1//! Persistence Layer - Reusable Repository Implementations
2//!
3//! This module provides generic, reusable repository implementations that can be
4//! used across all modules in the framework. Instead of writing custom repositories
5//! for each entity, modules can use these generic implementations.
6//!
7//! # Available Implementations
8//!
9//! - `InMemoryRepository` - Thread-safe in-memory storage (testing, prototyping)
10//! - `PostgresRepository` - PostgreSQL-backed storage (production, requires "postgres" feature)
11//!
12//! # Usage Pattern
13//!
14//! ## In-Memory (testing/prototyping)
15//!
16//! ```ignore
17//! use backbone_core::persistence::{InMemoryRepository, PersistentEntity, CrudRepository};
18//!
19//! #[derive(Clone, Debug, Serialize, Deserialize)]
20//! struct User {
21//! id: String,
22//! name: String,
23//! created_at: Option<DateTime<Utc>>,
24//! updated_at: Option<DateTime<Utc>>,
25//! deleted_at: Option<DateTime<Utc>>,
26//! }
27//!
28//! impl PersistentEntity for User { /* ... */ }
29//!
30//! let repo = InMemoryRepository::<User>::new();
31//! let user = repo.create(user).await?;
32//! ```
33//!
34//! ## PostgreSQL (production)
35//!
36//! ```ignore
37//! use backbone_core::persistence::{PostgresRepository, PostgresEntity, PersistentEntity};
38//! use sqlx::FromRow;
39//!
40//! #[derive(Clone, Debug, Serialize, Deserialize, FromRow)]
41//! struct User {
42//! id: String,
43//! name: String,
44//! created_at: Option<DateTime<Utc>>,
45//! updated_at: Option<DateTime<Utc>>,
46//! deleted_at: Option<DateTime<Utc>>,
47//! }
48//!
49//! impl PersistentEntity for User { /* ... */ }
50//!
51//! impl PostgresEntity for User {
52//! fn table_name() -> &'static str { "users" }
53//! fn select_columns() -> &'static [&'static str] {
54//! &["id", "name", "created_at", "updated_at", "deleted_at"]
55//! }
56//! fn insert_columns() -> &'static [&'static str] {
57//! &["id", "name", "created_at", "updated_at"]
58//! }
59//! fn bind_for_insert<'q>(entity: &Self, query: Query<'q, ...>) -> Query<'q, ...> {
60//! query.bind(&entity.id).bind(&entity.name)
61//! .bind(entity.created_at).bind(entity.updated_at)
62//! }
63//! }
64//!
65//! let pool = PgPool::connect("...").await?;
66//! let repo = PostgresRepository::<User>::new(pool);
67//! let user = repo.create(user).await?;
68//! ```
69
70pub mod adapter;
71pub mod memory;
72pub mod traits;
73
74#[cfg(feature = "postgres")]
75pub mod postgres;
76
77// Re-exports from traits
78pub use traits::{
79 CrudRepository, PartialUpdatable, PersistentEntity, RepositoryError, SearchableRepository,
80 Versioned,
81};
82
83// Re-export InMemoryRepository
84pub use memory::InMemoryRepository;
85
86// Re-export CrudServiceAdapter for bridging Repository to CrudService
87pub use adapter::{AdapterError, CrudServiceAdapter, SearchableCrudServiceAdapter, SimpleCrudServiceAdapter};
88
89// Re-export PostgreSQL types when feature is enabled
90#[cfg(feature = "postgres")]
91pub use postgres::{PostgresRepository, PostgresRepositoryBuilder};
92
93#[cfg(feature = "postgres")]
94pub use traits::PostgresEntity;