lib/dynamoid/criteria/chain.rb in dynamoid-2.0.0 vs lib/dynamoid/criteria/chain.rb in dynamoid-2.1.0

- old
+ new

@@ -159,11 +159,14 @@ # # @since 0.2.0 def records_via_scan if Dynamoid::Config.warn_on_scan Dynamoid.logger.warn 'Queries without an index are forced to use scan and are generally much slower than indexed queries!' - Dynamoid.logger.warn "You can index this query by adding this to #{source.to_s.downcase}.rb: index [#{query.keys.sort.collect{|name| ":#{name}"}.join(', ')}]" + Dynamoid.logger.warn "You can index this query by adding index declaration to #{source.to_s.downcase}.rb:" + Dynamoid.logger.warn "* global_secondary_index hash_key: 'some-name', range_key: 'some-another-name'" + Dynamoid.logger.warn "* local_secondary_indexe range_key: 'some-name'" + Dynamoid.logger.warn "Not indexed attributes: #{query.keys.sort.collect{|name| ":#{name}"}.join(', ')}" end Enumerator.new do |yielder| Dynamoid.adapter.scan(source.table_name, scan_query, scan_opts).each do |hash| yielder.yield source.from_database(hash) @@ -194,10 +197,12 @@ def field_hash(key) name, operation = key.to_s.split('.') val = type_cast_condition_parameter(name, query[key]) hash = case operation + when 'ne' + { ne: val } when 'gt' { gt: val } when 'lt' { lt: val } when 'gte' @@ -256,10 +261,12 @@ opts.merge(query_opts).merge(consistent_opts) end def type_cast_condition_parameter(key, value) + return value if [:array, :set].include?(source.attributes[key.to_sym][:type]) + if !value.respond_to?(:to_ary) source.dump_field(value, source.attributes[key.to_sym]) else value.to_ary.map { |el| source.dump_field(el, source.attributes[key.to_sym]) } end @@ -292,11 +299,11 @@ # See if can use any global secondary index # Chooses the first GSI found that can be utilized for the query # But only do so if projects ALL attributes otherwise we won't # get back full data source.global_secondary_indexes.each do |_, gsi| - next unless query_keys.include?(gsi.hash_key.to_s) && gsi.projected_attributes == :all + next unless query.keys.map(&:to_s).include?(gsi.hash_key.to_s) && gsi.projected_attributes == :all @hash_key = gsi.hash_key @range_key = gsi.range_key @index_name = gsi.name return true end @@ -307,36 +314,37 @@ # Start key needs to be set up based on the index utilized # If using a secondary index then we must include the index's composite key # as well as the tables composite key. def start_key + return @start if @start.is_a?(Hash) hash_key = @hash_key || source.hash_key range_key = @range_key || source.range_key key = {} - key[:hash_key_element] = type_cast_condition_parameter(hash_key, @start.send(hash_key)) - key[:range_key_element] = type_cast_condition_parameter(range_key, @start.send(range_key)) if range_key - - # Add table composite keys if differ from secondary index used composite key + key[hash_key] = type_cast_condition_parameter(hash_key, @start.send(hash_key)) + if range_key + key[range_key] = type_cast_condition_parameter(range_key, @start.send(range_key)) + end + # Add table composite keys if they differ from secondary index used composite key if hash_key != source.hash_key - key[:table_hash_key_element] = type_cast_condition_parameter(source.hash_key, @start.hash_key) + key[source.hash_key] = type_cast_condition_parameter(source.hash_key, @start.hash_key) end if source.range_key && range_key != source.range_key - key[:table_range_key_element] = type_cast_condition_parameter(source.range_key, @start.range_value) + key[source.range_key] = type_cast_condition_parameter(source.range_key, @start.range_value) end - key end def query_opts opts = {} opts[:index_name] = @index_name if @index_name opts[:select] = 'ALL_ATTRIBUTES' opts[:record_limit] = @record_limit if @record_limit opts[:scan_limit] = @scan_limit if @scan_limit opts[:batch_size] = @batch_size if @batch_size - opts[:next_token] = start_key if @start + opts[:exclusive_start_key] = start_key if @start opts[:scan_index_forward] = @scan_index_forward opts end def scan_query @@ -355,10 +363,10 @@ def scan_opts opts = {} opts[:record_limit] = @record_limit if @record_limit opts[:scan_limit] = @scan_limit if @scan_limit opts[:batch_size] = @batch_size if @batch_size - opts[:next_token] = start_key if @start + opts[:exclusive_start_key] = start_key if @start opts[:consistent_read] = true if @consistent_read opts end end