:pig: An expressive query DSL for Active Record
Have you ever used the Squeel gem? It's a really nice way to build complex queries. However, Squeel monkeypatches Active Record internals, because it was aimed at enhancing the existing API with the aim of inclusion into Rails. However, that inclusion never happened, and it left Squeel susceptible to breakage from arbitrary changes in Active Record, eventually burning out the maintainer.
BabySqueel provides a Squeel-like query DSL for Active Record while hopefully avoiding the majority of the version upgrade difficulties via a minimum of monkeypatching. :heart:
Add this line to your application's Gemfile:
gem 'baby_squeel'And then execute:
$ bundleOr install it yourself as:
$ gem install baby_squeelWith Active Record, you might write something like this:
Post.where('created_at >= ?', 2.weeks.ago)But then someone tells you, "Hey, you should use Arel!". So you convert your query to use Arel:
Post.where(Post.arel_table[:created_at].gteq(2.weeks.ago))Well, that's great, but it's also pretty verbose. Why don't you give BabySqueel a try:
Post.where.has { created_at >= 2.weeks.ago }BabySqueel's blocks use instance_eval, which means you won't have access to your instance variables or methods. Don't worry, there's a really easy solution. Just give arity to the block:
Post.where.has { |post| post.created_at >= 2.weeks.ago }Okay, so we have some models:
…Post.ordering { [id.desc, title.asc] }
# SELECT "posts".* FROM "posts"
# ORDER BY "posts"."id" DESC, "posts"."title" ASC
Post.ordering { (id * 5).desc }
# SELECT "posts".* FROM "posts"
# ORDER BY "posts"."id" * 5 DESC
Post.select(:author_id).group(:author_id).ordering { id.count.desc }
# SELECT "posts"."author_id" FROM "posts"
# GROUP BY "posts"."author_id"
# ORDER BY COUNT("posts"."id") DESC
Post.joins(:author).ordering { author.id.desc }
# SELECT "posts".* FROM "posts"
# INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
# ORDER BY "authors"."id" DESC…Post.selecting { id.count }.grouping { author_id }.when_having { id.count > 5 }
# SELECT COUNT("posts"."id") FROM "posts"
# GROUP BY "posts"."author_id"
# HAVING (COUNT("posts"."id") > 5)Post.selecting { coalesce(author_id, 5).as('author_id_with_default') }
# SELECT coalesce("posts"."author_id", 5) AS author_id_with_default FROM "posts"Post.joins(:author).where.has {
author.id.in Author.select(:id).where(name: 'Ray')
}
# SELECT "posts".* FROM "posts"
# INNER JOIN "authors" ON "authors"."id" = "posts"."author_id"
# WHERE "authors"."id" IN (
# SELECT "authors"."id" FROM "authors"
# WHERE "authors"."name" = 'Ray'
# )Post.where.has {
exists Post.where.has { author_id == 1 }
}
# SELECT "posts".* FROM "posts"
# WHERE (
# EXISTS(
# SELECT "posts".* FROM "posts"
# WHERE "posts"."author_id" = 1
# )
# )authors = Author.selecting { name.op('||', quoted('-dizzle')).as('swag') }
# SELECT "authors"."name" || '-dizzle' AS swag FROM "authors"
authors.first.swag #=> 'Ray Zane-dizzle'table = BabySqueel[:some_table]
Post.joining {
table.on(table.post_id == id)
}.where.has {
table.some_column == 1
}Given this polymorphism:
# app/models/picture.rb
belongs_to :imageable, polymorphic: true
# app/models/post.rb
has_many :pictures, as: :imageableThe query might look like this:
Picture.
joining { imageable.of(Post) }.
selecting { imageable.of(Post).id }# SQL Literals
Post.select('1 as one').ordering { sql('one').desc }
# Quoting
Post.selecting { title.op('||', quoted('diddly')) }
# Functions
Post.selecting { func('coalesce', id, 1) }Sifters are like little snippets of conditions that can take arguments.
class Post 7.1.6'; rm Gemfile.lock; bin/setup; rake
export AR='~> 7.2.3'; rm Gemfile.lock; bin/setup; rake
export AR='~> 8.0.4'; rm Gemfile.lock; bin/setup; rake
export AR='~> 8.1.1'; rm Gemfile.lock; bin/setup; rakeYou can also run bin/console to open up a prompt where you'll have access to some models to experiment with.
Bug reports and pull requests are welcome on GitHub at https://github.com/rzane/baby_squeel.
The gem is available as open source under the terms of the MIT License.
No open issues yet, or sync has not completed.