Skip to main content

gearbox_rs_postgres/
repository.rs

1use crate::{PgEntity, PgError};
2
3/// Repository trait providing CRUD operations for entities on PgClient.
4///
5/// This trait is implemented for `PgClient` automatically when using `#[derive(PgEntity)]`.
6/// Each entity type gets its own implementation, allowing a single `PgClient` to work
7/// with multiple entity types.
8///
9/// # Example
10///
11/// ```ignore
12/// use gearbox_rs_postgres::{PgClient, PgRepository};
13///
14/// let client: Arc<PgClient> = hub.get()?;
15///
16/// // Create
17/// let user = client.create(user).await?;
18///
19/// // Read
20/// let found = client.find_by_id::<User>(&id).await?;
21///
22/// // Update
23/// let updated = client.update(user).await?;
24///
25/// // Delete
26/// client.delete::<User>(&id).await?;
27/// ```
28#[gearbox_rs_core::async_trait]
29pub trait PgRepository<T: PgEntity>: Send + Sync {
30    async fn create(&self, entity: T) -> Result<T, PgError>;
31    async fn upsert(&self, entity: T) -> Result<T, PgError>;
32    async fn update(&self, entity: T) -> Result<T, PgError>;
33    async fn find_by_id(&self, id: &T::Id) -> Result<Option<T>, PgError>;
34    async fn find_by_ids(&self, ids: &[T::Id]) -> Result<Vec<T>, PgError>;
35    async fn find_page(&self, limit: i64, offset: i64) -> Result<Vec<T>, PgError>;
36    async fn exists(&self, id: &T::Id) -> Result<bool, PgError>;
37    async fn count(&self) -> Result<i64, PgError>;
38    async fn delete(&self, id: &T::Id) -> Result<bool, PgError>;
39    async fn delete_batch(&self, ids: &[T::Id]) -> Result<u64, PgError>;
40    async fn create_batch(&self, entities: Vec<T>) -> Result<Vec<T>, PgError>;
41    async fn upsert_batch(&self, entities: Vec<T>) -> Result<Vec<T>, PgError>;
42}