Skip to main content

shared/domain/ports/
database.rs

1use async_trait::async_trait;
2use serde::{Serialize, de::DeserializeOwned};
3
4use super::query::QueryBuilder;
5use crate::error::CoreError;
6
7#[async_trait]
8pub trait DatabaseAdapter<T: Send + Sync + Serialize + DeserializeOwned + 'static>:
9    Send + Sync
10{
11    // -- INSERTS
12    async fn insert(&self, data: T) -> Result<String, CoreError>;
13    async fn insert_many(&self, data: Vec<T>) -> Result<Vec<String>, CoreError>;
14    async fn upsert(&self, data: T) -> Result<String, CoreError>;
15    async fn upsert_many(&self, data: Vec<T>) -> Result<Vec<String>, CoreError>;
16
17    // -- FINDS
18    async fn find_one(&self, filter: QueryBuilder) -> Result<Option<T>, CoreError>;
19    async fn find_all(&self, filter: QueryBuilder) -> Result<Vec<T>, CoreError>;
20
21    // -- UPDATES
22    async fn find_one_and_update(
23        &self,
24        filter: QueryBuilder,
25        update: QueryBuilder,
26    ) -> Result<Option<T>, CoreError>;
27    async fn update_many(
28        &self,
29        filter: QueryBuilder,
30        update: QueryBuilder,
31    ) -> Result<(), CoreError>;
32
33    // -- DELETES
34    async fn delete_one(&self, filter: QueryBuilder) -> Result<(), CoreError>;
35    async fn delete_many(&self, filter: QueryBuilder) -> Result<(), CoreError>;
36
37    // transactions as a separate concern
38    // async fn begin_transaction(&self) -> Result<Box<dyn Transaction>, Error>;
39}
40
41// pub trait Transaction: Send + Sync {
42//     fn commit(self) -> impl std::future::Future<Output = Result<(), Error>> + Send;
43//     fn rollback(self) -> impl std::future::Future<Output = Result<(), Error>> + Send;
44// }
45
46// #[async_trait]
47// pub trait Transaction: Send + Sync {
48//     async fn commit(self) -> Result<(), Error>;
49//     async fn rollback(self) -> Result<(), Error>;
50// }