event-store-adapter-rs

This library is designed to turn DynamoDB into an Event Store for 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 fn new(event_store: EventStore<UserAccount, UserAccountEvent>) -> Self {
Self { event_store }
}
pub async fn store(
&mut self,
event: &UserAccountEvent,
version: usize,
snapshot_opt: Option<&UserAccount>,
) -> Result<()> {
self
.event_store
.store_event_and_snapshot_opt(event, version, snapshot_opt)
.await
}
pub async fn find_by_id(&self, id: &UserAccountId) -> Result<UserAccount> {
let (snapshot, seq_nr, version) = self.event_store.get_latest_snapshot_by_id(id).await?;
let events = self.event_store.get_events_by_id_since_seq_nr(id, seq_nr).await?;
let result = UserAccount::replay(events, Some(snapshot), version);
Ok(result)
}
}
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);
let mut user_account = repository.find_by_id(user_account_id).await.unwrap();
let user_account_event = user_account.rename(name).unwrap();
repository
.store(&user_account_event, user_account.version(), None)
.await
Table Specifications
See docs/DATABASE_SCHEMA.md.