Skip to content

Commit

Permalink
postgres store
Browse files Browse the repository at this point in the history
  • Loading branch information
renatillas committed Aug 4, 2024
1 parent 7076c8c commit a480c26
Show file tree
Hide file tree
Showing 11 changed files with 473 additions and 212 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/build
erl_crash.dump
src/main.gleam
/birdie_snapshots
98 changes: 96 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,107 @@
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/eventsourcing/)

```sh
gleam add eventsourcing@1
gleam add eventsourcing
```
```gleam
import eventsourcing
import eventsourcing/postgres_store
pub type BankAccount {
BankAccount(opened: Bool, balance: Float)
}
pub type BankAccountCommand {
OpenAccount(account_id: String)
DepositMoney(amount: Float)
WithDrawMoney(amount: Float)
}
pub type BankAccountEvent {
AccountOpened(account_id: String)
CustomerDepositedCash(amount: Float, balance: Float)
CustomerWithdrewCash(amount: Float, balance: Float)
}
pub fn event_type() {
"BankAccountEvent"
}
pub fn encode_event(event: BankAccountEvent) -> String {
// ...
todo
}
pub fn decode_event(
string: String,
) -> Result(BankAccountEvent, List(dynamic.DecodeError)) {
// ...
todo
}
pub fn handle(
bank_account: BankAccount,
command: BankAccountCommand,
) -> Result(List(BankAccountEvent), Nil) {
case command {
OpenAccount(account_id) -> Ok([AccountOpened(account_id)])
DepositMoney(amount) -> {
let balance = bank_account.balance +. amount
case amount >. 0.0 {
True -> Ok([CustomerDepositedCash(amount:, balance:)])
False -> Error(Nil)
}
}
WithDrawMoney(amount) -> {
let balance = bank_account.balance -. amount
case amount >. 0.0 {
True -> Ok([CustomerDepositedCash(amount:, balance:)])
False -> Error(Nil)
}
}
}
}
pub fn apply(bank_account: BankAccount, event: BankAccountEvent) {
case event {
AccountOpened(_) -> BankAccount(..bank_account, opened: True)
CustomerDepositedCash(_, balance) -> BankAccount(..bank_account, balance:)
CustomerWithdrewCash(_, balance) -> BankAccount(..bank_account, balance:)
}
}
pub fn aggregate_type() -> String {
"BankAccount"
}
//
pub fn main() {
// TODO: An example of the project in use
let postgres_store =
postgres_store.new(
pgo_config: pgo.Config(
..pgo.default_config(),
host: "localhost",
database: "postgres",
pool_size: 15,
password: option.Some("password"),
),
emtpy_entity: BankAccount(opened: False, balance: 0.0),
handle_command_function: handle,
apply_function: apply,
event_encoder: encode_event,
event_decoder: decode_event,
event_type: event_type(),
event_version: "1",
aggregate_type: aggregate_type(),
)
let event_sourcing = eventsourcing.new(postgres_store, [])
eventsourcing.execute(
event_sourcing,
"92085b42-032c-4d7a-84de-a86d67123858",
DepositMoney(10.0),
)
}
```

Expand Down
5 changes: 0 additions & 5 deletions birdie_snapshots/execute.accepted

This file was deleted.

10 changes: 10 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
postgres:
image: postgres:14-alpine
ports:
- 5432:5432
volumes:
- ~/apps/postgres:/var/lib/postgresql/data
environment:
- POSTGRES_USER=postgres
- POSTGRES_DB=postgres
15 changes: 6 additions & 9 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ version = "1.0.0"
# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
#
# description = ""
# licences = ["Apache-2.0"]
# repository = { type = "github", user = "", repo = "" }
# links = [{ title = "Website", href = "" }]
#
# For a full reference of all the available options, you can have a look at
# https://gleam.run/writing-gleam/gleam-toml/.
description = "CQRS and Event Soucing in Gleam!"
licences = ["Apache-2.0"]
repository = { type = "github", user = "renatillas", repo = "eventsoucing" }

[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
youid = ">= 1.2.0 and < 2.0.0"
gleam_otp = ">= 0.10.0 and < 1.0.0"
gleam_erlang = ">= 0.25.0 and < 1.0.0"
birl = ">= 1.7.1 and < 2.0.0"
gleam_pgo = ">= 0.13.0 and < 1.0.0"
gleam_json = ">= 1.0.1 and < 2.0.0"
decode = ">= 0.2.0 and < 1.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
Expand Down
16 changes: 10 additions & 6 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,40 @@

packages = [
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
{ name = "backoff", version = "1.1.6", build_tools = ["rebar3"], requirements = [], otp_app = "backoff", source = "hex", outer_checksum = "CF0CFFF8995FB20562F822E5CC47D8CCF664C5ECDC26A684CBE85C225F9D7C39" },
{ name = "birdie", version = "1.1.8", build_tools = ["gleam"], requirements = ["argv", "filepath", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_stdlib", "justin", "rank", "simplifile", "trie_again"], otp_app = "birdie", source = "hex", outer_checksum = "D225C0A3035FCD73A88402925A903AAD3567A1515C9EAE8364F11C17AD1805BB" },
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
{ name = "decode", version = "0.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "decode", source = "hex", outer_checksum = "965F517F67B8C172CA27A5C8E34C73733139E8C9E64736181B8C3179281F9793" },
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
{ name = "glam", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "66EC3BCD632E51EED029678F8DF419659C1E57B1A93D874C5131FE220DFAD2B2" },
{ name = "glance", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "8F3314D27773B7C3B9FB58D8C02C634290422CE531988C0394FA0DF8676B964D" },
{ name = "gleam_community_ansi", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "FE79E08BF97009729259B6357EC058315B6FBB916FAD1C2FF9355115FEB0D3A4" },
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
{ name = "gleam_erlang", version = "0.25.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "054D571A7092D2A9727B3E5D183B7507DAB0DA41556EC9133606F09C15497373" },
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
{ name = "gleam_otp", version = "0.10.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "0B04FE915ACECE539B317F9652CAADBBC0F000184D586AAAF2D94C100945D72B" },
{ name = "gleam_pgo", version = "0.13.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "pgo"], otp_app = "gleam_pgo", source = "hex", outer_checksum = "6A1E7F3E717C077788254871E4EF4A8DFF58FEC07D7FA7C7702C2CCF66095AC8" },
{ name = "gleam_stdlib", version = "0.39.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "2D7DE885A6EA7F1D5015D1698920C9BAF7241102836CE0C3837A4F160128A9C4" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "glexer", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "BD477AD657C2B637FEF75F2405FAEFFA533F277A74EF1A5E17B55B1178C228FB" },
{ name = "justin", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "justin", source = "hex", outer_checksum = "7FA0C6DB78640C6DC5FBFD59BF3456009F3F8B485BF6825E97E1EB44E9A1E2CD" },
{ name = "opentelemetry_api", version = "1.3.0", build_tools = ["rebar3", "mix"], requirements = ["opentelemetry_semantic_conventions"], otp_app = "opentelemetry_api", source = "hex", outer_checksum = "B9E5FF775FD064FA098DBA3C398490B77649A352B40B0B730A6B7DC0BDD68858" },
{ name = "opentelemetry_semantic_conventions", version = "0.2.0", build_tools = ["rebar3", "mix"], requirements = [], otp_app = "opentelemetry_semantic_conventions", source = "hex", outer_checksum = "D61FA1F5639EE8668D74B527E6806E0503EFC55A42DB7B5F39939D84C07D6895" },
{ name = "pg_types", version = "0.4.0", build_tools = ["rebar3"], requirements = [], otp_app = "pg_types", source = "hex", outer_checksum = "B02EFA785CAECECF9702C681C80A9CA12A39F9161A846CE17B01FB20AEEED7EB" },
{ name = "pgo", version = "0.14.0", build_tools = ["rebar3"], requirements = ["backoff", "opentelemetry_api", "pg_types"], otp_app = "pgo", source = "hex", outer_checksum = "71016C22599936E042DC0012EE4589D24C71427D266292F775EBF201D97DF9C9" },
{ name = "pprint", version = "1.0.3", build_tools = ["gleam"], requirements = ["glam", "gleam_stdlib"], otp_app = "pprint", source = "hex", outer_checksum = "76BBB92E23D12D954BD452686543F29EDE8EBEBB7FC0ACCBCA66EEF276EC3A06" },
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
{ name = "rank", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "rank", source = "hex", outer_checksum = "5660E361F0E49CBB714CC57CC4C89C63415D8986F05B2DA0C719D5642FAD91C9" },
{ name = "simplifile", version = "2.0.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "5FFEBD0CAB39BDD343C3E1CCA6438B2848847DC170BA2386DF9D7064F34DF000" },
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
{ name = "trie_again", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "trie_again", source = "hex", outer_checksum = "5B19176F52B1BD98831B57FDC97BD1F88C8A403D6D8C63471407E78598E27184" },
{ name = "youid", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_stdlib"], otp_app = "youid", source = "hex", outer_checksum = "EF0F693004E221155EE5909C6D3C945DD14F7117DBA882887CF5F45BE399B8CA" },
]

[requirements]
birdie = { version = ">= 1.1.8 and < 2.0.0" }
birl = { version = ">= 1.7.1 and < 2.0.0" }
decode = { version = ">= 0.2.0 and < 1.0.0" }
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
gleam_json = { version = ">= 1.0.1 and < 2.0.0" }
gleam_otp = { version = ">= 0.10.0 and < 1.0.0" }
gleam_pgo = { version = ">= 0.13.0 and < 1.0.0" }
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
pprint = { version = ">= 1.0.3 and < 2.0.0" }
youid = { version = ">= 1.2.0 and < 2.0.0" }
16 changes: 10 additions & 6 deletions src/eventsourcing.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import gleam/dict.{type Dict}
import gleam/list
import gleam/result

Expand Down Expand Up @@ -32,14 +31,13 @@ pub type EventStore(eventstore, entity, command, event, error) {
eventstore,
AggregateContext(entity, command, event, error),
List(event),
Dict(String, String),
) ->
List(EventEnvelop(event)),
)
}

pub type AggregateContext(entity, command, event, error) {
MemoryStoreAggregateContext(
AggregateContext(
aggregate_id: AggregateId,
aggregate: Aggregate(entity, command, event, error),
sequence: Int,
Expand Down Expand Up @@ -91,18 +89,24 @@ pub fn execute(
event_sourcing.event_store.eventstore,
aggregate_context,
events,
dict.new(),
)
event_sourcing.queries
|> list.map(fn(query) { query(aggregate_id, commited_events) })
Nil
}

pub type EventEnvelop(event) {
EventEnvelop(
MemoryStoreEventEnvelop(
aggregate_id: AggregateId,
sequence: Int,
payload: event,
metadata: Dict(String, String),
)
SerializedEventEnvelop(
aggregate_id: AggregateId,
sequence: Int,
payload: event,
event_type: String,
event_version: String,
aggregate_type: String,
)
}
16 changes: 4 additions & 12 deletions src/eventsourcing/memory_store.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -112,22 +112,16 @@ pub fn load_aggregate(
)
},
)
eventsourcing.MemoryStoreAggregateContext(
aggregate_id:,
aggregate:,
sequence:,
)
eventsourcing.AggregateContext(aggregate_id:, aggregate:, sequence:)
}

fn commit(
memory_store: MemoryStore(entity, command, event, error),
context: eventsourcing.AggregateContext(entity, command, event, error),
events: List(event),
metadata: Dict(String, String),
) {
let eventsourcing.MemoryStoreAggregateContext(aggregate_id, _, sequence) =
context
let wrapped_events = wrap_events(aggregate_id, sequence, events, metadata)
let eventsourcing.AggregateContext(aggregate_id, _, sequence) = context
let wrapped_events = wrap_events(aggregate_id, sequence, events)
let past_events = load_commited_events(memory_store, aggregate_id)
let events = list.append(wrapped_events, past_events)
io.println(
Expand All @@ -145,7 +139,6 @@ fn wrap_events(
aggregate_id: eventsourcing.AggregateId,
current_sequence: Int,
events: List(event),
base_metadata: Dict(String, String),
) -> List(eventsourcing.EventEnvelop(event)) {
list.map_fold(
over: events,
Expand All @@ -154,11 +147,10 @@ fn wrap_events(
let next_sequence = sequence + 1
#(
next_sequence,
eventsourcing.EventEnvelop(
eventsourcing.MemoryStoreEventEnvelop(
aggregate_id:,
sequence: sequence + 1,
payload: event,
metadata: base_metadata,
),
)
},
Expand Down
Loading

0 comments on commit a480c26

Please sign in to comment.