An event sourcing and CQRS astraction that aims to be practical more than "go by the book". It's built on top of Riker a lightweight actor framework.
WARNING It's pretty much alpha quality at the moment, features are still missing and the API is still changing.
This library adds a few extra event-sourcing related concepts to Riker, most notably the entity which represents a domain where external commands and bussiness logic is handled. Created with the Entity
actor using the usual Riker machinery(see creating actors).
rust
let entity = actor_system.actor_of_args::<Entity<MyEntity>, _>("my-entity", SomeArgs)`;
Suppose MyEntity
is a user supplied struct that implements the ES
trait.
```rust
struct MyEntity;
impl ES for MyEntity { type Args = SomeArgs; // the arguments passed to the constructor type Model = MyData; // The "model" is the data that is to be persisted along with its changes. type Cmd = MyEntityCommands; // The external command or commands(often in the form of an enum) this entity can handle. // type Event = (); // TODO: Similar to commands but for handling events emitted by other entities. type Error = MyEntityError; // Error produced by the handler functions.
// Used to construct an entity, receives the Entity
actor's context
// to be able to create other actors and hold their references
fn new(_cx: &Context
async fn handle_command(&mut self, cmd: Self::Cmd) -> Result
// when entities are created for the first time
Ok(Event::Create(MyData).into())
// or to update an existing entity
// Ok(Event::Change("some id".into(), MyDataUpdate).into())
} } ```
Define your aggregate(the data model), the updates it can handle and how to apply them.
```rust
struct MyData {
someid: String,
somefield: String,
other_field: Option
enum MyDataUpdate { TheChange(String, String), LittleChange(String), }
impl Model for MyData { type Change = MyDataUpdate;
fn id(&self) -> EntityId { self.some_id.into() }
fn applychange(&mut self, change: Self::Change) { match change { MyDataUpdate::TheChange(field, other) => { self.somefield = field; self.otherfield = Some(other); }, MyDataUpdate::LittleChange(field) => { self.somefield = field; }, } } } ```
RikerES provides an entity manager that will create and manage your entities
when they are register
ed. It provides a simpler API to send commands and queries to the right entity.
```rust
let mgr = Manager::new(actor_system).register::
let id = mgr.command(MyEntityCommands::DoSomething).await;
let data = mgr.query::
In the meantime(while I find a way to make it more automagical) you'll also have to
implement `EntityName` for both the entity and the command to be able to dispatch
commands to the right entity.
rust
impl EntityName for MyEntity {
const NAME: &'static str = "my-entity";
}
impl EntityName for MyEntityCommands {
const NAME: &'static str = "my-entity";
}
```