Class: SandthornDriverSequel::EventStore

Inherits:
Object
  • Object
show all
Includes:
EventStoreContext
Defined in:
lib/sandthorn_driver_sequel/event_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventStoreContext

#aggregates_table_name, #events_table_name, #snapshots_table_name, #with_context_if_exists

Constructor Details

#initialize(connection, configuration, context = nil) ⇒ EventStore

Returns a new instance of EventStore.



12
13
14
15
16
17
18
19
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 12

def initialize connection, configuration, context = nil
  @driver = connection
  @context = context
  @event_serializer = configuration.event_serializer
  @event_deserializer = configuration.event_deserializer
  @snapshot_serializer = configuration.snapshot_serializer
  @snapshot_deserializer = configuration.snapshot_deserializer
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



10
11
12
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 10

def context
  @context
end

#driverObject (readonly)

Returns the value of attribute driver.



10
11
12
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 10

def driver
  @driver
end

Class Method Details

.from_url(url, configuration, context = nil) ⇒ Object



21
22
23
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 21

def self.from_url url, configuration, context = nil
  new(SequelDriver.new(url: url), configuration, context)
end

Instance Method Details

#all(aggregate_type) ⇒ Object

get methods



43
44
45
46
47
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 43

def all aggregate_type
  return get_aggregate_ids(aggregate_type: aggregate_type).map do |id|
    get_aggregate_events_from_snapshot(id)
  end
end

#find(aggregate_id) ⇒ Object



49
50
51
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 49

def find aggregate_id
  get_aggregate_events_from_snapshot(aggregate_id)
end

#get_aggregate(aggregate_id, *class_name) ⇒ Object



83
84
85
86
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 83

def get_aggregate aggregate_id, *class_name
  warn(":get_aggregate is deprecated. Use :get_aggregate_events_from_snapshot")
  get_aggregate_events_from_snapshot(aggregate_id)
end

#get_aggregate_events(aggregate_id) ⇒ Object



54
55
56
57
58
59
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 54

def get_aggregate_events(aggregate_id)
  driver.execute do |db|
    events = get_event_access(db)
    events.find_events_by_aggregate_id(aggregate_id)
  end
end

#get_aggregate_events_from_snapshot(aggregate_id) ⇒ Object

If the aggregate has a snapshot, return events starting from the snapshots. Otherwise, return all events. TODO: needs a better name



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 64

def get_aggregate_events_from_snapshot(aggregate_id)
  driver.execute do |db|
    snapshots = get_snapshot_access(db)
    event_access = get_event_access(db)
    snapshot = snapshots.find_by_aggregate_id(aggregate_id)
    if snapshot
      events = event_access.after_snapshot(snapshot)
      snapshot_event = build_snapshot_event(snapshot)
      events.unshift(snapshot_event)
    else
      event_access.find_events_by_aggregate_id(aggregate_id)
    end
  end
end

#get_aggregate_ids(aggregate_type: nil) ⇒ Object



90
91
92
93
94
95
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 90

def get_aggregate_ids(aggregate_type: nil)
  driver.execute do |db|
    access = get_aggregate_access(db)
    access.aggregate_ids(aggregate_type: aggregate_type)
  end
end

#get_aggregate_list_by_typename(type) ⇒ Object



97
98
99
100
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 97

def get_aggregate_list_by_typename(type)
  warn(":get_aggregate_list_by_typenames is deprecated. Use :get_aggregate_ids")
  get_aggregate_ids(aggregate_type: type)
end

#get_all_typesObject



102
103
104
105
106
107
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 102

def get_all_types
  driver.execute do |db|
    access = get_aggregate_access(db)
    access.aggregate_types
  end
end

#get_events(*args) ⇒ Object



117
118
119
120
121
122
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 117

def get_events(*args)
  driver.execute do |db|
    event_access = get_event_access(db)
    event_access.get_events(*args)
  end
end

#get_new_events_after_event_id_matching_classname(event_id, class_name, take: 0) ⇒ Object



124
125
126
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 124

def get_new_events_after_event_id_matching_classname event_id, class_name, take: 0
  get_events(after_sequence_number: event_id, aggregate_types: Utilities.array_wrap(class_name), take: take)
end

#get_snapshot(aggregate_id) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 109

def get_snapshot aggregate_id
  driver.execute do |db|
    snapshots = get_snapshot_access(db)
    snapshot = snapshots.find_by_aggregate_id(aggregate_id)
    snapshot.data
  end
end

#obsolete_snapshots(*args) ⇒ Object



128
129
130
131
132
133
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 128

def obsolete_snapshots(*args)
  driver.execute do |db|
    snapshots = get_snapshot_access(db)
    snapshots.obsolete(*args)
  end
end

#save_events(events, aggregate_id, class_name) ⇒ Object

save methods



26
27
28
29
30
31
32
33
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 26

def save_events events, aggregate_id, class_name
  driver.execute_in_transaction do |db|
    aggregates = get_aggregate_access(db)
    event_access = get_event_access(db)
    aggregate = aggregates.find_or_register(aggregate_id, class_name)
    event_access.store_events(aggregate, events)
  end
end

#save_snapshot(aggregate) ⇒ Object



35
36
37
38
39
40
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 35

def save_snapshot aggregate
  driver.execute_in_transaction do |db|
    snapshot_access = get_snapshot_access(db)
    snapshot_access.record_snapshot(aggregate)
  end
end