Class: Epiphy::Adapter::Rethinkdb

Inherits:
Object
  • Object
show all
Includes:
RethinkDB::Shortcuts
Defined in:
lib/epiphy/adapter/rethinkdb.rb

Instance Method Summary collapse

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.

Examples:

connection = Epiphy::Connection.create  
adapter = Epiphy::Adapter::Rethinkdb.new(connection,
default_database)

Parameters:

  • connection (RethinkDB::Connection)
  • database (String) (defaults to: 'test')

    database name

Since:

  • 0.0.1



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

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Array)

    all the records

Since:

  • 0.1.0



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.message
  rescue Exception =>e
    raise Epiphy::Model::RuntimeError, e.message
  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

Parameters:

  • collection (Symbol)

    the target collection

Returns:

  • how many entry we removed

Since:

  • 0.0.1



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.message
  end
end

#connection=(connection) ⇒ Object

Assign a RethinkDB connection for this adapter.

chain it

Parameters:

  • connection (RethinkDB::Connection)

Returns:

  • (Object)

    the current adapter. enable us to continue

Since:

  • 0.1.0



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

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Integer)

    How many record?

Since:

  • 0.2.0



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.message
  rescue Exception =>e
    raise Epiphy::Model::RuntimeError, e.message
  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.

Parameters:

  • collection (Symbol the target collection)

    ollection [Symbol the target collection

  • entity (#id, #id=)

    the entity to create

Returns:

  • (Object)

    the entity

Raises:

Since:

  • 0.0.1



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’)

Examples:

adapter.database("test")
# Subsequent query will be run on `test` database
adapter.database("foo")
# Subsequent query will be run on `foo` database


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

Parameters:

  • collection (Symbol)

    the target collection

  • id (Object)

    the identity of the object

Returns:

  • (Object)

    the entity

Since:

  • 0.1.0



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.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • id (Object)

    the identity of the object.

Returns:

  • (Object)

    the entity

Since:

  • 0.1.0



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.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Object)

    the first entity

Since:

  • 0.1.0



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.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

Returns:

  • (Object)

    the last entity

Since:

  • 0.1.0



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.

Parameters:

  • collection (Symbol)

    the target collection (it must be mapped).

  • entity (#id, #id=)

    the entity to persist

Returns:

  • (Object)

    the entity

Since:

  • 0.1.0



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

Examples:

# To create a table
adapter.query do |r, rt|
  r.table_create('table_name')
end  

# To filter
adapter.query table: 'movie' do |r|
  r.filter({category: 'romantic'})
end

# To Drop a database
adapter.query do |t, r|
  r.db_drop 'dbname'
end

Parameters:

  • collection (Epiphy::Repository)
  • table (String) (defaults to: nil)
  • database (String) (defaults to: nil)

Returns:

  • query result of RethinkDB::run

Raises:

  • (ArgumentError)

See Also:

Since:

  • 0.0.1



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.

Parameters:

  • collection (Symbol the target collection)

    ollection [Symbol the target collection

  • entity (#id, #id=)

    the entity to create

Returns:

  • (Object)

    the entity

Since:

  • 0.0.1



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