Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/cribbles/activerecordlite

Lightweight SQLite ORM
https://github.com/cribbles/activerecordlite

Last synced: 1 day ago
JSON representation

Lightweight SQLite ORM

Awesome Lists containing this project

README

        

# ActiveRecordLite

## Summary

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.

## Demo

1. Clone the repo
2. Head into `irb` or `pry`
3. `load './demo.rb'`
4. Go wild (using [`demo.rb`](./demo.rb) as a reference)

## Libraries

- SQLite3 (gem)
- ActiveSupport::Inflector

## Features

- Replicates core functionality of ActiveRecord::Base and
ActiveRecord::Relation
(as [`SQLObject`](/lib/sql_object.rb) and
[`SQLRelation`](/lib/sql_relation.rb))
- 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.

## API

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](/lib/sql_object.rb#L53) (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)`.

## How It Works

SQLObject and SQLRelation make reference to
[`DBConnection`](/lib/db_connection.rb), 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`](./demo.rb) for an example).

Associations (`has_many`, `belongs_to`, `has_one_through`) are dynamically
generated by the [`Associatable`](./lib/associatable.rb) 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).

## Notes

ActiveSupport::Inflector isn't a hard dependency; it's really only used in
[`has_many`](./lib/has_many_options.rb), 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.

## License

ActiveRecordLite is released under the [MIT License](/LICENSE).

---
Developed by [Chris Sloop](http://chrissloop.com)