1#[cfg(feature = "postgres")]
2use sqlx::{Encode, Postgres, Type};
3#[cfg(feature = "mongo")]
4pub type MongoError = mongodb::error::Error;
5use thiserror::Error;
6
7#[derive(Error, Debug)]
8pub enum RepositoryError<DB> {
9 #[error("entity not found")]
10 NotFound,
11 #[error("database connection error: {0}")]
12 ConnectionError(DB),
13 #[error("constraint violation: {0}")]
14 ConstraintViolation(String),
15 #[error("unknown error: {0}")]
16 Other(String),
17}
18
19#[async_trait::async_trait]
20pub trait CrudRepository<T, ID, DB> {
21 async fn find_all(&self) -> Result<Vec<T>, RepositoryError<DB>>;
22 async fn find_one(&self, id: &ID) -> Result<Option<T>, RepositoryError<DB>>;
23 async fn insert_one(&self, entity: &T) -> Result<ID, RepositoryError<DB>>;
24 async fn update_one(&self, id: &ID, entity: &T) -> Result<Option<T>, RepositoryError<DB>>;
25 async fn delete_one(&self, id: &ID) -> Result<u64, RepositoryError<DB>>;
26}
27
28#[cfg(feature = "postgres")]
29pub trait PostgresEntity {
30 type Id;
31 fn columns() -> &'static [&'static str];
32 fn values<'e>(&'e self) -> Vec<&'e (impl Encode<'e, Postgres> + Type<Postgres>)>;
33}