Class: PgEventstore::Web::Paginator::StreamNamesCollection

Inherits:
BaseCollection
  • Object
show all
Defined in:
lib/pg_eventstore/web/paginator/stream_names_collection.rb,
sig/pg_eventstore/web/paginator/stream_names_collection.rbs

Constant Summary collapse

PER_PAGE =

Returns:

  • (Integer)
10

Constants inherited from BaseCollection

BaseCollection::MIN_QUERY_SIZE_FOR_ADVANCE_SEARCH

Instance Attribute Summary

Attributes inherited from BaseCollection

#config_name, #options, #order, #per_page, #starting_id

Instance Method Summary collapse

Methods inherited from BaseCollection

#connection, #count, #initialize, #prev_page_starting_id, #total_count

Constructor Details

This class inherits a constructor from PgEventstore::Web::Paginator::BaseCollection

Instance Method Details

#collectionArray<Hash<String => String>>

Returns:

  • (Array<Hash<String => String>>)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pg_eventstore/web/paginator/stream_names_collection.rb', line 11

def collection
  @collection ||=
    begin
      sql_builder = SQLBuilder.new.select('stream_name').from('partitions')
      sql_builder.where('event_type is null and stream_name is not null and context = ?', options[:context])
      sql_builder.where("stream_name #{direction_operator} ?", starting_id) if starting_id
      compute_stream_name_filter(sql_builder)
      sql_builder.limit(per_page).order("stream_name #{order}")
      connection.with do |conn|
        conn.exec_params(*sql_builder.to_exec_params)
      end.to_a
    end
end

#compute_stream_name_filter(builder) ⇒ void

This method returns an undefined value.

Parameters:

  • builder (PgEventstore::SQLBuilder)


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pg_eventstore/web/paginator/stream_names_collection.rb', line 49

def compute_stream_name_filter(builder)
  query = options[:query].to_s
  if query.size < MIN_QUERY_SIZE_FOR_ADVANCE_SEARCH
    builder.where('stream_name like ?', "#{query}%")
  else
    builder.where(
      'context || ? || stream_name ilike ?',
      PgEventstore::Event::SYSTEM_SYMBOL,
      "#{options[:context]}#{PgEventstore::Event::SYSTEM_SYMBOL}%#{query}%"
    )
  end
end

#direction_operatorString

Returns:

  • (String)


43
44
45
# File 'lib/pg_eventstore/web/paginator/stream_names_collection.rb', line 43

def direction_operator
  order == :asc ? '>=' : '<='
end

#next_page_starting_idString?

Returns:

  • (String, nil)


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pg_eventstore/web/paginator/stream_names_collection.rb', line 26

def next_page_starting_id
  return unless collection.size == per_page

  starting_id = collection.first['stream_name']
  sql_builder = SQLBuilder.new.select('stream_name').from('partitions')
  sql_builder.where("stream_name #{direction_operator} ?", starting_id)
  compute_stream_name_filter(sql_builder)
  sql_builder.where('event_type is null and context = ?', options[:context])
  sql_builder.limit(1).offset(per_page).order("stream_name #{order}")
  connection.with do |conn|
    conn.exec_params(*sql_builder.to_exec_params)
  end.to_a.dig(0, 'stream_name')
end