rskit_database/
repository.rs1use async_trait::async_trait;
4
5use rskit_errors::AppResult;
6
7#[async_trait]
12pub trait Repository<T, ID>: Send + Sync
13where
14 T: Send + Sync,
15 ID: Send + Sync,
16{
17 async fn find_by_id(&self, id: &ID) -> AppResult<Option<T>>;
19
20 async fn find_all(&self, opts: FindOpts) -> AppResult<Vec<T>>;
22
23 async fn find_first(&self, opts: FindOpts) -> AppResult<Option<T>>;
25
26 async fn count(&self, opts: FindOpts) -> AppResult<i64>;
28
29 async fn exists(&self, id: &ID) -> AppResult<bool>;
31
32 async fn create(&self, entity: &T) -> AppResult<T>;
34
35 async fn update(&self, entity: &T) -> AppResult<T>;
37
38 async fn delete(&self, id: &ID) -> AppResult<()>;
40
41 async fn upsert(&self, entity: &T) -> AppResult<T>;
43}
44
45#[derive(Debug, Default)]
47pub struct FindOpts {
48 pub limit: Option<i64>,
50 pub offset: Option<i64>,
52 pub order_by: Vec<String>,
54 pub filters: Vec<(String, serde_json::Value)>,
56}
57
58impl FindOpts {
59 #[must_use]
61 pub fn with_limit(mut self, n: i64) -> Self {
62 self.limit = Some(n);
63 self
64 }
65
66 #[must_use]
68 pub fn with_offset(mut self, n: i64) -> Self {
69 self.offset = Some(n);
70 self
71 }
72
73 #[must_use]
75 pub fn order_by(mut self, col: &str) -> Self {
76 self.order_by.push(col.to_owned());
77 self
78 }
79
80 #[must_use]
82 pub fn filter(mut self, col: &str, val: impl Into<serde_json::Value>) -> Self {
83 self.filters.push((col.to_owned(), val.into()));
84 self
85 }
86}