#event-sourcing #cqrs #ddd

event-store-adapter-rs

crate to make DynamoDB an Event Store

106 releases (57 stable)

1.1.41 Oct 25, 2024
1.1.39 Sep 26, 2024
1.1.37 May 30, 2024
1.1.19 Mar 29, 2024
0.2.28 Nov 25, 2023

#196 in Web programming

Download history 183/week @ 2024-07-18 477/week @ 2024-07-25 189/week @ 2024-08-01 41/week @ 2024-08-08 42/week @ 2024-08-15 42/week @ 2024-08-22 239/week @ 2024-08-29 38/week @ 2024-09-05 52/week @ 2024-09-12 93/week @ 2024-09-19 251/week @ 2024-09-26 225/week @ 2024-10-03 98/week @ 2024-10-10 41/week @ 2024-10-17 181/week @ 2024-10-24 51/week @ 2024-10-31

556 downloads per month

MIT/Apache

49KB
1K SLoC

event-store-adapter-rs

Workflow Status crates.io docs.rs Renovate License

This library is designed to turn DynamoDB into an Event Store for CQRS/Event Sourcing.

日本語

Usage

You can easily implement an Event Sourcing-enabled repository using EventStore.

pub struct UserAccountRepository {
  event_store: EventStore<UserAccount, UserAccountEvent>,
}

impl UserAccountRepository {
  pub async fn store_event(&mut self, event: &UserAccountEvent, version: usize) -> Result<(), RepositoryError> {
    let result = self.event_store.persist_event(event, version).await;
    match result {
      Ok(_) => Ok(()),
      Err(err) => Err(Self::handle_event_store_write_error(err)),
    }
  }

  pub async fn store_event_and_snapshot(
    &mut self,
    event: &UserAccountEvent,
    snapshot: &UserAccount,
  ) -> Result<(), RepositoryError> {
    let result = self.event_store.persist_event_and_snapshot(event, snapshot).await;
    match result {
      Ok(_) => Ok(()),
      Err(err) => Err(Self::handle_event_store_write_error(err)),
    }
  }

  pub async fn find_by_id(&self, id: &UserAccountId) -> Result<Option<UserAccount>, RepositoryError> {
    let snapshot_result = self.event_store.get_latest_snapshot_by_id(id).await;
    match snapshot_result {
      Ok(snapshot_opt) => match snapshot_opt {
        Some(snapshot) => {
          let events = self
            .event_store
            .get_events_by_id_since_seq_nr(id, snapshot.seq_nr() + 1)
            .await;
          match events {
            Ok(events) => Ok(Some(UserAccount::replay(events, snapshot))),
            Err(err) => Err(Self::handle_event_store_read_error(err)),
          }
        }
        None => Ok(None),
      },
      Err(err) => Err(Self::handle_event_store_read_error(err)),
    }
  }
    
}

The following is an example of the repository usage.

let event_store = EventStore::new(
  aws_dynamodb_client.clone(),
  journal_table_name.to_string(),
  journal_aid_index_name.to_string(),
  snapshot_table_name.to_string(),
  snapshot_aid_index_name.to_string(),
  64,
);
 
let mut repository = UserAccountRepository::new(event_store);
 
// Replay the aggregate from the event store
let mut user_account = repository.find_by_id(user_account_id).await.unwrap();

// Execute a command on the aggregate
let user_account_event = user_account.rename(name).unwrap();
 
// Store the new event without a snapshot
repository
  .store_event(&user_account_event, user_account.version())
  .await
// Store the new event with a snapshot
//  repository
//  .store_event_and_snapshot(&user_account_event, &user_account)
//  .await

Table Specifications

See docs/DATABASE_SCHEMA.md.

CQRS/Event Sourcing Example

See j5ik2o/cqrs-es-example-rs.

License.

MIT License. See LICENSE for details.

Dependencies

~19–27MB
~358K SLoC