Thanks to visit codestin.com
Credit goes to docs.rs

Crate thalo

Crate thalo 

Source
Expand description

Thalo is an event sourcing runtime that leverages the power of WebAssembly (wasm) through wasmtime, combined with sled as an embedded event store. It is designed to handle commands using compiled aggregate wasm components and to persist the resulting events, efficiently managing the rebuilding of aggregate states from previous events.

§Counter Aggregate Example

This example shows a basic counter aggregate, allowing the count to be incremented by an amount.

use serde::{Deserialize, Serialize};
use thalo::{events, export_aggregate, Aggregate, Apply, Command, Event, Handle};

export_aggregate!(Counter);

pub struct Counter {
    count: u64,
}

impl Aggregate for Counter {
    type Command = CounterCommand;
    type Event = CounterEvent;

    fn init(_id: String) -> Self {
        Counter { count: 0 }
    }
}

#[derive(Command, Deserialize)]
pub enum CounterCommand {
    Increment { amount: u64 },
}

impl Handle<CounterCommand> for Counter {
    type Error = Infallible;

    fn handle(&self, cmd: CounterCommand) -> Result<Vec<CounterEvent>, Self::Error> {
        match cmd {
            CounterCommand::Increment { amount } => events![Incremented { amount }],
        }
    }
}

#[derive(Event, Serialize, Deserialize)]
pub enum CounterEvent {
    Incremented(Incremented),
}

#[derive(Serialize, Deserialize)]
pub struct Incremented {
    pub amount: u64,
}

impl Apply<Incremented> for Counter {
    fn apply(&mut self, event: Incremented) {
        self.count += event.amount;
    }
}

Modules§

stream_name
Stream names are strings containing a category, and optional entity identifier.

Macros§

events
Shorthand for creating a Ok(vec![...]) in Handle implmentations.
export_aggregate
Exports an aggregate for use with the thalo runtime.

Traits§

Aggregate
Represents an aggregate root in an event-sourced system.
Apply
Applies an event, updating the aggregate state.
Handle
Handles a command, returning events.

Derive Macros§

Command
Used to implement traits for an aggregate command enum.
Event
Used to implement traits for an aggregate event enum.