rethinkdb_helper 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +17 -4
- data/lib/rethinkdb_helper.rb +182 -24
- data/rethinkdb_helper.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c7050a7a77d22a393fcc4b88452ae20146867fc
|
4
|
+
data.tar.gz: 584a217453b2d1c644b206c1e7ffc6fb783edfe1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33f11b396b13dae5c7f366af664d8d6cb3c82323bb4cecca753b111a4726c2de6b9363d817c667d8ed1d0569eb97339fb03310b8ff519788a2b69dfca910b9aa
|
7
|
+
data.tar.gz: 76fc8386bf4c5460cbd53f00990b8b1bf67fca5ad5bb3d9e26b1cf5b4038aadfd92518625123622d58a66da3d14aceaf89ed299426f24b678281c7c037fede9e
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# RethinkdbHelper
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
My convention as a wrapper around the rethinkdb gem.
|
4
|
+
Under Development.
|
5
|
+
Don't use it.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -22,7 +22,20 @@ Or install it yourself as:
|
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
```ruby
|
26
|
+
db = RethinkdbHelper.new(
|
27
|
+
host: 'localhost', # default
|
28
|
+
port: 28015, # default
|
29
|
+
db: 'test', # default
|
30
|
+
table: 'test', # default
|
31
|
+
create_if_missing: true # created db/table if they are missing
|
32
|
+
)
|
33
|
+
db.insert(json_documents) # takes array of hashes or json
|
34
|
+
cursor = db.search(:field_name, field_value_regex)
|
35
|
+
puts cursor.count
|
36
|
+
cursor.each {|d| ap d}
|
37
|
+
etc.
|
38
|
+
```
|
26
39
|
|
27
40
|
## Development
|
28
41
|
|
data/lib/rethinkdb_helper.rb
CHANGED
@@ -4,46 +4,64 @@
|
|
4
4
|
## Desc: Encapsulates some basic utility functions
|
5
5
|
## By: Dewayne VanHoozer (dvanhoozer@gmail.com)
|
6
6
|
#
|
7
|
+
# TODO: Review the nobrainer gem to see if this stuff is moot
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'forwardable'
|
11
|
+
|
7
12
|
require 'rethinkdb'
|
8
13
|
|
9
14
|
class RethinkdbHelper
|
10
15
|
include RethinkDB::Shortcuts
|
16
|
+
extend Forwardable
|
11
17
|
|
12
18
|
DEFAULTS = {
|
13
|
-
host: ENV['
|
14
|
-
port: (ENV['
|
15
|
-
db: ENV['
|
16
|
-
table: ENV['
|
19
|
+
host: ENV['RDB_HOST'] || 'localhost',
|
20
|
+
port: (ENV['RDB_PORT'] || '28015').to_i, # SMELL: is to_i necessary?
|
21
|
+
db: ENV['RDB_DB'] || 'test',
|
22
|
+
table: ENV['RDB_TABLE'] || 'test',
|
23
|
+
auth_key: ENV['RDB_AUTH_KEY'] || 'unknown',
|
17
24
|
drop: false,
|
18
25
|
create_if_missing: false
|
19
26
|
}
|
20
27
|
|
28
|
+
# TODO: Pass some methods to r for fullfillment
|
29
|
+
# connect, db, table
|
30
|
+
#def_delegators :r, :connect, :db, :table
|
31
|
+
|
32
|
+
# TODO: Pass some methods to @commection for fulfillment
|
33
|
+
# close, reconnect, use, noreply_wait
|
34
|
+
def_delegators :@connection, :close, :reconnect, :use, :noreply_wait
|
35
|
+
|
36
|
+
# TODO: Pass some methods to @table for fulfillment
|
37
|
+
# filter, get, get_all, between, eq_join,
|
38
|
+
# inner_join, outer_join
|
39
|
+
def_delegators :@table, :filter, :get, :get_all, :between, :eq_join,
|
40
|
+
:inner_join, :outer_join
|
41
|
+
|
21
42
|
# TODO: Limited to one table per instance, consider
|
22
43
|
# support for multiple table per db support;
|
23
44
|
# consider multiple db per instance support.
|
24
45
|
def initialize(options={})
|
25
46
|
@options = DEFAULTS.merge(options)
|
26
47
|
|
27
|
-
@connection =
|
28
|
-
host: @options[:host],
|
29
|
-
port: @options[:port]
|
30
|
-
).repl
|
48
|
+
@connection = connect
|
31
49
|
|
32
|
-
|
50
|
+
db_drop if db_exist? && drop?
|
33
51
|
|
34
52
|
unless db_exist?
|
35
53
|
if create_if_missing?
|
36
|
-
|
54
|
+
db_create
|
37
55
|
else
|
38
56
|
raise "db: '#{@options[:db]}' does not exist"
|
39
57
|
end
|
40
58
|
end
|
41
59
|
|
42
|
-
|
60
|
+
use(@options[:db])
|
43
61
|
|
44
62
|
unless table_exist?
|
45
63
|
if create_if_missing?
|
46
|
-
|
64
|
+
table_create
|
47
65
|
else
|
48
66
|
raise "table: '#{@options[:table]}' does not exist"
|
49
67
|
end
|
@@ -52,14 +70,135 @@ class RethinkdbHelper
|
|
52
70
|
@table = r.table(@options[:table])
|
53
71
|
end # def initialize
|
54
72
|
|
55
|
-
def
|
56
|
-
|
73
|
+
def table_wait(*options)
|
74
|
+
@table.wait(options).run
|
75
|
+
end
|
76
|
+
|
77
|
+
def table_status(table_name=@options[:table])
|
78
|
+
r.table_status(table_name).run
|
57
79
|
end
|
58
80
|
|
59
|
-
def
|
60
|
-
|
81
|
+
def reconfigure(options={})
|
82
|
+
@taboe.reconfigure(options).run
|
83
|
+
end
|
84
|
+
alias :table_reconfigure :reconfigure
|
85
|
+
|
86
|
+
def rebalance
|
87
|
+
@table.rebalance.run
|
88
|
+
end
|
89
|
+
alias :table_rebalance :rebalance
|
90
|
+
|
91
|
+
def table_config
|
92
|
+
@table.config.run
|
93
|
+
end
|
94
|
+
|
95
|
+
def db_config
|
96
|
+
@db.config.run
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
def db_wait(*options)
|
101
|
+
@db.wait(options).run
|
61
102
|
end
|
62
103
|
|
104
|
+
def server_wait(*options)
|
105
|
+
r.wait(options).run
|
106
|
+
end
|
107
|
+
|
108
|
+
def connect(options={host: @options[:host], port: @options[:port]})
|
109
|
+
r.connect(options).repl
|
110
|
+
end
|
111
|
+
|
112
|
+
def db_drop(db_name=@options[:db])
|
113
|
+
@db = nil
|
114
|
+
@table = nil
|
115
|
+
r.db_drop(db_name).run
|
116
|
+
end
|
117
|
+
alias :drop_db :db_drop
|
118
|
+
alias :db_delete :db_drop
|
119
|
+
alias :delete_db :db_drop
|
120
|
+
|
121
|
+
#def use(db_name=@options[:db])
|
122
|
+
# @connection.use(db_name)
|
123
|
+
#end
|
124
|
+
|
125
|
+
def db(db_name=@options[:db])
|
126
|
+
@db = r.db(db_name)
|
127
|
+
end
|
128
|
+
|
129
|
+
def table(table_name=@options[:table],options={})
|
130
|
+
@table = r.table(table_name, options)
|
131
|
+
end
|
132
|
+
|
133
|
+
def get_table(table_name=@options[:table], options)
|
134
|
+
r.table(table_name, options).run
|
135
|
+
end
|
136
|
+
|
137
|
+
def sync
|
138
|
+
@table.sync.run
|
139
|
+
end
|
140
|
+
alias :flush :sync
|
141
|
+
|
142
|
+
|
143
|
+
def table_create(table_name=@ooptions[:table], options={})
|
144
|
+
@table = r.table_create(table_name, options).run
|
145
|
+
end
|
146
|
+
alias :create_table :table_create
|
147
|
+
|
148
|
+
def table_drop(table_name=@options[:table])
|
149
|
+
@table = nil
|
150
|
+
@db.table_drop(table_name)
|
151
|
+
end
|
152
|
+
alias :drop_table :table_drop
|
153
|
+
alias :elete_table :table_drop
|
154
|
+
alias :table_delete :table_drop
|
155
|
+
|
156
|
+
def changes(options={})
|
157
|
+
@table.changes(options).run(connect)
|
158
|
+
end
|
159
|
+
|
160
|
+
def db_exist?(db_name=@options[:db])
|
161
|
+
db_list.include?(db_name)
|
162
|
+
end
|
163
|
+
|
164
|
+
def db_list
|
165
|
+
r.db_list.run
|
166
|
+
end
|
167
|
+
alias :list_db :db_list
|
168
|
+
|
169
|
+
def table_list
|
170
|
+
r.table_list.run
|
171
|
+
end
|
172
|
+
alias :list_table :table_list
|
173
|
+
|
174
|
+
def table_exist?(table_name=@options[:table])
|
175
|
+
table_list.include?(table_name)
|
176
|
+
end
|
177
|
+
|
178
|
+
def index_wait(*indexes)
|
179
|
+
@table.index_wait(indexes).run
|
180
|
+
end
|
181
|
+
alias :wait_on_index :index_wait
|
182
|
+
alias :wait_for_index :index_wait
|
183
|
+
alias :wait_index :index_wait
|
184
|
+
|
185
|
+
def index_create(index_name, index_function=nil, options={})
|
186
|
+
@table.index_create(index_name, index_funciton, options).run
|
187
|
+
end
|
188
|
+
alias :create_index :index_create
|
189
|
+
|
190
|
+
def index_list
|
191
|
+
@table.index_list.run
|
192
|
+
end
|
193
|
+
alias :list_indexes :index_list
|
194
|
+
|
195
|
+
def index_drop(index_name)
|
196
|
+
@table.index_drop(index_name).run
|
197
|
+
end
|
198
|
+
alias :drop_inde :index_drop
|
199
|
+
alias :delete_index :index_drop
|
200
|
+
alias :index_delete :index_drop
|
201
|
+
|
63
202
|
def drop?
|
64
203
|
@options[:drop]
|
65
204
|
end
|
@@ -68,14 +207,34 @@ class RethinkdbHelper
|
|
68
207
|
@options[:create_if_missing]
|
69
208
|
end
|
70
209
|
|
210
|
+
def get_key(key)
|
211
|
+
@table.get(key).run
|
212
|
+
end
|
213
|
+
|
214
|
+
def get_all_keys(keys, options={})
|
215
|
+
@table.get_all([keys].flatten, options).run
|
216
|
+
end
|
217
|
+
|
218
|
+
def get_between_keys(lower_key, upper_key, options={})
|
219
|
+
@table.between(lower_key, upper_key, options).run
|
220
|
+
end
|
221
|
+
alias :between_keys :get_between_keys
|
222
|
+
|
223
|
+
|
224
|
+
def join(foreign_key, table_name,options={})
|
225
|
+
@table.eq_join(foreign_key,
|
226
|
+
r.table(table_name), options).without({:right => "id"}).zip().run
|
227
|
+
end
|
228
|
+
|
71
229
|
# payloads is an array of hashes or a single
|
72
230
|
# hash document.
|
73
|
-
def insert(
|
231
|
+
def insert(payloads, options={})
|
232
|
+
payloads = [payloads].flatten
|
74
233
|
raise 'No document provided' if payloads.empty?
|
75
234
|
invalid_payloads = false
|
76
235
|
payloads.map{|doc| invalid_payloads &&= !doc.is_a?(Hash)}
|
77
236
|
raise 'Invalid document: must be Hash' if invalid_payloads
|
78
|
-
@table.insert(payloads).run
|
237
|
+
@table.insert(payloads.flatten, options).run
|
79
238
|
end
|
80
239
|
alias :add :insert
|
81
240
|
alias :load :insert
|
@@ -88,7 +247,7 @@ class RethinkdbHelper
|
|
88
247
|
#
|
89
248
|
# params is a hash where the key is the symbolized field_name
|
90
249
|
# and its value is the regex by which to filter
|
91
|
-
def
|
250
|
+
def search(params={})
|
92
251
|
raise 'No search terms' if params.empty?
|
93
252
|
field_name = params.keys.first
|
94
253
|
search_regex = params[field_name]
|
@@ -97,12 +256,11 @@ class RethinkdbHelper
|
|
97
256
|
match(search_regex)}.
|
98
257
|
run
|
99
258
|
end
|
100
|
-
alias :search :filter
|
101
259
|
|
102
|
-
def close
|
103
|
-
|
104
|
-
end
|
260
|
+
#def close
|
261
|
+
# @connection.close
|
262
|
+
#end
|
105
263
|
|
106
264
|
end # class RethinkdbHelper
|
107
265
|
|
108
|
-
|
266
|
+
RDB = RethinkdbHelper unless defined?(RDB)
|
data/rethinkdb_helper.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rethinkdb_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dewayne VanHoozer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|