Skip to main content

pleme_database/
lib.rs

1//! # pleme-database
2//!
3//! Database utilities library for Pleme platform services.
4//!
5//! ## Features
6//!
7//! - **Connection Pooling** - Managed PostgreSQL connection pools
8//! - **Transactions** - Transactional operations with rollback
9//! - **Repository Pattern** - Clean data access layer abstraction
10//! - **Cache Integration** - Redis caching with automatic invalidation
11//!
12//! ## Usage
13//!
14//! ```rust,no_run
15//! use pleme_database::{DatabasePool, Repository};
16//!
17//! #[tokio::main]
18//! async fn main() -> anyhow::Result<()> {
19//!     let pool = DatabasePool::connect("postgres://localhost/mydb").await?;
20//!
21//!     // Use repository pattern
22//!     let users = UserRepository::new(pool);
23//!     let user = users.find_by_id("123").await?;
24//!
25//!     Ok(())
26//! }
27//! ```
28
29pub mod pool;
30pub mod transaction;
31pub mod repository;
32
33#[cfg(feature = "cache")]
34pub mod cache;
35
36pub use pool::{DatabasePool, PoolConfig};
37pub use transaction::{Transaction, Transactional};
38pub use repository::{
39    Repository, SoftDelete, ProductScoped, PaginatedRepository,
40    PaginationParams, PaginatedResponse, BaseRepository,
41};
42
43#[cfg(feature = "cache")]
44pub use cache::{CacheManager, CacheAside};
45
46use thiserror::Error;
47
48/// Database errors
49#[derive(Error, Debug)]
50pub enum DatabaseError {
51    #[error("Connection failed: {0}")]
52    ConnectionFailed(String),
53
54    #[error("Query failed: {0}")]
55    QueryFailed(String),
56
57    #[error("Transaction failed: {0}")]
58    TransactionFailed(String),
59
60    #[error("Not found: {0}")]
61    NotFound(String),
62
63    #[error("Constraint violation: {0}")]
64    ConstraintViolation(String),
65
66    #[cfg(feature = "cache")]
67    #[error("Cache error: {0}")]
68    CacheError(String),
69}
70
71/// Result type for database operations
72pub type Result<T> = std::result::Result<T, DatabaseError>;