Sha256: 8d08d4789f349676d4df300ae59bcde4fcd6dce7551ee9f4285ed11f7ed341ee

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

module PgEventstore
  module Web
    module Paginator
      class StreamContextsCollection < BaseCollection
        PER_PAGE = 10

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

        # @return [String, nil]
        def next_page_starting_id
          return unless collection.size == per_page

          starting_id = collection.first['context']
          sql_builder =
            SQLBuilder.new.select('context').from('partitions').where('stream_name is null and event_type is null').
              where("context #{direction_operator} ?", starting_id).where('context like ?', "#{options[:query]}%").
              limit(1).offset(per_page).order("context #{order}")

          connection.with do |conn|
            conn.exec_params(*sql_builder.to_exec_params)
          end.to_a.dig(0, 'context')
        end

        private

        # @return [String]
        def direction_operator
          order == :asc ? '>=' : '<='
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pg_eventstore-1.1.0 lib/pg_eventstore/web/paginator/stream_contexts_collection.rb
pg_eventstore-1.0.4 lib/pg_eventstore/web/paginator/stream_contexts_collection.rb
pg_eventstore-1.0.0.rc2 lib/pg_eventstore/web/paginator/stream_contexts_collection.rb
pg_eventstore-1.0.0.rc1 lib/pg_eventstore/web/paginator/stream_contexts_collection.rb