ActiveRecordLite is an ORM that maps SQLite queries onto Ruby objects. It aims to provide an austere, yet full-featured alternative to ActiveRecord without all the overhead.
- Clone the repo
- Head into
irb
orpry
load './demo.rb'
- Go wild (using
demo.rb
as a reference)
- SQLite3 (gem)
- ActiveSupport::Inflector
- Replicates core functionality of ActiveRecord::Base and
ActiveRecord::Relation
(as
SQLObject
andSQLRelation
) - Super friendly API - most SQLObject and SQLRelation enumerative methods will accept params or a block as an argument
- SQLRelation search parameters are stackable and lazily-evaluated - i.e.
Cat.all.where(name: "Rocco").where(owner_id: 2)
won't fire off a SQL query until you call#count
,#force
,#limit
, etc.
SQLObject provides a few of your favorite ActiveRecord associations:
has_many
belongs_to
has_one_through
SQLObject provides all your favorite ActiveRecord methods:
::count
::find
::where
::delete_all
::update_all
#save
#create
#update
#destroy
SQLRelation provides all your favorite Enumerable
methods:
#<<
#all?
#any?
#count
#empty?
#first
#last
#none?
#one?
SQLObject delegates enumerable methods (along with
::find
, ::where
, ::delete_all
and ::update_all
) to SQLRelation. For
example, Cat.any?
will iterate over the entire cats
table and return an
SQLRelation instance.
SQLObject utility methods conventionally return an SQLRelation instance as a
collection object. This means you can chain methods like so:
Cat.where(owner_id: 1).update_all(owner_id: 2).find(1)
.
SQLObject and SQLRelation make reference to
DBConnection
, which delegates SQLite::Database
instance methods like #execute
, #get_first_row
, #last_insert_row_id
, etc.
to a singleton instance of SQLite::Database. Consumers of the API specify this
instance by calling DBConnection::open(file)
, providing an SQLite db file path
as an argument.
SQLObject and SQLRelation provide a utility belt of class/instance methods that
map to SQL queries. You can make use of the SQLObject API by creating a class
that inherits from it (see demo.rb
for an example).
Associations (has_many
, belongs_to
, has_one_through
) are dynamically
generated by the Associatable
module, which extends
SQLObject. Associations infer class_name
, foreign_key
and primary_key
based on the same conventions as ActiveRecord. You can override these if the
database schema you're working with is non-compliant (or
ActiveSupport::Inflector fails to guess your table name).
ActiveSupport::Inflector isn't a hard dependency; it's really only used in
has_many
, to provide sensible class_name
and foreign_key
defaults (see above).
If you really want to trim overhead, you can get away with declaring options for these manually in your association definitions.
ActiveRecordLite is released under the MIT License.
Developed by Chris Sloop