Crates.io | event-store-adapter-rs |
lib.rs | event-store-adapter-rs |
version | |
source | src |
created_at | 2023-09-01 08:31:48.679609 |
updated_at | 2024-10-25 00:15:27.627908 |
description | crate to make DynamoDB an Event Store |
homepage | |
repository | https://github.com/j5ik2o/event-store-adapter-rs |
max_upload_size | |
id | 960709 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
This library is designed to turn DynamoDB into an Event Store for CQRS/Event Sourcing.
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
See j5ik2o/cqrs-es-example-rs.
MIT License. See LICENSE for details.