Class: DynamoDbRepository
- Inherits:
-
Object
- Object
- DynamoDbRepository
- Defined in:
- lib/dynamodb_framework/dynamodb_repository.rb
Instance Attribute Summary collapse
-
#dynamodb ⇒ Object
readonly
Returns the value of attribute dynamodb.
-
#table_name ⇒ Object
Returns the value of attribute table_name.
Instance Method Summary collapse
- #all ⇒ Object
- #delete(key) ⇒ Object
- #get_by_key(hash_key, hash_value, range_key = nil, range_value = nil) ⇒ Object
-
#initialize ⇒ DynamoDbRepository
constructor
A new instance of DynamoDbRepository.
- #map(item) ⇒ Object
- #put(item) ⇒ Object
- #query(hash_key_name, hash_key_value, range_key_name = nil, range_key_value = nil, expression = nil, expression_params = nil, index_name = nil, limit = nil, count = false) ⇒ Object
- #scan(expression, expression_params, limit = nil, count = false) ⇒ Object
Constructor Details
#initialize ⇒ DynamoDbRepository
Returns a new instance of DynamoDbRepository.
6 7 8 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 6 def initialize @dynamodb = DynamoDbStore.new end |
Instance Attribute Details
#dynamodb ⇒ Object (readonly)
Returns the value of attribute dynamodb.
3 4 5 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 3 def dynamodb @dynamodb end |
#table_name ⇒ Object
Returns the value of attribute table_name.
4 5 6 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 4 def table_name @table_name end |
Instance Method Details
#all ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 55 def all result = dynamodb.client.scan({ :table_name => table_name }) output = [] result.items.each do |item| output.push(map(item)) end return output end |
#delete(key) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 25 def delete(key) params = { table_name: @table_name, key: key } dynamodb.client.delete_item(params) end |
#get_by_key(hash_key, hash_value, range_key = nil, range_value = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 37 def get_by_key(hash_key, hash_value, range_key = nil, range_value = nil) key = {} key[hash_key] = hash_value if(range_key != nil) key[range_key] = range_value end params = { table_name: table_name, key: key } result = dynamodb.client.get_item(params) return map(result.item) end |
#map(item) ⇒ Object
181 182 183 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 181 def map(item) return item end |
#put(item) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 10 def put(item) json_string = item.to_json json_obj = JSON.load(json_string) params = { table_name: @table_name, item: json_obj } dynamodb.client.put_item(params) end |
#query(hash_key_name, hash_key_value, range_key_name = nil, range_key_value = nil, expression = nil, expression_params = nil, index_name = nil, limit = nil, count = false) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 120 def query(hash_key_name, hash_key_value, range_key_name = nil, range_key_value = nil, expression = nil, expression_params = nil, index_name = nil, limit = nil, count = false) params = { table_name: table_name } if expression != nil params[:filter_expression] = expression end if index_name != nil params[:index_name] = index_name end if range_key_name != nil params[:key_condition_expression] = '#hash_key = :hash_key and #range_key = :range_key' params[:expression_attribute_names] = { '#hash_key' => hash_key_name, '#range_key' => range_key_name } params[:expression_attribute_values] = { ':hash_key' => hash_key_value, ':range_key' => range_key_value } else params[:key_condition_expression] = '#hash_key = :hash_key' params[:expression_attribute_names] = { '#hash_key' => hash_key_name } params[:expression_attribute_values] = { ':hash_key' => hash_key_value } end if expression_params != nil expression_params.each do |key, value| if key.starts_with?('#') params[:expression_attribute_names][key] = value elsif key.starts_with?(':') params[:expression_attribute_values][key] = value end end end if limit != nil params[:limit] = limit end if count params[:select] = 'COUNT' else params[:select] = 'ALL_ATTRIBUTES' end result = dynamodb.client.query(params) if count return result.count else output = [] result.items.each do |item| output.push(map(item)) end return output end end |
#scan(expression, expression_params, limit = nil, count = false) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 70 def scan(expression, expression_params, limit = nil, count = false) params = { :table_name => table_name } if expression != nil params[:filter_expression] = expression end if expression_params != nil params[:expression_attribute_names] = {} params[:expression_attribute_values] = {} expression_params.each do |key, value| if key.starts_with?('#') params[:expression_attribute_names][key] = value elsif key.starts_with?(':') params[:expression_attribute_values][key] = value end end end if limit != nil params[:limit] = limit end if count params[:select] = 'COUNT' else params[:select] = 'ALL_ATTRIBUTES' end result = dynamodb.client.scan(params) if count return result.count else output = [] result.items.each do |item| output.push(map(item)) end return output end end |