Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ EventSourcingDB.read_events(
"/books/42",
%EventSourcingDB.ReadEventsOptions{
recursive: false,
from_latest_event: %EventSourcingDB.FromLatestEventOptions{
from_latest_event: %EventSourcingDB.ReadFromLatestEventOptions{
subject: "/books/42",
type: "io.eventsourcingdb.library.book-borrowed",
if_event_is_missing: :read_everything
Expand Down Expand Up @@ -297,7 +297,7 @@ EventSourcingDB.observe_events(
"/books/42",
%EventSourcingDB.ObserveEventsOptions{
recursive: false,
from_latest_event: %EventSourcingDB.FromLatestEventOptions{
from_latest_event: %EventSourcingDB.ObserveFromLatestEventOptions{
subject: "/books/42",
type: "io.eventsourcingdb.library.book-borrowed",
if_event_is_missing: :read_everything
Expand Down
4 changes: 2 additions & 2 deletions lib/eventsourcingdb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ defmodule EventSourcingDB do
"/books/42",
%EventSourcingDB.ReadEventsOptions{
recursive: false,
from_latest_event: %EventSourcingDB.FromLatestEventOptions{
from_latest_event: %EventSourcingDB.ReadFromLatestEventOptions{
subject: "/books/42",
type: "io.eventsourcingdb.library.book-borrowed",
if_event_is_missing: :read_everything
Expand Down Expand Up @@ -378,7 +378,7 @@ defmodule EventSourcingDB do
"/books/42",
%EventSourcingDB.ObserveEventsOptions{
recursive: false,
from_latest_event: %EventSourcingDB.FromLatestEventOptions{
from_latest_event: %EventSourcingDB.ObserveFromLatestEventOptions{
subject: "/books/42",
type: "io.eventsourcingdb.library.book-borrowed",
if_event_is_missing: :read_everything
Expand Down
2 changes: 1 addition & 1 deletion lib/eventsourcingdb/request_options/observe_events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule EventSourcingDB.ObserveEventsOptions do

typedstruct do
field :recursive, boolean(), enforce: true
field :from_latest_event, EventSourcingDB.FromLatestEventOptions.t()
field :from_latest_event, EventSourcingDB.ObserveFromLatestEventOptions.t()
field :lower_bound, EventSourcingDB.BoundOptions.t()
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
defmodule EventSourcingDB.ObserveFromLatestEventOptions do
@moduledoc """
Options for observing events starting from the latest event of a given type.
"""
use TypedStruct

typedstruct enforce: true do
field :if_event_is_missing, :read_everything | :wait_for_event
field :subject, String.t()
field :type, String.t()
end

@spec new(keyword()) :: t()
def new(options) do
options =
options
|> Keyword.validate!([:if_event_is_missing, :subject, :type])

struct!(__MODULE__, options)
end

defimpl Jason.Encoder do
@spec encode(EventSourcingDB.ObserveFromLatestEventOptions.t(), Jason.Encode.opts()) ::
iodata()
def encode(value, opts) do
Jason.Encode.map(
%{
"ifEventIsMissing" =>
case value.if_event_is_missing do
:read_everything -> "read-everything"
:wait_for_event -> "wait-for-event"
end,
"subject" => value.subject,
"type" => value.type
},
opts
)
end
end
end
2 changes: 1 addition & 1 deletion lib/eventsourcingdb/request_options/read_events.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule EventSourcingDB.ReadEventsOptions do
typedstruct do
field :recursive, boolean(), enforce: true
field :order, :chronological | :antichronological
field :from_latest_event, EventSourcingDB.FromLatestEventOptions.t()
field :from_latest_event, EventSourcingDB.ReadFromLatestEventOptions.t()
field :lower_bound, EventSourcingDB.BoundOptions.t()
field :upper_bound, EventSourcingDB.BoundOptions.t()
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule EventSourcingDB.FromLatestEventOptions do
defmodule EventSourcingDB.ReadFromLatestEventOptions do
@moduledoc """
Reading events from when a certain event occured.
Options for reading events starting from the latest event of a given type.
"""
use TypedStruct

typedstruct enforce: true do
field :if_event_is_missing, :read_everything | :read_nothing | :wait_for_event
field :if_event_is_missing, :read_everything | :read_nothing
field :subject, String.t()
field :type, String.t()
end
Expand All @@ -20,7 +20,7 @@ defmodule EventSourcingDB.FromLatestEventOptions do
end

defimpl Jason.Encoder do
@spec encode(EventSourcingDB.FromLatestEventOptions.t(), Jason.Encode.opts()) ::
@spec encode(EventSourcingDB.ReadFromLatestEventOptions.t(), Jason.Encode.opts()) ::
iodata()
def encode(value, opts) do
Jason.Encode.map(
Expand All @@ -29,7 +29,6 @@ defmodule EventSourcingDB.FromLatestEventOptions do
case value.if_event_is_missing do
:read_everything -> "read-everything"
:read_nothing -> "read-nothing"
:wait_for_event -> "wait-for-event"
end,
"subject" => value.subject,
"type" => value.type
Expand Down
5 changes: 3 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ defmodule EventSourcingDB.MixProject do
],
"Request Options": [
EventSourcingDB.BoundOptions,
EventSourcingDB.FromLatestEventOptions,
EventSourcingDB.ObserveEventsOptions,
EventSourcingDB.ReadEventsOptions
EventSourcingDB.ObserveFromLatestEventOptions,
EventSourcingDB.ReadEventsOptions,
EventSourcingDB.ReadFromLatestEventOptions
],
Testing: [
EventSourcingDB.TestContainer
Expand Down
4 changes: 2 additions & 2 deletions test/observe_events_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule EventSourcingDBTest.ObserveEvents do
alias EventSourcingDB.TestContainer
alias EventSourcingDB.ObserveEventsOptions
alias EventSourcingDB.BoundOptions
alias EventSourcingDB.FromLatestEventOptions
alias EventSourcingDB.ObserveFromLatestEventOptions
import EventSourcingDBTest.Utils
use ExUnit.Case, async: true

Expand Down Expand Up @@ -70,7 +70,7 @@ defmodule EventSourcingDBTest.ObserveEvents do
events =
EventSourcingDB.observe_events!(client, "/", %ObserveEventsOptions{
recursive: true,
from_latest_event: %FromLatestEventOptions{
from_latest_event: %ObserveFromLatestEventOptions{
subject: "/test",
type: "io.eventsourcingdb.test.bar",
if_event_is_missing: :read_everything
Expand Down
6 changes: 3 additions & 3 deletions test/read_events_test.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule EventSourcingDBTest.ReadEvents do
alias EventSourcingDB.Errors.TransmissionError
alias EventSourcingDB.Client
alias EventSourcingDB.FromLatestEventOptions
alias EventSourcingDB.ReadFromLatestEventOptions
alias EventSourcingDB.BoundOptions
alias EventSourcingDB.ReadEventsOptions
alias EventSourcingDB.TestContainer
Expand Down Expand Up @@ -181,7 +181,7 @@ defmodule EventSourcingDBTest.ReadEvents do

events =
EventSourcingDB.read_events!(client, "/test", %ReadEventsOptions{
from_latest_event: %FromLatestEventOptions{
from_latest_event: %ReadFromLatestEventOptions{
subject: "/",
type: "io.eventsourcingdb.test.does-not-exist",
if_event_is_missing: :read_nothing
Expand All @@ -204,7 +204,7 @@ defmodule EventSourcingDBTest.ReadEvents do

events =
EventSourcingDB.read_events!(client, "/test", %ReadEventsOptions{
from_latest_event: %FromLatestEventOptions{
from_latest_event: %ReadFromLatestEventOptions{
subject: "/marker",
type: "io.eventsourcingdb.test",
if_event_is_missing: :read_nothing
Expand Down
Loading