Sha256: f96fa86667f5cc85bc98157aa09fcaa19f09027cec27c493cd2a98448453ecae

Contents?: true

Size: 1.73 KB

Versions: 6

Compression:

Stored size: 1.73 KB

Contents

module EventSource
  module Postgres
    class Write
      include EventSource::Write

      dependency :put

      def configure(session: nil)
        Put.configure(self, session: session)
      end

      def write(batch, stream_name, expected_version: nil)
        logger.trace(tag: :write) { "Writing batch (Stream Name: #{stream_name}, Number of Events: #{batch.length}, Expected Version: #{expected_version.inspect})" }

        unless expected_version.nil?
          expected_version = ExpectedVersion.canonize(expected_version)
        end

        last_position = nil
        put.session.transaction do
          batch.each do |event_data|
            last_position = write_event_data(event_data, stream_name, expected_version: expected_version)

            unless expected_version.nil?
              expected_version += 1
            end
          end
        end

        logger.debug(tag: :write) { "Wrote batch (Stream Name: #{stream_name}, Number of Events: #{batch.length}, Expected Version: #{expected_version.inspect})" }

        last_position
      end

      def write_event_data(event_data, stream_name, expected_version: nil)
        logger.trace(tag: :write) { "Writing event data (Stream Name: #{stream_name}, Type: #{event_data.type}, Expected Version: #{expected_version.inspect})" }
        logger.trace(tags: [:data, :event_data, :write]) { event_data.pretty_inspect }

        put.(event_data, stream_name, expected_version: expected_version).tap do
          logger.debug(tag: :write) { "Wrote event data (Stream Name: #{stream_name}, Type: #{event_data.type}, Expected Version: #{expected_version.inspect})" }
          logger.debug(tags: [:data, :event_data, :write]) { event_data.pretty_inspect }
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
evt-event_source-postgres-0.19.0.3 lib/event_source/postgres/write.rb
evt-event_source-postgres-0.19.0.2 lib/event_source/postgres/write.rb
evt-event_source-postgres-0.19.0.1 lib/event_source/postgres/write.rb
evt-event_source-postgres-0.19.0.0 lib/event_source/postgres/write.rb
evt-event_source-postgres-0.18.0.1 lib/event_source/postgres/write.rb
evt-event_source-postgres-0.18.0.0 lib/event_source/postgres/write.rb