Raw SQL
You can perform queries with raw SQL:
def all_by_sql
users.read("SELECT * FROM users")
end
end
You can sort records using #order
:
Limit
class UserRepository < Hanami::Repository
users.order { created_at.desc }.limit(number)
end
end
You can use any SQL functions like ILIKE
, IN
, NOT
, , etc.. These functions are available as Ruby methods inside the #where
block:
Joins
You can join several relations:
class BookRepository < Hanami::Repository
associations do
has_many :comments
end
def commented_within(date_range)
books
.join(comments)
.where(comments[:created_at].qualified => date_range)
end
end
In case your database schema doesn’t follow this convention above, you can specify an explicit foreign key:
You can also use #inner_join
method.
class UserRepository < Hanami::Repository
associations do
has_many :books
end
def users_group_by_id
users.
left_join(:books).
group(:id)
end