Permalink
Browse files

Use ROM `combine` instead of SQL joins

  • Loading branch information...
joelvh committed Apr 17, 2018
1 parent 42ba423 commit 5a9809cdee2f34f12294658faf6a10596ee977f7
@@ -10,7 +10,7 @@ class Events < ::ROM::Relation[:sql]
attribute :created_at, ::ROM::Types::DateTime.default { Time.now.utc }

associations do
has_many :stream_entries, foreign_key: :event_id
has_many :stream_entries
end
end

@@ -20,10 +20,6 @@ class Events < ::ROM::Relation[:sql]
def by_pks(ids)
where(id: ids)
end

def for_stream_entries(assoc, stream_entries)
new dataset.order(nil).join(stream_entries.dataset, event_id: :id)
end
end
end
end
@@ -10,7 +10,7 @@ class StreamEntries < ::ROM::Relation[:sql]
attribute :created_at, ::ROM::Types::DateTime.default { Time.now.utc }

associations do
belongs_to :events, as: :event
belongs_to :events, as: :event, foreign_key: :event_id
end
end

@@ -42,7 +42,7 @@ def ordered(direction, stream, offset_entry_id = nil)
order_columns = %i[position id]
order_columns.delete(:position) if stream.global?

query = where(stream: stream.name)
query = by_stream(stream)
query = query.where { id.public_send(operator, offset_entry_id) } if offset_entry_id
query.order { |r| order_columns.map { |c| r[:stream_entries][c].public_send(order) } }
end
@@ -80,12 +80,15 @@ def read(direction, stream, from: :head, limit: nil)
unless from.equal?(:head)
offset_entry_id = stream_entries.by_stream_and_event_id(stream, from)[:id]
end

events
.for_stream_entries(nil, stream_entries.ordered(direction, stream, offset_entry_id))
.limit(limit)
.map_with(:serialized_record_mapper)
.to_a

Mappers::SerializedRecord.new.call(
stream_entries
.ordered(direction, stream, offset_entry_id)
.limit(limit)
.combine(:event)
.to_a
.map(&:event)
)

rescue ::ROM::TupleCountMismatchError
raise EventNotFound.new(from)

0 comments on commit 5a9809c

Please sign in to comment.