thalo

A high-performance event sourcing runtime utilizing WebAssembly an embedded event store

15 unstable releases (6 breaking)

0.8.0 Nov 21, 2023
0.7.0 Nov 10, 2023
0.5.0 Jan 20, 2022
0.4.0 Jan 1, 2022
0.1.3 Nov 28, 2021

#1199 in Database interfaces

Download history 15/week @ 2024-06-25 15/week @ 2024-07-02 2/week @ 2024-07-09 13/week @ 2024-07-16 95/week @ 2024-07-23 42/week @ 2024-07-30 2/week @ 2024-08-20 15/week @ 2024-08-27 3/week @ 2024-09-10 3/week @ 2024-09-17 45/week @ 2024-09-24 17/week @ 2024-10-01

68 downloads per month
Used in 8 crates

Apache-2.0 OR MIT

27KB
389 lines

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;
    }
}

Dependencies

~0.9–1.8MB
~38K SLoC