Class: RethinkdbHelper
- Inherits:
-
Object
- Object
- RethinkdbHelper
- Extended by:
- Forwardable
- Includes:
- RethinkDB::Shortcuts
- Defined in:
- lib/rethinkdb_helper.rb
Constant Summary collapse
- DEFAULTS =
{ host: ENV['RDB_HOST'] || 'localhost', port: (ENV['RDB_PORT'] || '28015').to_i, # SMELL: is to_i necessary? db: ENV['RDB_DB'] || 'test', table: ENV['RDB_TABLE'] || 'test', auth_key: ENV['RDB_AUTH_KEY'] || 'unknown', drop: false, create_if_missing: false }
Instance Method Summary collapse
- #changes(options = {}) ⇒ Object
- #connect(options = {host: @options[:host], port: @options[:port]}) ⇒ Object
- #create_if_missing? ⇒ Boolean
-
#db(db_name = @options[:db]) ⇒ Object
def use(db_name=@options) @connection.use(db_name) end.
- #db_config ⇒ Object
- #db_drop(db_name = @options[:db]) ⇒ Object (also: #drop_db, #db_delete, #delete_db)
- #db_exist?(db_name = @options[:db]) ⇒ Boolean
- #db_list ⇒ Object (also: #list_db)
- #db_wait(*options) ⇒ Object
- #drop? ⇒ Boolean
- #get_all_keys(keys, options = {}) ⇒ Object
- #get_between_keys(lower_key, upper_key, options = {}) ⇒ Object (also: #between_keys)
- #get_key(key) ⇒ Object
- #get_table(table_name = @options[:table], options) ⇒ Object
- #index_create(index_name, index_function = nil, options = {}) ⇒ Object (also: #create_index)
- #index_drop(index_name) ⇒ Object (also: #drop_inde, #delete_index, #index_delete)
- #index_list ⇒ Object (also: #list_indexes)
- #index_wait(*indexes) ⇒ Object (also: #wait_on_index, #wait_for_index, #wait_index)
-
#initialize(options = {}) ⇒ RethinkdbHelper
constructor
TODO: Limited to one table per instance, consider support for multiple table per db support; consider multiple db per instance support.
-
#insert(payloads, options = {}) ⇒ Object
(also: #add, #load)
payloads is an array of hashes or a single hash document.
- #join(foreign_key, table_name, options = {}) ⇒ Object
- #rebalance ⇒ Object (also: #table_rebalance)
- #reconfigure(options = {}) ⇒ Object (also: #table_reconfigure)
-
#search(params = {}) ⇒ Object
TODO: Currently limited to one search field and regex consider how to use more than one field.
- #server_wait(*options) ⇒ Object
- #sync ⇒ Object (also: #flush)
- #table(table_name = @options[:table], options = {}) ⇒ Object
- #table_config ⇒ Object
- #table_create(table_name = @ooptions[:table], options = {}) ⇒ Object (also: #create_table)
- #table_drop(table_name = @options[:table]) ⇒ Object (also: #drop_table, #elete_table, #table_delete)
- #table_exist?(table_name = @options[:table]) ⇒ Boolean
- #table_list ⇒ Object (also: #list_table)
- #table_status(table_name = @options[:table]) ⇒ Object
-
#table_wait(*options) ⇒ Object
def initialize.
Constructor Details
#initialize(options = {}) ⇒ RethinkdbHelper
TODO: Limited to one table per instance, consider
support for multiple table per db support;
consider multiple db per instance support.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rethinkdb_helper.rb', line 45 def initialize(={}) = DEFAULTS.merge() @connection = connect db_drop if db_exist? && drop? unless db_exist? if create_if_missing? db_create else raise "db: '#{@options[:db]}' does not exist" end end use([:db]) unless table_exist? if create_if_missing? table_create else raise "table: '#{@options[:table]}' does not exist" end end @table = r.table([:table]) end |
Instance Method Details
#changes(options = {}) ⇒ Object
156 157 158 |
# File 'lib/rethinkdb_helper.rb', line 156 def changes(={}) @table.changes().run(connect) end |
#connect(options = {host: @options[:host], port: @options[:port]}) ⇒ Object
108 109 110 |
# File 'lib/rethinkdb_helper.rb', line 108 def connect(={host: [:host], port: [:port]}) r.connect().repl end |
#create_if_missing? ⇒ Boolean
206 207 208 |
# File 'lib/rethinkdb_helper.rb', line 206 def create_if_missing? [:create_if_missing] end |
#db(db_name = @options[:db]) ⇒ Object
125 126 127 |
# File 'lib/rethinkdb_helper.rb', line 125 def db(db_name=[:db]) @db = r.db(db_name) end |
#db_config ⇒ Object
95 96 97 |
# File 'lib/rethinkdb_helper.rb', line 95 def db_config @db.config.run end |
#db_drop(db_name = @options[:db]) ⇒ Object Also known as: drop_db, db_delete, delete_db
112 113 114 115 116 |
# File 'lib/rethinkdb_helper.rb', line 112 def db_drop(db_name=[:db]) @db = nil @table = nil r.db_drop(db_name).run end |
#db_exist?(db_name = @options[:db]) ⇒ Boolean
160 161 162 |
# File 'lib/rethinkdb_helper.rb', line 160 def db_exist?(db_name=[:db]) db_list.include?(db_name) end |
#db_list ⇒ Object Also known as: list_db
164 165 166 |
# File 'lib/rethinkdb_helper.rb', line 164 def db_list r.db_list.run end |
#db_wait(*options) ⇒ Object
100 101 102 |
# File 'lib/rethinkdb_helper.rb', line 100 def db_wait(*) @db.wait().run end |
#drop? ⇒ Boolean
202 203 204 |
# File 'lib/rethinkdb_helper.rb', line 202 def drop? [:drop] end |
#get_all_keys(keys, options = {}) ⇒ Object
214 215 216 |
# File 'lib/rethinkdb_helper.rb', line 214 def get_all_keys(keys, ={}) @table.get_all([keys].flatten, ).run end |
#get_between_keys(lower_key, upper_key, options = {}) ⇒ Object Also known as: between_keys
218 219 220 |
# File 'lib/rethinkdb_helper.rb', line 218 def get_between_keys(lower_key, upper_key, ={}) @table.between(lower_key, upper_key, ).run end |
#get_key(key) ⇒ Object
210 211 212 |
# File 'lib/rethinkdb_helper.rb', line 210 def get_key(key) @table.get(key).run end |
#get_table(table_name = @options[:table], options) ⇒ Object
133 134 135 |
# File 'lib/rethinkdb_helper.rb', line 133 def get_table(table_name=[:table], ) r.table(table_name, ).run end |
#index_create(index_name, index_function = nil, options = {}) ⇒ Object Also known as: create_index
185 186 187 |
# File 'lib/rethinkdb_helper.rb', line 185 def index_create(index_name, index_function=nil, ={}) @table.index_create(index_name, index_funciton, ).run end |
#index_drop(index_name) ⇒ Object Also known as: drop_inde, delete_index, index_delete
195 196 197 |
# File 'lib/rethinkdb_helper.rb', line 195 def index_drop(index_name) @table.index_drop(index_name).run end |
#index_list ⇒ Object Also known as: list_indexes
190 191 192 |
# File 'lib/rethinkdb_helper.rb', line 190 def index_list @table.index_list.run end |
#index_wait(*indexes) ⇒ Object Also known as: wait_on_index, wait_for_index, wait_index
178 179 180 |
# File 'lib/rethinkdb_helper.rb', line 178 def index_wait(*indexes) @table.index_wait(indexes).run end |
#insert(payloads, options = {}) ⇒ Object Also known as: add, load
payloads is an array of hashes or a single hash document.
231 232 233 234 235 236 237 238 |
# File 'lib/rethinkdb_helper.rb', line 231 def insert(payloads, ={}) payloads = [payloads].flatten raise 'No document provided' if payloads.empty? invalid_payloads = false payloads.map{|doc| invalid_payloads &&= !doc.is_a?(Hash)} raise 'Invalid document: must be Hash' if invalid_payloads @table.insert(payloads.flatten, ).run end |
#join(foreign_key, table_name, options = {}) ⇒ Object
224 225 226 227 |
# File 'lib/rethinkdb_helper.rb', line 224 def join(foreign_key, table_name,={}) @table.eq_join(foreign_key, r.table(table_name), ).without({:right => "id"}).zip().run end |
#rebalance ⇒ Object Also known as: table_rebalance
86 87 88 |
# File 'lib/rethinkdb_helper.rb', line 86 def rebalance @table.rebalance.run end |
#reconfigure(options = {}) ⇒ Object Also known as: table_reconfigure
81 82 83 |
# File 'lib/rethinkdb_helper.rb', line 81 def reconfigure(={}) @taboe.reconfigure().run end |
#search(params = {}) ⇒ Object
TODO: Currently limited to one search field and regex
consider how to use more than one field
returns an enumerable cursor into the database for documents that match the search terms.
params is a hash where the key is the symbolized field_name and its value is the regex by which to filter
250 251 252 253 254 255 256 257 258 |
# File 'lib/rethinkdb_helper.rb', line 250 def search(params={}) raise 'No search terms' if params.empty? field_name = params.keys.first search_regex = params[field_name] @table.filter{|document| document[field_name]. match(search_regex)}. run end |
#server_wait(*options) ⇒ Object
104 105 106 |
# File 'lib/rethinkdb_helper.rb', line 104 def server_wait(*) r.wait().run end |
#sync ⇒ Object Also known as: flush
137 138 139 |
# File 'lib/rethinkdb_helper.rb', line 137 def sync @table.sync.run end |
#table(table_name = @options[:table], options = {}) ⇒ Object
129 130 131 |
# File 'lib/rethinkdb_helper.rb', line 129 def table(table_name=[:table],={}) @table = r.table(table_name, ) end |
#table_config ⇒ Object
91 92 93 |
# File 'lib/rethinkdb_helper.rb', line 91 def table_config @table.config.run end |
#table_create(table_name = @ooptions[:table], options = {}) ⇒ Object Also known as: create_table
143 144 145 |
# File 'lib/rethinkdb_helper.rb', line 143 def table_create(table_name=[:table], ={}) @table = r.table_create(table_name, ).run end |
#table_drop(table_name = @options[:table]) ⇒ Object Also known as: drop_table, elete_table, table_delete
148 149 150 151 |
# File 'lib/rethinkdb_helper.rb', line 148 def table_drop(table_name=[:table]) @table = nil @db.table_drop(table_name) end |
#table_exist?(table_name = @options[:table]) ⇒ Boolean
174 175 176 |
# File 'lib/rethinkdb_helper.rb', line 174 def table_exist?(table_name=[:table]) table_list.include?(table_name) end |
#table_list ⇒ Object Also known as: list_table
169 170 171 |
# File 'lib/rethinkdb_helper.rb', line 169 def table_list r.table_list.run end |
#table_status(table_name = @options[:table]) ⇒ Object
77 78 79 |
# File 'lib/rethinkdb_helper.rb', line 77 def table_status(table_name=[:table]) r.table_status(table_name).run end |
#table_wait(*options) ⇒ Object
def initialize
73 74 75 |
# File 'lib/rethinkdb_helper.rb', line 73 def table_wait(*) @table.wait().run end |