Sequel is Gorgeous
by Ardekantur
My original DataMapper classes:
class Post include DataMapper::Resource property :id, Serial property :title, String property :body, Text property :created, DateTime property :author, String validates_present :body, :title has n, :categories, :through => Resource def self.latest_by_author(name) all(:author => name, :order => [:created.desc]) end def self.count_by_author(name) count(:author => name) end def categories_as_comma_list categories.map { |x| x.name }.join ', ' end def categories_as_linked_comma_list categories.map { |x| "<a href='/archive/#{x.name}'>#{x.name}</a>"}.join ', ' end end class Category include DataMapper::Resource property :id, Serial property :name, String validates_present :name has n, :posts, :through => Resource end
My new Sequel classes:
class Post < Sequel::Model many_to_many :categories def self.latest_by_author(name) all(:author => name, :order => [:created.desc]) end def self.count_by_author(name) filter(:author => name).count end def categories_as_comma_list categories.map { |x| x.name }.join ', ' end def categories_as_linked_comma_list categories.map { |x| "<a href='/archive/#{x.name}'>#{x.name}</a>"}.join ', ' end end class Category < Sequel::Model many_to_many :posts end
Ignoring the migrations and validations, next to nothing has changed. All of my queries work as expected. They’re inefficient because I’m not giving Sequel any hints on how to handle them, but that will hopefully change soon as I migrate to using Sequel’s DSL to specify queries, and stop using models altogether. I’ve been mucking around in the ORM debate and the more I think about it, the more I’d like to use a lightweight language to describe my SQL, rather than hand it all off to a more complex ORM, which is exactly what Sequel provides me.