Module: DynamodbRecord::Query::ClassMethods

Defined in:
lib/dynamodb_record/query.rb

Overview

Module that provides class methods for queries against the DynamoDB database.

Instance Method Summary collapse

Instance Method Details

#all(opts = {}) ⇒ Object



9
10
11
12
13
14
# File 'lib/dynamodb_record/query.rb', line 9

def all(opts = {})
  options = default_options
  options.merge!(opts.slice(:limit))
  response = client.scan(options)
  DynamodbRecord::Collection.new(response, self)
end

#where(opts = {}) ⇒ Object

search table

Parameters:

  • opts (Hash) (defaults to: {})

    Limit of record and exclusive_start_key



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dynamodb_record/query.rb', line 18

def where(opts = {})
  limit = opts.delete(:limit)
  exclusive_start_key = opts.delete(:exclusive_start_key)

  expression_attribute_names = {}
  expression_attribute_values = {}
  filter_expression = ''
  opts.each do |key, value|
    expression_attribute_names["##{key}".to_sym] = key.to_s
    expression_attribute_values[":#{key}"] = value
    filter_expression << if filter_expression.empty?
                           "##{key} = :#{key}"
                         else
                           " And ##{key} = :#{key}"
                         end
  end

  options = default_options
  options.merge!(limit:) if limit
  options.merge!(expression_attribute_names:) if expression_attribute_names.present?
  options.merge!(expression_attribute_values:) if expression_attribute_values.present?
  options.merge!(filter_expression:) if filter_expression.present?
  if exclusive_start_key.present?
    json_string_decode = Base64.urlsafe_decode64(exclusive_start_key)
    decode = JSON.parse(json_string_decode)
    options.merge!(exclusive_start_key: decode)
  end
  DynamodbRecord::Collection.new(client.scan(options), self)
end