#cqrs #pattern #context #adapter #command #query #bus

kti_cqrs_rs

Implementation of CQRS pattern in Rust

3 releases (breaking)

0.2.0 Mar 16, 2024
0.1.0 Mar 3, 2024
0.0.1 Nov 4, 2023

#15 in #cqrs

Download history 7/week @ 2024-01-05 35/week @ 2024-01-12 3/week @ 2024-01-19 14/week @ 2024-01-26 11/week @ 2024-02-02 2/week @ 2024-02-09 23/week @ 2024-02-16 73/week @ 2024-02-23 164/week @ 2024-03-01 32/week @ 2024-03-08 193/week @ 2024-03-15 57/week @ 2024-03-22 61/week @ 2024-03-29 24/week @ 2024-04-05 5/week @ 2024-04-12 4/week @ 2024-04-19

94 downloads per month
Used in kti_cqrs_provider_rs

MIT/Apache

5KB
62 lines

Implementation of CQRS pattern in Rust

Currently the crate contains only query & command handlers

Simple example (existed in repo)

#[cfg(test)]
mod tests {
  use std::sync::Arc;

  use kti_cqrs_rs::core::bus::{command_bus::CommandBus, query_bus::QueryBus};
  use tokio::sync::Mutex;

  use super::{
    adapters::{
      mutex_repository_adapter::MutexRepositoryAdapter, mutex_service_adapter::MutexServiceAdapter,
    },
    contexts::mutex_context::MutexContext,
    ports::mutex_service_port::MutexServicePort,
  };

  fn create_service() -> Box<dyn MutexServicePort> {
    let store = Arc::new(Mutex::new(vec![]));

    let query_repository = Box::new(MutexRepositoryAdapter::new(store.clone()));
    let command_repository = Box::new(MutexRepositoryAdapter::new(store));

    Box::new(MutexServiceAdapter::new(
      Arc::new(Mutex::new(MutexContext::new(
        query_repository,
        command_repository,
      ))),
      CommandBus,
      QueryBus,
    ))
  }

  #[tokio::test]
  async fn should_be_empty_vector() {
    let service = create_service();

    let count = service.get_count().await.unwrap();

    assert_eq!(count, 0);
  }

  #[tokio::test]
  async fn should_be_one_element() {
    let service = create_service();

    service.add_element(1).await.unwrap();

    let count = service.get_count().await.unwrap();

    assert_eq!(count, 1);
  }

  #[tokio::test]
  async fn should_be_empty_after_remove() {
    let service = create_service();

    service.add_element(1).await.unwrap();
    service.remove_element(1).await.unwrap();

    let count = service.get_count().await.unwrap();

    assert_eq!(count, 0);
  }

  #[tokio::test]
  async fn should_be_error_on_remove_not_existed_element() {
    let service = create_service();

    let res = service.remove_element(1).await;

    assert!(res.is_err());
  }
}

Dependencies

~2.7–9MB
~76K SLoC