Skip to main content

systemprompt_database/repository/
base.rs

1use async_trait::async_trait;
2use sqlx::PgPool;
3use std::sync::Arc;
4
5pub type PgDbPool = Arc<PgPool>;
6
7#[async_trait]
8pub trait Repository: Send + Sync {
9    type Entity: Send + Sync;
10    type Id: Send + Sync;
11    type Error: Send + Sync + std::error::Error;
12
13    fn pool(&self) -> &PgDbPool;
14
15    async fn find_by_id(&self, id: &Self::Id) -> Result<Option<Self::Entity>, Self::Error>;
16
17    async fn find_all(&self) -> Result<Vec<Self::Entity>, Self::Error>;
18
19    async fn insert(&self, entity: &Self::Entity) -> Result<Self::Id, Self::Error>;
20
21    async fn update(&self, entity: &Self::Entity) -> Result<(), Self::Error>;
22
23    async fn delete(&self, id: &Self::Id) -> Result<(), Self::Error>;
24
25    async fn exists(&self, id: &Self::Id) -> Result<bool, Self::Error> {
26        Ok(self.find_by_id(id).await?.is_some())
27    }
28
29    async fn count(&self) -> Result<i64, Self::Error>;
30}
31
32#[async_trait]
33pub trait PaginatedRepository: Repository {
34    async fn find_paginated(
35        &self,
36        limit: i64,
37        offset: i64,
38    ) -> Result<Vec<Self::Entity>, Self::Error>;
39}