rskit_database/
repository.rs1use async_trait::async_trait;
4
5use rskit_errors::AppResult;
6
7#[async_trait]
13pub trait Repository<T, ID>: Send + Sync
14where
15 T: Send + Sync,
16 ID: Send + Sync,
17{
18 async fn find_by_id(&self, id: &ID) -> AppResult<Option<T>>;
20
21 async fn find_all(&self, opts: FindOpts) -> AppResult<Vec<T>>;
23
24 async fn find_first(&self, opts: FindOpts) -> AppResult<Option<T>>;
26
27 async fn count(&self, opts: FindOpts) -> AppResult<i64>;
29
30 async fn exists(&self, id: &ID) -> AppResult<bool>;
32
33 async fn create(&self, entity: &T) -> AppResult<T>;
35
36 async fn update(&self, entity: &T) -> AppResult<T>;
38
39 async fn delete(&self, id: &ID) -> AppResult<()>;
41
42 async fn upsert(&self, entity: &T) -> AppResult<T>;
44}
45
46#[derive(Debug, Default)]
48pub struct FindOpts {
49 pub limit: Option<i64>,
51 pub offset: Option<i64>,
53 pub order_by: Vec<String>,
55 pub filters: Vec<(String, serde_json::Value)>,
57}
58
59impl FindOpts {
60 #[must_use]
62 pub fn with_limit(mut self, n: i64) -> Self {
63 self.limit = Some(n);
64 self
65 }
66
67 #[must_use]
69 pub fn with_offset(mut self, n: i64) -> Self {
70 self.offset = Some(n);
71 self
72 }
73
74 #[must_use]
76 pub fn order_by(mut self, col: &str) -> Self {
77 self.order_by.push(col.to_owned());
78 self
79 }
80
81 #[must_use]
83 pub fn filter(mut self, col: &str, val: impl Into<serde_json::Value>) -> Self {
84 self.filters.push((col.to_owned(), val.into()));
85 self
86 }
87}