Class: SandthornDriverSequel2::EventStore

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventStoreContext

#events_table_name, #snapshots_table_name, #with_context_if_exists

Constructor Details

#initialize(url: nil, context: nil, file_output_options: {}) ⇒ EventStore

Returns a new instance of EventStore.



7
8
9
10
11
12
13
14
15
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 7

def initialize url: nil, context: nil, file_output_options: {}
  @driver = SequelDriver.new url: url
  @context = context
  @url = url

  driver.execute do |db|
    @storage = Storage.new(db, context, file_output_options)
  end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



5
6
7
# File 'lib/sandthorn_driver_sequel_2/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_2/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_2/event_store.rb', line 5

def url
  @url
end

Instance Method Details

#build_snapshot_event(snapshot) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 57

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



65
66
67
68
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 65

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



25
26
27
28
29
30
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 25

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 42

def get_aggregate_events_from_snapshot(aggregate_id)
  driver.execute do |db|
    #snapshots = get_snapshot_access(db)
    event_access = get_event_access(db)
    snapshot = false#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



70
71
72
73
74
75
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 70

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

#get_aggregate_list_by_typename(type) ⇒ Object



77
78
79
80
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 77

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_events(*args) ⇒ Object



97
98
99
100
101
102
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 97

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



104
105
106
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 104

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

def get_all_types

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

end



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

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



108
109
110
111
112
113
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 108

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



17
18
19
20
21
22
23
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 17

def save_events events, aggregate_id, class_name
  driver.execute_in_transaction do |db|
    event_access = get_event_access(db)
    events = events.map { |event| event[:aggregate_type] = class_name; event[:aggregate_id] = aggregate_id; event;}
    event_access.store_events(events)
  end
end

#save_snapshot(aggregate_snapshot, aggregate_id) ⇒ Object



32
33
34
35
36
37
# File 'lib/sandthorn_driver_sequel_2/event_store.rb', line 32

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