Class: SandthornDriverSequel::EventStore

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

Instance Attribute 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(url: nil, context: nil) ⇒ EventStore

Returns a new instance of EventStore.



7
8
9
10
11
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 7

def initialize url: nil, context: nil
  @driver = SequelDriver.new url: url
  @context = context
  @url = url
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 5

def context
  @context
end

#driverObject (readonly)

Returns the value of attribute driver.



5
6
7
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 5

def driver
  @driver
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 5

def url
  @url
end

Instance Method Details

#build_snapshot_event(snapshot) ⇒ Object



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

def build_snapshot_event(snapshot)
  {
      aggregate_version: snapshot[:aggregate_version],
      event_data: snapshot[:snapshot_data],
      event_name: "aggregate_set_from_snapshot"
  }
end

#get_aggregate(aggregate_id, *class_name) ⇒ Object



62
63
64
65
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 62

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



22
23
24
25
26
27
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 22

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 39

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



67
68
69
70
71
72
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 67

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



74
75
76
77
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 74

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

#get_all_typesObject



79
80
81
82
83
84
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 79

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

#get_events(*args) ⇒ Object



94
95
96
97
98
99
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 94

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



101
102
103
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 101

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



86
87
88
89
90
91
92
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 86

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

#obsolete_snapshots(*args) ⇒ Object



105
106
107
108
109
110
# File 'lib/sandthorn_driver_sequel/event_store.rb', line 105

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



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

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_snapshot, aggregate_id) ⇒ Object



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

def save_snapshot aggregate_snapshot, aggregate_id
  driver.execute_in_transaction do |db|
    snapshot_access = get_snapshot_access(db)
    snapshot_access.record_snapshot(aggregate_id, aggregate_snapshot)
  end
end