Class: Ki::Orm::Db
Overview
This singleton class establishes the database connection and CRUD operations.
Connecting
Db.instance.establish_connection
Queries
db = Db.instance
db.find 'users', {}
db.find 'users', { name: 'Homer' }
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#db ⇒ Object
readonly
Returns the value of attribute db.
Instance Method Summary collapse
-
#collection_names ⇒ Object
Returns.
- #connection_string ⇒ Object
-
#count(name, hash = {}) ⇒ Object
Count the number of hashes found in a specified collection, filtered by a hash attribute.
-
#delete(name, hash) ⇒ Object
Deletes a hash from the database.
-
#establish_connection ⇒ Object
Creates the mongo connection.
-
#find(name, hash = {}) ⇒ Object
Find a hash in the database.
-
#insert(name, hash) ⇒ Object
Insert a hash in the database.
-
#update(name, hash) ⇒ Object
Update a hash from the database.
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
21 22 23 |
# File 'lib/ki/orm.rb', line 21 def config @config end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
21 22 23 |
# File 'lib/ki/orm.rb', line 21 def connection @connection end |
#db ⇒ Object (readonly)
Returns the value of attribute db.
21 22 23 |
# File 'lib/ki/orm.rb', line 21 def db @db end |
Instance Method Details
#collection_names ⇒ Object
Returns
An array of all the collection names in the database.
56 57 58 |
# File 'lib/ki/orm.rb', line 56 def collection_names @db.collection_names.delete_if { |name| name =~ /^system/ } end |
#connection_string ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/ki/orm.rb', line 43 def connection_string db = KiConfig.instance.database if ENV['MONGODB_URI'] ENV['MONGODB_URI'] else "#{db['host']}:#{db['port']}/#{db['name']}" end end |
#count(name, hash = {}) ⇒ Object
Count the number of hashes found in a specified collection, filtered by a hash attribute
Attributes
-
name
- the mongo collection name
Options
-
hash
- used for filtering
Returns
The number of objects found.
Examples
db = Db.instance
db.count 'users'
db.count 'users', { name: 'Homer' }
182 183 184 |
# File 'lib/ki/orm.rb', line 182 def count(name, hash = {}) @db[name].count hash end |
#delete(name, hash) ⇒ Object
Deletes a hash from the database
Attributes
-
name
- the mongo collection name -
:hash
- Filter by specific hash attributes. id required
Returns
Empty hash
Examples
db = Db.instance
db.delete 'users', { id: 'id' }
db.delete 'users', {}
155 156 157 158 159 |
# File 'lib/ki/orm.rb', line 155 def delete(name, hash) hash = nourish_hash_id hash @db[name].remove hash {} end |
#establish_connection ⇒ Object
Creates the mongo connection.
It first checks if the env variable “MONGODB_URI” is set. The required format: docs.mongodb.org/manual/reference/connection-string/
If the env variable is not set, it will use the information stored in config.yml.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ki/orm.rb', line 31 def establish_connection @config = KiConfig.instance.database if ENV['MONGODB_URI'] @connection = Mongo::Connection.new @db = @connection.db else @connection = Mongo::Connection.new(@config['host'], @config['port']) @db = @connection.db(@config['name']) end self end |
#find(name, hash = {}) ⇒ Object
Find a hash in the database
Attributes
-
name
- the mongo collection name
Options
-
:hash
- Filter by specific hash attributes
Returns
An array of hashes which match the query criteria
Examples
db = Db.instance
db.find 'users'
db.find 'users', { id: 'id' }
db.find 'users', { name: 'Homer' }
104 105 106 107 |
# File 'lib/ki/orm.rb', line 104 def find(name, hash = {}) hash = nourish_hash_id hash @db[name].find(hash).to_a.stringify_ids end |
#insert(name, hash) ⇒ Object
Insert a hash in the database.
Attributes
-
name
- the mongo collection name -
hash
- the hash which will be inserted
Returns
The inserted hash.
-
:conditions
- An SQL fragment like “administrator = 1”
Examples
db = Db.instance
db.insert 'users', { name: 'Homer' }
78 79 80 81 |
# File 'lib/ki/orm.rb', line 78 def insert(name, hash) @db[name].insert(hash) [hash].stringify_ids.first end |
#update(name, hash) ⇒ Object
Update a hash from the database
The method will update the hash obj with the id specified in the hash attribute.
Attributes
-
name
- the mongo collection name -
hash
- requires at least the id of the hash which needs to beupdated
Returns
The updated hash
Examples
db = Db.instance
db.update('users', { id: 'id', name: 'Sir Homer' })
129 130 131 132 133 134 135 136 |
# File 'lib/ki/orm.rb', line 129 def update(name, hash) hash = nourish_hash_id hash id = hash['_id'].to_s hash.delete('_id') @db[name].update({ '_id' => BSON::ObjectId(id) }, hash) hash['id'] = id hash end |