OpalOrm
Welcome to OpalORM! OpalORM is a lightweight object-relational mapper for Ruby. With it you can define a schema for your models, and then set up relations between them for easy access in your project.
Note: this gem is in its early stages and should not be trusted with important data. If you want stronger validations, security, and more features, look no further than the library that inspired OpalORM: ActiveRecord.
Installation
Add this line to your application's Gemfile:
gem 'opal_orm'
And then execute:
$ bundle
Or install it yourself as:
$ gem install opal_orm
In the root folder of your project, create a file named Rakefile
if you don't have one already.
Paste the following into your Rakefile:
spec = Gem::Specification.find_by_name 'opal_orm'
load "#{spec.gem_dir}/lib/tasks/opal_db.rake"
Usage
Database setup
To start, run:
$ opal_orm new DATABASE_NAME
which will create an empty Sqlite database in ./db/DATABASE_NAME.db
.
Then, run:
$ opal_orm generate SCHEMA_NAME TABLE1 TABLE2 ...
Where SCHEMA_NAME
is the name of the new schema, TABLE1
, TABLE2
etc. are the names
of the tables you'd like to generate. You can define more tables in the schema file if you
need to.
The schema file will be placed in ./db/SCHEMA_NAME.rb
.
Inside your schema file, define each table in a create_table
block.
For example,
create_table("cats") do |t|
t.string :name
t.integer :toy_count
t.float :weight
t.text :biography
end
corresponds to
CREATE TABLE cats (
id INTEGER PRIMARY KEY,
name VARCHAR(255),
toy_count INTEGER,
weight REAL,
biography TEXT
);
(The primary key column is generated automatically.)
Currently, the only supported column types are string, float, text, and integer. More to come!
Once your schema file is ready, run the following rake task to convert it to SQL and add it to the database:
$ rake opal_db:setup schema=SCHEMA_FILE
Using OpalORM::SQLObject
To use OpalORM's core features, inherit your models from OpalORM::SQLObject
:
class Cat < OpalORM::SQLObject
end
Instantiate a class like normal, then access data using all
, find
, and where
:
c = Cat.new
c.name = "Mr. Bojangles"
c.save
puts Cat.find(1).name
puts Cat.all
puts Cat.where(name: "Mr. Bojangles")
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/nmatte/OpalORM.
License
The gem is available as open source under the terms of the MIT License.