Class: Epiphy::Adapter::Rethinkdb
- Inherits:
-
Object
- Object
- Epiphy::Adapter::Rethinkdb
- Includes:
- RethinkDB::Shortcuts
- Defined in:
- lib/epiphy/adapter/rethinkdb.rb
Instance Method Summary collapse
-
#all(collection) ⇒ Array
private
Returns all the records for the given collection.
-
#clear(collection) ⇒ Object
private
Delete all records from the table.
-
#connection=(connection) ⇒ Object
Assign a RethinkDB connection for this adapter.
-
#count(collection) ⇒ Integer
private
Count entity in table.
-
#create(collection, entity) ⇒ Object
private
Insert a document.
-
#database=(db) ⇒ Object
Set the current, default database for this connection.
-
#delete(collection, id) ⇒ Object
private
Remove the record from the given collection, with the given id.
-
#find(collection, id) ⇒ Object
private
Returns an unique record from the given collection, with the given id.
-
#first(collection, order_by: nil) ⇒ Object
private
Returns the first record in the given collection.
-
#initialize(conn, database: 'test') ⇒ Rethinkdb
constructor
Create an adapter object, with the option to pass a connection object as dependency.
-
#last(collection, order_by: nil) ⇒ Object
private
Returns the last record in the given collection.
-
#persist(collection, entity) ⇒ Object
private
Creates or updates a record in the database for the given entity.
-
#query(table: nil, database: nil) ⇒ Object
private
Execute a ReQL query.
-
#update(collection, entity) ⇒ Object
private
Insert a document.
Constructor Details
#initialize(conn, database: 'test') ⇒ Rethinkdb
Create an adapter object, with the option to pass a connection object as dependency.
Without the dattabase string, Epiphy will assumes a ‘test` database just as RethinkDB do by default.
25 26 27 28 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 25 def initialize(conn, database: 'test') self.connection=(conn) self.database=(database) unless database.nil? end |
Instance Method Details
#all(collection) ⇒ Array
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns all the records for the given collection
187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 187 def all(collection) # TODO consider to make this lazy (aka remove #all) #query(collection).all begin query table: collection do |r| r end rescue RethinkDB::RqlRuntimeError => e raise Epiphy::Model::RuntimeError, e. rescue Exception =>e raise Epiphy::Model::RuntimeError, e. end end |
#clear(collection) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Delete all records from the table
272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 272 def clear(collection) begin result = query table: collection do |r| r.delete() end if result["errors"] == 0 return result['deleted'] end return false rescue RethinkDB::RqlRuntimeError => e raise Epiphy::Model::RuntimeError, e. end end |
#connection=(connection) ⇒ Object
Assign a RethinkDB connection for this adapter.
chain it
38 39 40 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 38 def connection= (connection) @connection = connection end |
#count(collection) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Count entity in table
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 209 def count(collection) # TODO consider to make this lazy (aka remove #all) begin query table: collection do |r| r.count end rescue RethinkDB::RqlRuntimeError => e raise Epiphy::Model::RuntimeError, e. rescue Exception =>e raise Epiphy::Model::RuntimeError, e. end end |
#create(collection, entity) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Insert a document.
When the ID is already existed, we simply return the ID if insert succesful. Or, the generated ID will be returned.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 141 def create(collection, entity) begin result = query table: collection do |r| r.insert(entity) end rescue RethinkDB::RqlRuntimeError => e raise e end if result["inserted"]==1 return entity["id"] if result["generated_keys"].nil? result["generated_keys"].first else if result['first_error'].include? 'Duplicate primary key' raise Epiphy::Model::EntityExisted, 'Duplicate primary key' end end end |
#database=(db) ⇒ Object
Set the current, default database for this connection.
At, any time, you can re-set this to change the database. Doing so will trigger the adapter to switch to the new database.
At RethinkDB level, this is similar with what we do with r.db(‘foo’)
58 59 60 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 58 def database= (db) @database = db end |
#delete(collection, id) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Remove the record from the given collection, with the given id
251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 251 def delete(collection, id) begin result = query table: collection do |r| r.get(id).delete() end if result["errors"] == 0 return result["deleted"] end return false rescue return false end end |
#find(collection, id) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an unique record from the given collection, with the given id.
232 233 234 235 236 237 238 239 240 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 232 def find(collection, id) #begin result = query table: collection do |r| r.get(id) end #rescue #end result end |
#first(collection, order_by: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the first record in the given collection.
294 295 296 297 298 299 300 301 302 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 294 def first(collection, order_by: nil) begin query table: collection do |q,r| q.order_by(r.asc(order_by)).nth(0) end rescue RethinkDB::RqlRuntimeError => e return nil end end |
#last(collection, order_by: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the last record in the given collection.
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 312 def last(collection, order_by: nil) begin query table: collection do |q, r| q.order_by(r.desc(order_by)).nth(0) end rescue RethinkDB::RqlRuntimeError => e return nil end end |
#persist(collection, entity) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates or updates a record in the database for the given entity.
120 121 122 123 124 125 126 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 120 def persist(collection, entity) if entity["id"] update(collection, entity) else create(collection, entity) end end |
#query(table: nil, database: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Execute a ReQL query. The database and table is passed as parameter and the query is build by a block.
The table param can be omitted so we can run the drop, create table
With a valid database param, only this query will be run on it. To set a persitent different database for subsequent queries, consider set a different database.
The block is passed 2 object. The first is the ReQL at the table() level. The second is the ReQL at top name space level
101 102 103 104 105 106 107 108 109 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 101 def query(table: nil, database: nil) raise ArgumentError, 'Missing query block' unless block_given? if block_given? rql = get_table(table, database) @current_rql = rql rql = yield(rql, r) end rql.run(@connection) end |
#update(collection, entity) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Insert a document.
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/epiphy/adapter/rethinkdb.rb', line 167 def update(collection, entity) begin result = query table: collection do |r| r.get(entity["id"]).update(entity) end rescue return false end return result["replaced"] end |