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.



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

def initialize(store)
  @dynamodb = store
end

Instance Attribute Details

#dynamodbObject (readonly)

Returns the value of attribute dynamodb.



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

def dynamodb
  @dynamodb
end

#table_nameObject

Returns the value of attribute table_name.



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

def table_name
  @table_name
end

Instance Method Details

#allObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 61

def all

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

  output = []
  result.items.each do |item|
    output.push(item)
  end

  return output

end

#delete(keys) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 29

def delete(keys)

  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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 43

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

  key = {}
  key[partition_key] = partition_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 result.item

end

#put(item) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 13

def put(item)

  hash = to_hash(item)

  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



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
180
181
182
183
184
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 126

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' => partition_key_value, ':range_key' => 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' => partition_key_value }
  end

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

    return output
  end

end

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



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
119
120
121
122
123
124
# File 'lib/dynamodb_framework/dynamodb_repository.rb', line 76

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] = 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(item)
    end

    return output
  end

end