Technical writings of Shkrt
Sequel is a database ORM for Ruby applications, authored by Jeremy Evans, also an author of Roda. Many modern web application building toolkits, such as Hanami, Phoenix, Dry-Web etc. tend to stay away from ActiveRecord for different reasons, and most of them do not utilize even active record pattern. Sequel from this point of view can be considered as an alternative ORM, using the same active record pattern, as the ActiveRecord itself does. Looking at the Sequel’s documentation, transition from ActiveRecord to Sequel should not be that difficult.
This is how our ActiveRecord setup looks like:
Let’s achieve the same functionality via Sequel:
It seems like migrating a simple ActiveRecord application to sequel won’t cause many problems.
Let’s take a look at some of the mostly used queries:
AR | Sequel | SQL(AR) | SQL(Sequel) |
---|---|---|---|
Book.find(1) |
Book[1] |
SELECT "books"."*" FROM "books" WHERE "books"."id" = 1 LIMIT 1 |
SELECT * FROM "books" WHERE ("id"=1) |
Book.last |
Book.last |
SELECT "books"."*" FROM "books" ORDER BY "books"."id" DESC LIMIT 1 |
SELECT * FROM "books" ORDER BY "id" LIMIT 1 |
Writer.includes(:books) |
Writer.eager(:books) |
SELECT "writers"."*" FROM "writers" SELECT "books"."*" FROM "books" WHERE "books"."writer_id" IN (1) |
|
Book.select(:id) |
Book.select(:id) |
SELECT "books"."id" FROM "books" |
SELECT "id" FROM "books" |
Writer.joins(:books) |
Writer.association_join(:books) |
SELECT "writers"."*" FROM "writers" INNER JOIN "books" ON "books"."writer_id" = "writers"."id" |
SELECT * FROM "writers" INNER JOIN "books" ON ("books"."writer_id" = "writers"."id") |
Writer.joins('LEFT JOIN books ON books.writer_id = writers.id') |
Writer.aasociation_left_join(:books) |
SELECT "writers"."*" FROM "writers" LEFT JOIN "books" ON "books"."writer_id" = "writers"."id" |
SELECT * FROM "writers" LEFT JOIN "books" ON ("books"."writer_id" = "writers"."id") |
Writer.order(full_name: :desc) |
Writer.reverse_order(:full_name) |
SELECT "writers"."*" FROM "writers" ORDER BY "writers"."full_name" DESC |
SELECT * FROM "writers" ORDER BY "writers"."full_name" DESC |
As we see, Sequel’s API offers very convenient interface for ordering, eager loading, joining and many more typical orm-related tasks. To actually use Sequel with Rails instead of ActiveRecord, you will have to use sequel-rails gem. If you’re not in need of full orm support, including generators, migrations, or if you just want to do some experiments in command line, you can inherit existing Rails models from Sequel::Model, and set up all associations from scratch. That way you can also migrate your application part by part, iteratively changing connected models to Sequel::Model.
Suggested reading:
Why you should stop using ActiveRecord and start using Sequel
[ruby
sequel
activerecord
]