Sha256: 87a2abb5e6a209cb55b8b98c4e7fe0682eca7db6b2ae234b3dc00be73826a03e

Contents?: true

Size: 1.69 KB

Versions: 7

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

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

        # @return [Array<Hash>]
        def collection
          @_collection ||=
            begin
              sql_builder =
                SQLBuilder.new.select('stream_id').from('events').
                  where('context = ? and stream_name = ?', options[:context], options[:stream_name]).
                  where('stream_id like ?', "#{options[:query]}%")
              sql_builder.where("stream_id #{direction_operator} ?", starting_id) if starting_id
              sql_builder.group('stream_id').limit(per_page).order("stream_id #{order}")
              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['stream_id']
          sql_builder =
            SQLBuilder.new.select('stream_id').from('events').
              where("stream_id #{direction_operator} ?", starting_id).where('stream_id like ?', "#{options[:query]}%").
              where('context = ? and stream_name = ?', options[:context], options[:stream_name]).
              group('stream_id').limit(1).offset(per_page).order("stream_id #{order}")

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

        private

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pg_eventstore-1.1.3 lib/pg_eventstore/web/paginator/stream_ids_collection.rb
pg_eventstore-1.1.2 lib/pg_eventstore/web/paginator/stream_ids_collection.rb
pg_eventstore-1.1.1 lib/pg_eventstore/web/paginator/stream_ids_collection.rb
pg_eventstore-1.1.0 lib/pg_eventstore/web/paginator/stream_ids_collection.rb
pg_eventstore-1.0.4 lib/pg_eventstore/web/paginator/stream_ids_collection.rb
pg_eventstore-1.0.0.rc2 lib/pg_eventstore/web/paginator/stream_ids_collection.rb
pg_eventstore-1.0.0.rc1 lib/pg_eventstore/web/paginator/stream_ids_collection.rb