ORMery is an object-relational mapping tool built using Ruby, with an SQLite3 database.
To try it out, simply load songs.rb into your favorite Ruby REPL (we recommend pry but irb works just fine).
To use your own SQL file, add it to db_connection.rb by changing the SQL_FILE function like so:
SQL_FILE = File.join(ROOT_FOLDER, 'YOUR_FILE.sql')
DB_FILE = File.join(ROOT_FOLDER, 'YOUR_DB.db')In the terminal, run the following command to create the database:
cat YOUR_FILE.sql | sqlite3 YOUR_DB.db
Your SQL database is now ready to go!
To create your own classes, create a .rb file, and add require_relative 'lib/sql_object'.
Each class needs to inherit from SQLObject. After writing any associations, you must also include the finalize! method for any changes to take effect.
Look at song.rb for reference!
Song.all #Returns all songs
Album.find(1) #Finds the album where id = 1
Album.columns #Returns an array of all the columns in the table
Artist.table_name #Returns the name of its SQL table
Artist.table_name = #Change the name of SQL tableClass instances can be created, modified, and saved easily:
charlie_parker = Artist.new(name: "Charlie Parker") #.new takes in an optional params hash
charlie_parker.save
bud_powell = Artist.new
bud_powell.name = "Bud Powell"
bud_powell.saveTo search by a specific column value, use the .where method, passing in a hash with the column as a key and the query as the value:
Artist.where(name: "John Coltrane") #Returns either an artist object or an empty array if nothing is foundWrite your associations in the class definition, before calling finalize!:
class Song < SQLObject
belongs_to :album #First association
has_one_through :artist, :album, :artist #Second association
finalize!
endORMery currently supports three different associations:
-
belongs_to- Use when a class has aforeign_idcolumn pointing to another class'sprimary_idcolumn. Takes a required name, and an optional hash forclass_name,primary_key,foreign_key. -
has_many- The opposite ofbelongs_to. Use when another class has a column pointing to its ownprimary_id. Takes a required name, and an optional hash forclass_name,primary_key,foreign_key. -
has_one_through- Connects twobelongs_toassociations. Requires three arguments:name,through_name, andsource_name.
In our demo file, Songs belong to Albums, Albums belong to Artists, and Songs have one Artist through Albums.
We can then call these associations as methods:
Song.album
Song.artist
Album.songs
Artist.albumsHere are some features I intend to incorporate in future releases:
.firstand.lastmethods.joinhas_many_throughandbelongs_to_throughassociations