Packages

An opinionated event-sourcing framework for Elixir apps built from Event Modeling slices, on the FACT event store.

Current section

Files

Jump to
datumo README.md
Raw

README.md

# Datumo

An opinionated event-sourcing framework for Elixir apps built from
[Event Modeling](https://eventmodeling.org) slices, on the
[FACT](https://github.com/evntd/fact) event store.

> **Early.** Datumo is being extracted from two real apps. Until 1.0 the API
> can change between minor versions; every change that touches your slices is
> in the changelog.

*Datumo* is Esperanto for "a datum": one piece of data, one fact.

## What it is

Each slice of the board becomes a small module that Datumo runs:

- **Write slices** implement `Datumo.StateChange`: `query/1` names the past
  events the decision needs, `apply_event/2` folds them, `execute/2` decides.
  `Datumo.Decide` reads, folds, decides and appends with a
  [DCB](https://dcb.events) append condition: the append fails if anything the
  decision read changed in between, and the decision is retried.
- **Read slices** implement `Datumo.StateView`: the view is folded when it's
  read, from the events its query names. No tables to keep in sync.
- **Events** are structs that say their board type and tags:
  `@derive {Datumo.Event, type: "BookLent", tags: [:book_id, :member_id]}`.
- **Commands** are `embedded_schema`s (`use Datumo.Command`): `new/2` checks
  their shape with an Ecto changeset before anything is decided.
- **Tests are the board's scenarios**: `Datumo.Scenario` runs
  `given |> decide |> expect` through the slice's own query, so a query that
  misses an event fails the test instead of passing silently.

The opinions are deliberate: one store (FACT), DCB instead of aggregates,
folding on read, Ecto changesets for input. No database server.

## Installation

```elixir
def deps do
  [
    {:datumo, "~> 0.1"}
  ]
end
```

## A write slice

```elixir
defmodule Library.Slices.LendBook.Decider do
  use Datumo.StateChange

  alias Library.Slices.LendBook.{BookLent, LendBook}

  @impl true
  def query(%LendBook{book_id: book_id}) do
    Fact.QueryItem.types(["BookAdded", "BookLent"])
    |> Fact.QueryItem.tags(["book:#{book_id}"])
  end

  @impl true
  def initial_state, do: %{added: false, lent: false}

  @impl true
  def apply_event(state, %{"event_type" => "BookAdded"}), do: %{state | added: true}
  def apply_event(state, %{"event_type" => "BookLent"}), do: %{state | lent: true}

  @impl true
  def execute(%LendBook{} = cmd, state) do
    cond do
      not state.added -> {:error, :book_not_found}
      state.lent -> {:error, :book_already_lent}
      true -> {:ok, [%BookLent{loan_id: cmd.loan_id, book_id: cmd.book_id, member_id: cmd.member_id}]}
    end
  end
end

Datumo.Decide.execute(db, Library.Slices.LendBook.Decider, cmd)
```

A full app built this way:
[eventmodelers-elixir-fact-library](https://github.com/ortegacmanuel/eventmodelers-elixir-fact-library),
a lending library. It is built by the
[Elixir FACT build kit](https://github.com/ortegacmanuel/eventmodelers-elixir-fact-kit)
for [eventmodelers.ai](https://eventmodelers.ai).

## License

MIT