cqrs-rust-lib 0.4.0

An opinionated implementation of CQRS/Event Sourcing with pluggable storage backends (InMemory, PostgreSQL, MongoDB)
Documentation
use crate::read::Paged;
use crate::{CqrsContext, CqrsError};
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fmt::Debug;
use std::sync::Arc;

#[derive(Debug, thiserror::Error)]
pub enum StorageError {
    #[error("Missing parent id")]
    MissingParentId,
    #[error("Invalid parent id")]
    InvalidParentId,
    #[error("Unsupported method : {0}")]
    UnsupportedMethod(String),
}

pub trait HasId {
    fn field_id() -> &'static str;
    fn id(&self) -> &str;
    fn parent_field_id() -> Option<&'static str>;
    fn parent_id(&self) -> Option<&str>;
}

pub type DynStorage<V, Q> = Arc<dyn Storage<V, Q> + Send + Sync>;
#[async_trait::async_trait]
pub trait Storage<V, Q>
where
    V: Debug + Clone + Default + Serialize + DeserializeOwned + Send + Sync,
    Q: Clone + Debug + DeserializeOwned + Send + Sync,
{
    fn type_name(&self) -> &str;
    async fn filter(
        &self,
        parent_id: Option<String>,
        query: Q,
        context: CqrsContext,
    ) -> Result<Paged<V>, CqrsError>;

    async fn find_by_id(
        &self,
        parent_id: Option<String>,
        id: &str,
        context: CqrsContext,
    ) -> Result<Option<V>, CqrsError>;

    async fn save(&self, entity: V, context: CqrsContext) -> Result<(), CqrsError>;
}