Skip to content

Commit

Permalink
Feat: implement scoping on the current scope for the event store oper…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
mindreframer committed Aug 24, 2024
1 parent d4c5799 commit 163c3b6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 7 deletions.
11 changes: 11 additions & 0 deletions lib/event_store/base_query.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule EventStore.BaseQuery do
alias Essig.Schemas.Event
use Essig.Repo

def query() do
scope_uuid = Essig.Context.current_scope()

from(event in Event)
|> where([event], event.scope_uuid == ^scope_uuid)
end
end
2 changes: 1 addition & 1 deletion lib/event_store/read_all_stream_backward.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Essig.EventStore.ReadAllStreamBackward do
end

def query(from_id, amount) do
from(event in Event)
EventStore.BaseQuery.query()
|> where([event], event.id < ^from_id)
|> order_by(desc: :id)
|> limit(^amount)
Expand Down
3 changes: 1 addition & 2 deletions lib/event_store/read_all_stream_forward.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
defmodule Essig.EventStore.ReadAllStreamForward do
alias Essig.Schemas.Event
use Essig.Repo

def run(from_id, amount) do
query(from_id, amount) |> Repo.all()
end

def query(from_id, amount) do
from(event in Event)
EventStore.BaseQuery.query()
|> where([event], event.id > ^from_id)
|> order_by(asc: :id)
|> limit(^amount)
Expand Down
3 changes: 1 addition & 2 deletions lib/event_store/read_stream_backward.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
defmodule Essig.EventStore.ReadStreamBackward do
use Essig.Repo
alias Essig.Schemas.Event

def run(stream_uuid, from_seq, amount) do
query(stream_uuid, from_seq, amount) |> Repo.all()
end

def query(stream_uuid, from_seq, amount) do
from(Event)
EventStore.BaseQuery.query()
|> where([event], event.stream_uuid == ^stream_uuid)
|> where([event], event.seq < ^from_seq)
|> order_by(desc: :seq)
Expand Down
3 changes: 1 addition & 2 deletions lib/event_store/read_stream_forward.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
defmodule Essig.EventStore.ReadStreamForward do
use Essig.Repo
alias Essig.Schemas.Event

def run(stream_uuid, from_seq, amount) do
query(stream_uuid, from_seq, amount) |> Repo.all()
end

def query(stream_uuid, from_seq, amount) do
from(Event)
EventStore.BaseQuery.query()
|> where([event], event.stream_uuid == ^stream_uuid)
|> where([event], event.seq > ^from_seq)
|> order_by(asc: :id)
Expand Down
31 changes: 31 additions & 0 deletions lib/event_store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,37 @@ defmodule Essig.EventStoreTest do
end
end

describe "respects the current scope" do
test "when the scope is switched, the events from other scopes are not accessible and the seq order is correct!" do
scope1 = Essig.Context.current_scope()

Essig.Server.start_scope(scope1)
uuid1 = Essig.Ecto.UUID7.generate()
init_stream(uuid1, 0)

scope2 = Essig.Ecto.UUID7.generate()
Essig.Server.start_scope(scope2)
uuid2 = Essig.Ecto.UUID7.generate()
init_stream(uuid2, 0)

# switch to scope1
Essig.Server.start_scope(scope1)
events1 = Essig.EventStore.read_all_stream_forward(0, 3)

# switch to scope2
Essig.Server.start_scope(scope2)
events2 = Essig.EventStore.read_all_stream_forward(0, 3)

assert events1 != events2

assert Enum.map(events1, & &1.seq) == [1, 2, 3]
assert Enum.map(events2, & &1.seq) == [1, 2, 3]

assert Enum.map(events1, & &1.scope_uuid) |> Enum.uniq() == [scope1]
assert Enum.map(events2, & &1.scope_uuid) |> Enum.uniq() == [scope2]
end
end

def anonym_ids(list) when is_list(list) do
Enum.map(list, &anonym_ids/1)
end
Expand Down

0 comments on commit 163c3b6

Please sign in to comment.