Class: TCB::EventStore::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/tcb/event_store/active_record.rb

Instance Method Summary collapse

Constructor Details

#initializeActiveRecord

Returns a new instance of ActiveRecord.



8
9
10
# File 'lib/tcb/event_store/active_record.rb', line 8

def initialize
  @mutex = Mutex.new
end

Instance Method Details

#append(stream_id:, events:, occurred_at: Time.now, correlation_id: nil, causation_id: nil) ⇒ Object



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
41
42
43
# File 'lib/tcb/event_store/active_record.rb', line 12

def append(stream_id:, events:, occurred_at: Time.now, correlation_id: nil, causation_id: nil)
  @mutex.synchronize do
    next_ver = next_version(stream_id)

    envelopes = events.map.with_index(next_ver) do |event, version|
      event_id = SecureRandom.uuid

      event_record_for(stream_id).create!(
        event_id:       event_id,
        stream_id:      stream_id,
        version:        version,
        event_type:     event.class.name,
        payload:        serialize(event),
        occurred_at:    occurred_at,
        correlation_id: correlation_id,
        causation_id:   causation_id
      )

      TCB::Envelope.new(
        event:          event,
        event_id:       event_id,
        stream_id:      stream_id,
        version:        version,
        occurred_at:    occurred_at,
        correlation_id: correlation_id,
        causation_id:   causation_id
      )
    end

    envelopes
  end
end

#read(stream_id, from_version: nil, to_version: nil, occurred_after: nil, limit: nil, order: :asc) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tcb/event_store/active_record.rb', line 45

def read(stream_id, from_version: nil, to_version: nil, occurred_after: nil, limit: nil, order: :asc)
  scope = event_record_for(stream_id)
    .where(stream_id: stream_id)
    .order(version: order)

  scope = scope.where("version >= ?", from_version) if from_version
  scope = scope.where("version <= ?", to_version) if to_version
  scope = scope.where("occurred_at > ?", occurred_after) if occurred_after
  scope = scope.limit(limit) if limit

  scope.map do |record|
    TCB::Envelope.new(
      event:          deserialize(record.payload),
      event_id:       record.event_id,
      stream_id:      record.stream_id,
      version:        record.version,
      occurred_at:    record.occurred_at,
      correlation_id: record.correlation_id,
      causation_id:   record.causation_id
    )
  end
end

#read_by_correlation(correlation_id, context:, occurred_after: nil, occurred_before: nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tcb/event_store/active_record.rb', line 68

def read_by_correlation(correlation_id, context:, occurred_after: nil, occurred_before: nil)
  tables = find_tables_for_context(context)
  return [] if tables.empty?

  union_sql = tables.map do |table|
    conditions = ["correlation_id = ?"]
    conditions << "occurred_at > ?" if occurred_after
    conditions << "occurred_at < ?" if occurred_before

    "SELECT * FROM #{table} WHERE #{conditions.join(' AND ')}"
  end.join(" UNION ALL ")

  bindings = tables.flat_map do
    params = [correlation_id]
    params << occurred_after  if occurred_after
    params << occurred_before if occurred_before
    params
  end

  sql = "#{union_sql} ORDER BY occurred_at ASC"
  records = ::ActiveRecord::Base.connection.exec_query(
    ::ActiveRecord::Base.sanitize_sql_array([sql, *bindings])
  )

  records.map do |record|
    TCB::Envelope.new(
      event:          deserialize(record["payload"]),
      event_id:       record["event_id"],
      stream_id:      record["stream_id"],
      version:        record["version"],
      occurred_at:    Time.parse(record["occurred_at"].to_s),
      correlation_id: record["correlation_id"],
      causation_id:   record["causation_id"]
    )
  end
end