How to convert this raw query to ROM/Sequel?


#1

I have this raw query in my repository class. Could use some help with this to convert it to Sequel, thanks!

Can’t find much resources on it in Sequel or ROM, wonder if any expert could help

def by_parent_days(parent, days = nil)
  query_days = (days || DEFAULT_DAYS) - 1

  impressions.read(
    <<~SQL
      SELECT COUNT(id) as counts, MAX(created_at) as date_at
      FROM impressions
      WHERE created_at >= '#{query_days.days.ago.to_date}'::date AND #{column} = '#{value}'
      GROUP BY created_at::date AT TIME ZONE '#{Time.zone.name}'
      ORDER BY date_at ASC
    SQL
  ).map do |row|
    {
      count: row[:counts],
      date: row[:date_at].in_time_zone
    }
  end
end

#2

I think I’m sticking with it in this format but data model changed slightly.
Suggestions are welcomed!

  def data_days(event_type, data_condition, days = DEFAULT_DAYS)
    query_days = (days || DEFAULT_DAYS)

    events.read(<<~SQL).map.to_a
      SELECT COUNT(id) as count, MAX(created_at) as date_at
      FROM events
      WHERE created_at >= '#{query_days.days.ago.to_date}'::date
      AND event_type = '#{event_type}'
      AND data @> '#{data_condition.to_json}'
      GROUP BY created_at::date AT TIME ZONE '#{Time.zone.name}'
      ORDER BY date_at ASC
    SQL
  end