cqrs_rust_lib/read/
storage.rs1use crate::read::Paged;
2use crate::{CqrsContext, CqrsError, MaybeSend, MaybeSync};
3use serde::de::DeserializeOwned;
4use serde::Serialize;
5use std::fmt::Debug;
6use std::sync::Arc;
7
8#[derive(Debug, thiserror::Error)]
15pub enum StorageError {
16 #[error("Missing parent id")]
17 MissingParentId,
18 #[error("Invalid parent id")]
19 InvalidParentId,
20 #[error("Unsupported method : {0}")]
21 UnsupportedMethod(String),
22}
23
24pub trait HasId {
25 fn field_id() -> &'static str;
26 fn id(&self) -> &str;
27 fn parent_field_id() -> Option<&'static str>;
28 fn parent_id(&self) -> Option<&str>;
29}
30
31#[cfg(not(target_arch = "wasm32"))]
32pub type DynStorage<V, Q> = Arc<dyn Storage<V, Q> + Send + Sync>;
33#[cfg(target_arch = "wasm32")]
34pub type DynStorage<V, Q> = Arc<dyn Storage<V, Q>>;
35
36cqrs_async_trait! {
37pub trait Storage<V, Q>
38where
39 V: Debug + Clone + Default + Serialize + DeserializeOwned + MaybeSend + MaybeSync,
40 Q: Clone + Debug + MaybeSend + MaybeSync,
41{
42 fn type_name(&self) -> &str;
43 async fn filter(
44 &self,
45 parent_id: Option<String>,
46 query: Q,
47 context: CqrsContext,
48 ) -> Result<Paged<V>, CqrsError>;
49
50 async fn find_by_id(
51 &self,
52 parent_id: Option<String>,
53 id: &str,
54 context: CqrsContext,
55 ) -> Result<Option<V>, CqrsError>;
56
57 async fn save(&self, entity: V, context: CqrsContext) -> Result<(), CqrsError>;
58}
59}