Class: Appfuel::Dynamodb::NoSqlModel
Class Method Summary
collapse
Instance Method Summary
collapse
-
#basic_table_query(attrs_returned, key_expr, values = {}) ⇒ Object
-
#basic_table_query_params(attrs_returned, key_expr, values = {}) ⇒ Object
-
#batch_get(ids, &block) ⇒ Object
-
#batch_get_params(ids, opts = {}) ⇒ Object
-
#batch_keys(ids) ⇒ Object
-
#batch_put(list) ⇒ Object
-
#client ⇒ Object
-
#delete(hash_value, range_value = nil) ⇒ Object
-
#delete_item(hash_value, range_value = nil) ⇒ Object
-
#get(hash_value, range_value = nil) ⇒ Object
-
#get_item(hash_value, range_value = nil) ⇒ Object
-
#index_name(key) ⇒ Object
-
#manual_query(params) ⇒ Object
-
#primary_key ⇒ Object
-
#put(data) ⇒ Object
-
#put_params(data) ⇒ Object
-
#query_select_map(type) ⇒ Object
-
#scan(params = {}) ⇒ Object
-
#select_index(key, select, key_expr, values = {}) ⇒ Object
-
#select_index_params(key, attrs_returned, key_expr, values = {}) ⇒ Object
-
#table_name ⇒ Object
-
#table_params(hash_key_value, range_key_value = nil) ⇒ Object
-
#table_prefix ⇒ Object
#app_container, #feature_name, included, #qualify_container_key
Class Method Details
.client ⇒ Object
46
47
48
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 46
def client
@client ||= app_container[CLIENT_CONTAINER_KEY]
end
|
.config ⇒ Object
38
39
40
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 38
def config
@config ||= load_config
end
|
.config_key(value = nil) ⇒ Object
14
15
16
17
18
19
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 14
def config_key(value = nil)
if value.nil?
return @config_key ||= DEFAULT_CONFIG_KEY
end
@config_key = value.to_s
end
|
.container_class_type ⇒ Object
10
11
12
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 10
def container_class_type
'dynamodb'
end
|
.index(index_key, index_name) ⇒ Object
59
60
61
62
63
64
65
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 59
def index(index_key, index_name)
unless index_name.start_with?(table_prefix)
index_name = "#{table_prefix}#{index_name}"
end
indexes[index_key.to_sym] = index_name
end
|
.indexes ⇒ Object
50
51
52
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 50
def indexes
@indexes ||= {}
end
|
.inherited(klass) ⇒ Object
67
68
69
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 67
def inherited(klass)
stage_class_for_registration(klass)
end
|
.load_config ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 21
def load_config
config = app_container[:config]
key = config_key.to_s
if key.include?('.')
keys = key.split('.').map {|k| k.to_sym}
else
keys = [config_key]
end
@config ||= keys.each.inject(config) do |c, k|
unless c.key?(k)
fail "[dynamodb] config key (#{k}) not found - #{self}"
end
c[k]
end
end
|
.primary_key(hash = nil, hash_type = nil, range = nil, range_type = nil) ⇒ Object
71
72
73
74
75
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 71
def primary_key(hash = nil, hash_type = nil, range = nil, range_type = nil)
return @primary_key if hash.nil?
@primary_key = PrimaryKey.new(hash, hash_type, range, range_type)
end
|
.table_name(value = nil) ⇒ Object
54
55
56
57
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 54
def table_name(value = nil)
return @table_name if value.nil?
@table_name = "#{table_prefix}#{value}"
end
|
.table_prefix ⇒ Object
42
43
44
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 42
def table_prefix
@table_prefex ||= config[:table_prefix]
end
|
Instance Method Details
#basic_table_query(attrs_returned, key_expr, values = {}) ⇒ Object
145
146
147
148
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 145
def basic_table_query(attrs_returned, key_expr, values = {})
params = basic_table_query_params(attrs_returned, key_expr, values)
manual_query(params)
end
|
#basic_table_query_params(attrs_returned, key_expr, values = {}) ⇒ Object
136
137
138
139
140
141
142
143
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 136
def basic_table_query_params(attrs_returned, key_expr, values = {})
create_table_hash(
table_name: table_name,
select: query_select_map(attrs_returned),
key_condition_expression: key_expr,
expression_attribute_values: values
)
end
|
#batch_get(ids, &block) ⇒ Object
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 171
def batch_get(ids, &block)
table_key = table_name
params = batch_get_params(ids)
result = client.batch_get_item(params)
unless result.responses.key?(table_key)
fail "db table name #{table_key} is not correct"
end
list = result.responses[table_key]
return list unless block_given?
list.each do |item|
yield item
end
end
|
#batch_get_params(ids, opts = {}) ⇒ Object
163
164
165
166
167
168
169
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 163
def batch_get_params(ids, opts = {})
{
request_items: {
table_name => {keys: batch_keys(ids)}
}
}
end
|
#batch_keys(ids) ⇒ Object
154
155
156
157
158
159
160
161
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 154
def batch_keys(ids)
fail "ids must response to :map" unless ids.respond_to?(:map)
key = primary_key
ids.map do |id|
hash_value, range_value = id.is_a?(Array)? id : [id, nil]
key.params(hash_value, range_value)
end
end
|
#batch_put(list) ⇒ Object
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 193
def batch_put(list)
cards = list.map do |card|
{ put_request: { item: card } }
end
payload = {
request_items: { table_name => cards }
}
client.batch_write_item(payload)
end
|
#client ⇒ Object
80
81
82
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 80
def client
self.class.client
end
|
#delete(hash_value, range_value = nil) ⇒ Object
222
223
224
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 222
def delete(hash_value, range_value = nil)
delete_item(hash_value, range_value)
end
|
#delete_item(hash_value, range_value = nil) ⇒ Object
226
227
228
229
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 226
def delete_item(hash_value, range_value = nil)
params = table_params(hash_value, range_value)
client.delete_item(params)
end
|
#get(hash_value, range_value = nil) ⇒ Object
205
206
207
208
209
210
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 205
def get(hash_value, range_value = nil)
result = get_item(hash_value, range_value)
return false if result.item.nil?
result.item
end
|
#get_item(hash_value, range_value = nil) ⇒ Object
217
218
219
220
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 217
def get_item(hash_value, range_value = nil)
params = table_params(hash_value, range_value)
client.get_item(params)
end
|
#index_name(key) ⇒ Object
92
93
94
95
96
97
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 92
def index_name(key)
unless self.class.indexes.key?(key)
fail "index #{key} has not been registered"
end
self.class.indexes[key]
end
|
#manual_query(params) ⇒ Object
150
151
152
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 150
def manual_query(params)
client.query(params)
end
|
#primary_key ⇒ Object
99
100
101
102
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 99
def primary_key
fail "No primary key assigned" if self.class.primary_key.nil?
self.class.primary_key
end
|
#put(data) ⇒ Object
188
189
190
191
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 188
def put(data)
params = put_params(data)
client.put_item(params)
end
|
#put_params(data) ⇒ Object
104
105
106
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 104
def put_params(data)
create_table_hash(item: data)
end
|
#query_select_map(type) ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 113
def query_select_map(type)
case type
when :all_attrs then 'ALL_ATTRIBUTES'
when :all_projected then 'ALL_PROJECTED_ATTRIBUTES'
when :count then 'COUNT'
when :specific then 'SPECIFIC_ATTRIBUTES'
end
end
|
#scan(params = {}) ⇒ Object
212
213
214
215
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 212
def scan(params = {})
params[:table_name] = table_name
client.scan(params)
end
|
#select_index(key, select, key_expr, values = {}) ⇒ Object
131
132
133
134
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 131
def select_index(key, select, key_expr, values = {})
params = select_index_params(key, select, key_expr, values)
manual_query(params)
end
|
#select_index_params(key, attrs_returned, key_expr, values = {}) ⇒ Object
122
123
124
125
126
127
128
129
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 122
def select_index_params(key, attrs_returned, key_expr, values = {})
create_table_hash(
index_name: index_name(key),
select: query_select_map(attrs_returned),
key_condition_expression: key_expr,
expression_attribute_values: values
)
end
|
#table_name ⇒ Object
84
85
86
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 84
def table_name
self.class.table_name
end
|
#table_params(hash_key_value, range_key_value = nil) ⇒ Object
108
109
110
111
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 108
def table_params(hash_key_value, range_key_value = nil)
params = primary_key.params(hash_key_value, range_key_value)
create_table_hash(key: params)
end
|
#table_prefix ⇒ Object
88
89
90
|
# File 'lib/appfuel/storage/dynamodb/adapter.rb', line 88
def table_prefix
self.class.table_prefix
end
|