Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upcasting records to newer types as transformation in pipeline #836

Merged
merged 3 commits into from
Dec 30, 2020

Conversation

mostlyobvious
Copy link
Member

@mostlyobvious mostlyobvious commented Nov 3, 2020

Usage:

  class Mapper < PipelineMapper
    def initialize(upcast_map: {})
      super(Pipeline.new(
        Transformation::Upcast.new(upcast_map),
      ))
    end
  end

  RubyEventStore::Client.new(
    mapper: Mapper.new(upcast_map: {
      'OldEventType' => lambda { |record|
        Record.new(
          event_type: 'NewEventType',
          data:        ...,
          metadata:    record.metadata,
          timestamp:   record.timestamp,
          valid_at:    record.valid_at,
          event_id:    record.event_id
        )
      }
    }),
    repository: ...
  )

@mostlyobvious
Copy link
Member Author

mostlyobvious commented Nov 3, 2020

Possible use:

mk_record = ->(record, new_event_type, new_data) do
  Record.new(
    event_type:  new_event_type,
    data:        new_data,
    metadata:    record.metadata,
    timestamp:   record.timestamp,
    valid_at:    record.valid_at,
    event_id:    record.event_id
  )
end

class NewEventType
  class UpcastFromOldEventType
    def call(record)
      ...
    end
  end
end

RubyEventStore::Client.new(
  mapper: Mapper.new(upcast_map: {
    'OldEventType' => lambda { |record| mk_record.call(record, 'NewEventType', record.data) },
    'OldEventType' => NewEventType::UpcastFromOldEventType.new,
  }),
  repository: ...
)

Usage:

  class Mapper < PipelineMapper
    def initialize(upcast_map: {})
      super(Pipeline.new(
        Transformation::Upcast.new(upcast_map),
      ))
    end
  end

  RubyEventStore::Client.new(
    mapper: Mapper.new(upcast_map: {
      'OldEventType' => lambda { |record|
        Record.new(
          event_type: 'NewEventType',
          data:        ...,
          metadata:    record.metadata,
          timestamp:   record.timestamp,
          valid_at:    record.valid_at,
          event_id:    record.event_id
        )
      }
    }),
    repository: ...
  )
@mostlyobvious mostlyobvious merged commit aa2f6d2 into master Dec 30, 2020
@mostlyobvious mostlyobvious deleted the upcasting branch December 30, 2020 20:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant