-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdcb_event_store.rb
More file actions
40 lines (34 loc) · 951 Bytes
/
dcb_event_store.rb
File metadata and controls
40 lines (34 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class DcbEventStore
def initialize(store = RubyEventStore::Client.new)
@store = store
end
def execute(projection)
projection.run(@store)
end
def append(event_or_events, query = nil, append_condition = nil)
events = Array(event_or_events)
if (append_condition && query&.last&.event_id != append_condition)
raise RubyEventStore::WrongExpectedEventVersion
end
@store.append(events)
events.each do |event|
Array(event.class.tags.call(event)).each do |tag|
@store.link(
event.event_id,
stream_name: tag,
expected_version: expected_version(append_condition, tag)
)
end
end
end
def read
@store.read
end
private
def expected_version(append_condition, stream_name)
return :auto unless append_condition
@store.position_in_stream(append_condition, stream_name)
rescue RubyEventStore::EventNotFoundInStream
:auto
end
end