class FrpEventsourcing::EventRepository

Attributes

adapter[R]

Public Class Methods

new(adapter: ::EventStoreEvent) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 3
def initialize(adapter: ::EventStoreEvent)
  @adapter = adapter
end

Public Instance Methods

create(event, stream_name) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 8
def create(event, stream_name)
  data = event.to_h.merge!(stream: stream_name)
  adapter.create(data)

  event.emit if event.respond_to?(:emit)

  event
end
delete_stream(stream_name) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 17
def delete_stream(stream_name)
  condition = {stream: stream_name}
  adapter.destroy_all condition
end
has_event?(event_id) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 22
def has_event?(event_id)
  adapter.exists?(event_id: event_id)
end
last_stream_event(stream_name) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 26
def last_stream_event(stream_name)
  build_event_entity(adapter.where(stream: stream_name).last)
end
read_all_streams_backward(start_event_id, count) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 73
def read_all_streams_backward(start_event_id, count)
  stream = adapter
  unless start_event_id.equal?(:head)
    starting_event = adapter.find_by(event_id: start_event_id)
    stream = stream.where('id < ?', starting_event)
  end

  stream.order('id DESC').limit(count)
    .map(&method(:build_event_entity))
end
read_all_streams_forward(start_event_id, count) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 62
def read_all_streams_forward(start_event_id, count)
  stream = adapter
  unless start_event_id.equal?(:head)
    starting_event = adapter.find_by(event_id: start_event_id)
    stream = stream.where('id > ?', starting_event)
  end

  stream.limit(count)
    .map(&method(:build_event_entity))
end
read_events_backward(stream_name, start_event_id, count) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 41
def read_events_backward(stream_name, start_event_id, count)
  stream = adapter.where(stream: stream_name)
  unless start_event_id.equal?(:head)
    starting_event = adapter.find_by(event_id: start_event_id)
    stream = stream.where('id < ?', starting_event)
  end

  stream.order('id DESC').limit(count)
    .map(&method(:build_event_entity))
end
read_events_forward(stream_name, start_event_id, count) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 30
def read_events_forward(stream_name, start_event_id, count)
  stream = adapter.where(stream: stream_name)
  unless start_event_id.equal?(:head)
    starting_event = adapter.find_by(event_id: start_event_id)
    stream = stream.where('id > ?', starting_event)
  end

  stream.limit(count)
    .map(&method(:build_event_entity))
end
read_stream_events_backward(stream_name) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 57
def read_stream_events_backward(stream_name)
  adapter.where(stream: stream_name).order('id DESC')
    .map(&method(:build_event_entity))
end
read_stream_events_forward(stream_name) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 52
def read_stream_events_forward(stream_name)
  adapter.where(stream: stream_name)
    .map(&method(:build_event_entity))
end

Private Instance Methods

build_event_entity(record) click to toggle source
# File lib/frp-eventsourcing/event_repository.rb, line 86
def build_event_entity(record)
  return nil unless record
  record.event_type.constantize.new(
    event_id: record.event_id,
    metadata: record.metadata,
    data: record.data
  )
end