Class: DynamoDbFramework::Query

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

Defined Under Namespace

Classes: InvalidQueryError

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, partition_key:, partition_value:, index_name: nil) ⇒ Query

Returns a new instance of Query.



10
11
12
13
14
15
16
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 10

def initialize(table_name:, partition_key:, partition_value:, index_name: nil)
  @table_name = table_name
  @partition_key = partition_key
  @partition_value = partition_value
  @index_name = index_name
  @parts = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



18
19
20
21
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 18

def method_missing(name)
  @parts << { type: :field, value: name }
  self
end

Instance Method Details

#andObject



73
74
75
76
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 73

def and
  @parts << { type: :and }
  self
end

#buildObject



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
125
126
127
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 90

def build
  @expression_string = ''
  @expression_params = {}

  counter = 0
  @parts.each do |p|
    case p[:type]
      when :field
        field_param = '#' + p[:value].to_s
        @expression_string += ' ' + field_param
        @expression_params[field_param] = p[:value].to_s
      when :condition
        param_name = ':p' + counter.to_s
        counter = counter + 1
        @expression_string += ' ' + p[:expression].to_s + ' ' + param_name
        @expression_params[param_name] = clean_value(p[:value])
      when :contains
        param_name = ':p' + counter.to_s
        counter = counter + 1
        field_param = '#' + p[:field].to_s
        @expression_string += ' contains(' + field_param + ', ' + param_name + ')'
        @expression_params[field_param] = p[:field].to_s
        @expression_params[param_name] = clean_value(p[:value])
      when :exists
        field_param = '#' + p[:field].to_s
        @expression_string += ' attribute_exists(' + field_param + ')'
        @expression_params[field_param] = p[:field].to_s
      when :and
        @expression_string += ' and'
      when :or
        @expression_string += ' or'
      else
        raise 'Invalid query part'
    end
  end

  return @expression_string.strip, @expression_params
end

#clean_value(value) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 139

def clean_value(value)
  if value.is_a?(Time) || value.is_a?(DateTime)
    convert_date(value)
  else
    value
  end
end

#condition(expression:, value:) ⇒ Object



129
130
131
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 129

def condition(expression:, value:)
  @parts << { type: :condition, expression: expression, value: value }
end

#contains(value) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 53

def contains(value)
  field = @parts.last
  unless field[:type] == :field
    raise ::InvalidQueryError.new('The contains query part can only be chained to a field.')
  end
  @parts.pop
  @parts << { type: :contains, field: field[:value], value: value }
  self
end

#convert_date(value) ⇒ Object



133
134
135
136
137
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 133

def convert_date(value)
  klass = value.class
  return value.iso8601 if klass == DateTime
  return value.to_i if klass == Time
end

#eq(value) ⇒ Object



23
24
25
26
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 23

def eq(value)
  condition(expression: '=', value: value)
  self
end

#execute(store: DynamoDbFramework.default_store, limit: nil, count: false) ⇒ Object



83
84
85
86
87
88
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 83

def execute(store: DynamoDbFramework.default_store, limit: nil, count: false)
  build
  repository = DynamoDbFramework::Repository.new(store)
  repository.table_name = @table_name
  repository.query(@partition_key, @partition_value, nil, nil, @expression_string, @expression_params, @index_name, limit, count)
end

#exists?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 63

def exists?
  field = @parts.last
  unless field[:type] == :field
    raise ::InvalidQueryError.new('The exists? query part can only be chained to a field.')
  end
  @parts.pop
  @parts << { type: :exists, field: field[:value] }
  self
end

#gt(value) ⇒ Object



33
34
35
36
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 33

def gt(value)
  condition(expression: '>', value: value)
  self
end

#gt_eq(value) ⇒ Object



38
39
40
41
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 38

def gt_eq(value)
  condition(expression: '>=', value: value)
  self
end

#lt(value) ⇒ Object



43
44
45
46
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 43

def lt(value)
  condition(expression: '<', value: value)
  self
end

#lt_eq(value) ⇒ Object



48
49
50
51
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 48

def lt_eq(value)
  condition(expression: '<=', value: value)
  self
end

#not_eq(value) ⇒ Object



28
29
30
31
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 28

def not_eq(value)
  condition(expression: '<>', value: value)
  self
end

#orObject



78
79
80
81
# File 'lib/dynamodb_framework/dynamodb_query.rb', line 78

def or
  @parts << { type: :or }
  self
end