Class: TCB::EventQuery

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

Instance Method Summary collapse

Constructor Details

#initialize(store:, context:, stream_id: nil, from_version: nil, to_version: nil, occurred_after: nil) ⇒ EventQuery

Returns a new instance of EventQuery.



5
6
7
8
9
10
11
12
# File 'lib/tcb/event_query.rb', line 5

def initialize(store:, context:, stream_id: nil, from_version: nil, to_version: nil, occurred_after: nil)
  @store = store
  @context = context
  @stream_id = stream_id
  @from_version = from_version
  @to_version = to_version
  @occurred_after = occurred_after
end

Instance Method Details

#between_versions(from, to) ⇒ Object



47
48
49
# File 'lib/tcb/event_query.rb', line 47

def between_versions(from, to)
  from_version(from).to_version(to)
end

#from_version(version) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/tcb/event_query.rb', line 25

def from_version(version)
  self.class.new(
    store: @store,
    context: @context,
    stream_id: @stream_id,
    from_version: version,
    to_version: @to_version,
    occurred_after: @occurred_after
  )
end

#in_batches(of: 1000, from_version: nil, to_version: nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/tcb/event_query.rb', line 76

def in_batches(of: 1000, from_version: nil, to_version: nil)
  return enum_for(:in_batches, of: of, from_version: from_version, to_version: to_version) unless block_given?

  cursor = from_version || @from_version
  ceiling = to_version || @to_version

  loop do
    batch = @store.read(
      @stream_id,
      from_version: cursor,
      to_version: ceiling,
      occurred_after: @occurred_after,
      limit: of
    )

    break if batch.empty?

    yield batch

    break if batch.size < of

    cursor = batch.last.version + 1
  end
end

#last(count) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tcb/event_query.rb', line 62

def last(count)
  return [] unless @stream_id

  result = @store.read(
    @stream_id,
    from_version: @from_version,
    to_version: @to_version,
    occurred_after: @occurred_after,
    limit: count,
    order: :desc
  )
  result.reverse
end

#occurred_after(time) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/tcb/event_query.rb', line 51

def occurred_after(time)
  self.class.new(
    store: @store,
    context: @context,
    stream_id: @stream_id,
    from_version: @from_version,
    to_version: @to_version,
    occurred_after: time
  )
end

#stream(aggregate_id) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/tcb/event_query.rb', line 14

def stream(aggregate_id)
  self.class.new(
    store: @store,
    context: @context,
    stream_id: StreamId.build(@context, aggregate_id).to_s,
    from_version: @from_version,
    to_version: @to_version,
    occurred_after: @occurred_after
  )
end

#to_aObject



101
102
103
104
105
# File 'lib/tcb/event_query.rb', line 101

def to_a
  result = []
  in_batches { |batch| result.push(*batch) }
  result
end

#to_version(version) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/tcb/event_query.rb', line 36

def to_version(version)
  self.class.new(
    store: @store,
    context: @context,
    stream_id: @stream_id,
    from_version: @from_version,
    to_version: version,
    occurred_after: @occurred_after
  )
end