Class: DynamoDbFramework::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamodb_framework/dynamodb_repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ Repository

Returns a new instance of Repository.



10
11
12
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 10

def initialize(store)
  @dynamodb = store
end

Instance Attribute Details

#dynamodbObject (readonly)

Returns the value of attribute dynamodb.



7
8
9
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 7

def dynamodb
  @dynamodb
end

#table_nameObject

Returns the value of attribute table_name.



8
9
10
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 8

def table_name
  @table_name
end

Instance Method Details

#allObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 91

def all

  result = dynamodb.client.scan({
                                    :table_name => @table_name
                                })

  output = []
  result.items.each do |item|
    hash_helper.hash_kit.indifferent!(item)
    output.push(item)
  end

  return output

end

#delete(keys) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 36

def delete(keys)

  params =
      {
          table_name: @table_name,
          key: keys
      }

  dynamodb.client.delete_item(params)

  return true

end

#delete_item(partition_key:, partition_key_value:, range_key: nil, range_key_value: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 50

def delete_item(partition_key:, partition_key_value:, range_key: nil, range_key_value: nil)

  keys = {
      partition_key.to_s => clean_value(partition_key_value)
  }

  if range_key != nil
    keys[range_key.to_s] = clean_value(range_key_value)
  end

  params =
      {
          table_name: @table_name,
          key: keys
      }

  dynamodb.client.delete_item(params)

  return true

end

#get_by_key(partition_key, partition_value, range_key = nil, range_value = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 72

def get_by_key(partition_key, partition_value, range_key = nil, range_value = nil)

  key = {}
  key[partition_key] = clean_value(partition_value)
  if(range_key != nil)
    key[range_key] = clean_value(range_value)
  end

  params = {
      table_name: table_name,
      key: key
  }

  result = dynamodb.client.get_item(params)
  hash_helper.hash_kit.indifferent!(result.item)
  return result.item

end

#put(item) ⇒ Object

Store the hash of an object to the dynamodb table Note : [DateTime] attributes will be stored as an ISO8601 string

[Time] attributes will be stored as an Epoch Int

The intent is that if you need to sort in dynamo by dates, then make sure you use a [Time] type. The Epoch int allows you to compare properly as comparing date strings are not reliable.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 19

def put(item)
  hash = to_hash(item)

  clean_hash(hash)

  params =
      {
          table_name: @table_name,
          item: hash
      }

  dynamodb.client.put_item(params)

  return true

end

#query(partition_key_name, partition_key_value, range_key_name = nil, range_key_value = nil, expression = nil, expression_params = nil, index_name = nil, limit = nil, count = false) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 158

def query(partition_key_name, partition_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] = '#partition_key = :partition_key and #range_key = :range_key'
    params[:expression_attribute_names] = { '#partition_key' => partition_key_name, '#range_key' => range_key_name }
    params[:expression_attribute_values] = { ':partition_key' => clean_value(partition_key_value), ':range_key' => clean_value(range_key_value) }
  else
    params[:key_condition_expression] = '#partition_key = :partition_key'
    params[:expression_attribute_names] = { '#partition_key' => partition_key_name }
    params[:expression_attribute_values] = { ':partition_key' => clean_value(partition_key_value) }
  end

  if expression_params != nil
    expression_params.each do |key, value|
      if key[0] == '#'
        params[:expression_attribute_names][key] = clean_value(value)
      elsif key[0] == ':'
        params[:expression_attribute_values][key] = clean_value(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|
      hash_helper.hash_kit.indifferent!(item)
      output.push(item)
    end

    return output
  end

end

#scan(expression, expression_params, limit = nil, count = false) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
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
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 107

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[0] == '#'
        params[:expression_attribute_names][key] = value
      elsif key[0] == ':'
        params[:expression_attribute_values][key] = clean_value(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|
      hash_helper.hash_kit.indifferent!(item)
      output.push(item)
    end

    return output
  end

end