Skip to content

Commit

Permalink
Use ROM combine instead of SQL joins
Browse files Browse the repository at this point in the history
  • Loading branch information
joelvh committed Apr 17, 2018
1 parent 42ba423 commit 5a9809c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
Expand Up @@ -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

Expand All @@ -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
Expand Down
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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)
Expand Down

0 comments on commit 5a9809c

Please sign in to comment.