Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nerdyworm/es
Event Stores and Streams for elixir
https://github.com/nerdyworm/es
Last synced: 2 months ago
JSON representation
Event Stores and Streams for elixir
- Host: GitHub
- URL: https://github.com/nerdyworm/es
- Owner: nerdyworm
- Created: 2017-04-18T01:41:34.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-12-15T02:29:18.000Z (about 7 years ago)
- Last Synced: 2024-02-13T09:10:40.956Z (11 months ago)
- Language: Elixir
- Size: 42 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-elixir-cqrs - ES - Event Sourcing for Ecto and Postgresl/Dynamodb events storage. (Libraries)
README
# ES
Event Sourcing for Ecto and Postgresl/Dynamodb events storage.
## Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `es` to your list of dependencies in `mix.exs`:```elixir
def deps do
[{:es, "~> 0.1.0"}]
end
```Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/es](https://hexdocs.pm/es).## Configuration
### Aggregates
```elixir
defmodule Person do
use ES.Aggregate
use Ecto.Schema
import Ecto.Changesetembedded_schema do
field :name, :string
field :version, :integer, default: 0
field :pending, {:array, :string}, default: [], virtual: true
field :changesets, {:array, :string}, default: [], virtual: true
enddefmodule Created do
use Ecto.Schema@primary_key false
embedded_schema do
field :name, :string
field :uuid, :string
enddef changeset(struct, params \\ %{}) do
struct
|> Ecto.Changeset.cast(params, [:name, :uuid])
|> Ecto.Changeset.validate_required([:name, :uuid])
end
enddef create(uuid, name) do
%Person{}
|> apply(%Created{name: name, uuid: uuid})
enddef handle_event(%Created{name: name, uuid: uuid}, workflow) do
workflow
|> change(%{name: name, id: uuid})
end
enddefmodule EventStore do
use ES.EventStore, adapter: ES.Storage.Memory, inline: true
end{:ok, pid} = EventStore.start_link
{:ok, aggregate} = Person.create(ES.uuid, "bob")
{:ok, aggregate} = EventStore.commit(aggregate)
{aggregate.name, aggregate.version}
```