Project

opal_orm

0.0
No commit activity in last 3 years
No release in over 3 years
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.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
~> 10.0
~> 3.0

Runtime

= 1.3.11
= 0.19.1
 Project Readme

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.