class DynamoDbFramework::Query
Public Class Methods
new(table_name:, partition_key:, partition_value:, index_name: nil)
click to toggle source
# 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
Public Instance Methods
and()
click to toggle source
# File lib/dynamodb_framework/dynamodb_query.rb, line 73 def and @parts << { type: :and } self end
build()
click to toggle source
# 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)
click to toggle source
# 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:)
click to toggle source
# File lib/dynamodb_framework/dynamodb_query.rb, line 129 def condition(expression:, value:) @parts << { type: :condition, expression: expression, value: value } end
contains(value)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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?()
click to toggle source
# 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)
click to toggle source
# File lib/dynamodb_framework/dynamodb_query.rb, line 33 def gt(value) condition(expression: '>', value: value) self end
gt_eq(value)
click to toggle source
# File lib/dynamodb_framework/dynamodb_query.rb, line 38 def gt_eq(value) condition(expression: '>=', value: value) self end
lt(value)
click to toggle source
# File lib/dynamodb_framework/dynamodb_query.rb, line 43 def lt(value) condition(expression: '<', value: value) self end
lt_eq(value)
click to toggle source
# File lib/dynamodb_framework/dynamodb_query.rb, line 48 def lt_eq(value) condition(expression: '<=', value: value) self end
method_missing(name)
click to toggle source
# File lib/dynamodb_framework/dynamodb_query.rb, line 18 def method_missing(name) @parts << { type: :field, value: name } self end
not_eq(value)
click to toggle source
# File lib/dynamodb_framework/dynamodb_query.rb, line 28 def not_eq(value) condition(expression: '<>', value: value) self end
or()
click to toggle source
# File lib/dynamodb_framework/dynamodb_query.rb, line 78 def or @parts << { type: :or } self end