BridgeClient::ORM::Base

The BridgeClient::ORM::Base module should be inherited by modules/models in your Ruby/Rails application, giving your modules access to method such as find, update, and delete. Both class methods and instance methods are included, giving your model a feel similar to an ORM model such as ActiveRecord.

Setup

Begin by creating module that inherits from BridgeClient::ORM::Base. You must set self.from to the table name you would like your module to be based on. Each instance of this class will contain data from a row in this table. You must also set self.schema to the schema containing the above table.

class Note < BridgeClient::ORM::Base
  self.from = 'notes'
  self.schema = 'myschema'
end

Instances of your class contain an instance variable called @record, which is a hash of a row in the self.from database, where each key is a column in the table. Data inside of the @record hash can be accessed by using the name of the column as a method. See the self.query example below for more information.

self.query

The self.query class method create an instance of BridgeClient::Query and automatically setting from to be your self.from value. See the BridgeClient::Query documentation for more information on how to use Query. When results are returned from running the query using list or first, the object returned is an instance of this class.

Example:

notes = Note.query.limit(2).list
=> [#<Note:0x277dddcf @record={"updated_at"=>"2020-8-11T19:32:10Z", "note_text"=>"My note", "id"=>1, "created_at"=>"2020-8-11T19:32:10Z"}>, #<Note:0xd5eca06 @record={"updated_at"=>"2020-8-11T20:9:9Z", "note_text"=>"My other note", "id"=>2, "created_at"=>"2020-8-11T20:9:9Z"}>]

notes.first.record
=> {"updated_at"=>"2020-8-11T19:32:10Z", "note_text"=>"My note", "id"=>1, "created_at"=>"2020-8-11T19:32:10Z"}

# Recall that data inside of the `@record` hash can be accessed directly by calling column as a method. 
notes.first.note_text
=> "My note"

self.create(params = {})

The self.create class method allows you to insert a row into the database and an instance of this class is returned with the inserted data.

Example:

note = Note.create(note_text: "My note 3")
=> #<Note:0x5cdafb4d @record={"updated_at"=>"2021-8-12T18:41:29Z", "note_text"=>"My note 3", "id"=>8, "created_at"=>"2021-8-12T18:41:29Z"}>
note.note_text
=> "My note 3"

self.find(id)

The self.find class method queries the database for a record with its id column matching the argument; it is returned if it exists.

Example:

note = Note.find(8)
=> #<Note:0x65528bcf @record={"updated_at"=>"2021-8-12T18:41:29Z", "note_text"=>"My note 3", "id"=>8, "created_at"=>"2021-8-12T18:41:29Z"}>
note.note_text
=> "My note 3"

update(params = {})

The update method does a SQL update to the instance using the provided arguments. It returns a new instance containing the modified data.

Example:

note = Note.find(8)
=> #<Note:0x65528bcf @record={"updated_at"=>"2021-8-12T18:41:29Z", "note_text"=>"My note 3", "id"=>8, "created_at"=>"2021-8-12T18:41:29Z"}>

note.update(note_text: "My note 4")
=> #<Note:0x65528bcf @record={"updated_at"=>"2021-8-12T18:41:29Z", "note_text"=>"My note 4", "id"=>8, "created_at"=>"2021-8-12T18:41:29Z"}>

delete()

The delete method deletes a SQL record from the database if one exists with an id matching the argument; it returns a boolean indicating whether it was successful.

Example:

note = Note.find(8)
=> #<Note:0x5bed8b91 @record={"updated_at"=>"2021-8-12T18:41:29Z", "note_text"=>"My note 4", "id"=>8, "created_at"=>"2021-8-12T18:41:29Z"}>

note.delete()
=> true