Module: Dynamoid::Finders::ClassMethods
- Defined in:
- lib/dynamoid/finders.rb
Instance Method Summary collapse
- #_find_all(ids, options = {}) ⇒ Object
- #_find_by_id(id, options = {}) ⇒ Object
-
#find(*ids, **options) ⇒ Dynamoid::Document
Find one or many objects, specified by one id or an array of ids.
-
#find_all(ids, options = {}) ⇒ Object
Find several models at once.
-
#find_all_by_composite_key(hash_key, options = {}) ⇒ Array
Find all objects by hash and range keys.
-
#find_all_by_secondary_index(hash, options = {}) ⇒ Array
Find all objects by using local secondary or global secondary index.
-
#find_by_composite_key(hash_key, range_key, options = {}) ⇒ Object
Find one object directly by hash and range keys.
-
#find_by_id(id, options = {}) ⇒ Dynamoid::Document
Find one object directly by primary key.
-
#method_missing(method, *args) ⇒ Dynamoid::Document|Array
Find using exciting method_missing finders attributes.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Dynamoid::Document|Array
Find using exciting method_missing finders attributes. Uses criteria chains under the hood to accomplish this neatness.
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/dynamoid/finders.rb', line 296 def method_missing(method, *args) if method =~ /find/ ActiveSupport::Deprecation.warn("[Dynamoid] .#{method} is deprecated! Call .where instead of") finder = method.to_s.split('_by_').first attributes = method.to_s.split('_by_').last.split('_and_') chain = Dynamoid::Criteria::Chain.new(self) chain = chain.where({}.tap { |h| attributes.each_with_index { |attr, index| h[attr.to_sym] = args[index] } }) if finder =~ /all/ chain.all else chain.first end else super end end |
Instance Method Details
#_find_all(ids, options = {}) ⇒ Object
120 121 122 123 124 125 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 |
# File 'lib/dynamoid/finders.rb', line 120 def _find_all(ids, = {}) raise Errors::MissingRangeKey if range_key && ids.any? { |pk, sk| sk.nil? } if range_key ids = ids.map do |pk, sk| sk_casted = TypeCasting.cast_field(sk, attributes[range_key]) sk_dumped = Dumping.dump_field(sk_casted, attributes[range_key]) [pk, sk_dumped] end end = .slice(:consistent_read) items = if Dynamoid.config.backoff items = [] backoff = nil Dynamoid.adapter.read(table_name, ids, ) do |hash, has_unprocessed_items| items += hash[table_name] if has_unprocessed_items backoff ||= Dynamoid.config.build_backoff backoff.call else backoff = nil end end items else items = Dynamoid.adapter.read(table_name, ids, ) items ? items[table_name] : [] end if items.size == ids.size || ![:raise_error] items ? items.map { |i| from_database(i) } : [] else ids_list = range_key ? ids.map { |pk, sk| "(#{pk},#{sk})" } : ids.map(&:to_s) = "Couldn't find all #{name.pluralize} with primary keys [#{ids_list.join(', ')}] " += "(found #{items.size} results, but was looking for #{ids.size})" raise Errors::RecordNotFound, end end |
#_find_by_id(id, options = {}) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/dynamoid/finders.rb', line 164 def _find_by_id(id, = {}) raise Errors::MissingRangeKey if range_key && [:range_key].nil? if range_key key = [:range_key] key_casted = TypeCasting.cast_field(key, attributes[range_key]) key_dumped = Dumping.dump_field(key_casted, attributes[range_key]) [:range_key] = key_dumped end if item = Dynamoid.adapter.read(table_name, id, .slice(:range_key, :consistent_read)) from_database(item) elsif [:raise_error] primary_key = range_key ? "(#{id},#{[:range_key]})" : id = "Couldn't find #{name} with primary key #{primary_key}" raise Errors::RecordNotFound, end end |
#find(*ids, **options) ⇒ Dynamoid::Document
Find one or many objects, specified by one id or an array of ids.
By default it raises RecordNotFound
exception if at least one model isn’t found. This behavior can be changed with raise_error
option. If specified raise_error: false option then find
will not raise the exception.
When a document schema includes range key it always should be specified in find
method call. In case it’s missing MissingRangeKey
exception will be raised.
Please note that find
doesn’t preserve order of models in result when passes multiple ids.
Supported following options:
-
consistent_read
-
range_key
-
raise_error
63 64 65 66 67 68 69 |
# File 'lib/dynamoid/finders.rb', line 63 def find(*ids, **) if ids.size == 1 && !ids[0].is_a?(Array) _find_by_id(ids[0], .reverse_merge(raise_error: true)) else _find_all(ids.flatten(1), .reverse_merge(raise_error: true)) end end |
#find_all(ids, options = {}) ⇒ Object
Find several models at once.
Returns objects found by the given array of ids, either hash keys, or hash/range key combinations using BatchGetItem
.
Returns empty array if no results found.
Uses backoff specified by Dynamoid::Config.backoff
config option.
91 92 93 94 95 |
# File 'lib/dynamoid/finders.rb', line 91 def find_all(ids, = {}) ActiveSupport::Deprecation.warn('[Dynamoid] .find_all is deprecated! Call .find instead of') _find_all(ids, ) end |
#find_all_by_composite_key(hash_key, options = {}) ⇒ Array
Find all objects by hash and range keys.
216 217 218 219 220 221 222 |
# File 'lib/dynamoid/finders.rb', line 216 def find_all_by_composite_key(hash_key, = {}) ActiveSupport::Deprecation.warn('[Dynamoid] .find_all_composite_key is deprecated! Call .where instead of') Dynamoid.adapter.query(table_name, .merge(hash_value: hash_key)).flat_map { |i| i }.collect do |item| from_database(item) end end |
#find_all_by_secondary_index(hash, options = {}) ⇒ Array
Find all objects by using local secondary or global secondary index
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/dynamoid/finders.rb', line 246 def find_all_by_secondary_index(hash, = {}) ActiveSupport::Deprecation.warn('[Dynamoid] .find_all_by_secondary_index is deprecated! Call .where instead of') range = [:range] || {} hash_key_field, hash_key_value = hash.first range_key_field, range_key_value = range.first range_op_mapped = nil if range_key_field range_key_field = range_key_field.to_s range_key_op = 'eq' if range_key_field.include?('.') range_key_field, range_key_op = range_key_field.split('.', 2) end range_op_mapped = RANGE_MAP.fetch(range_key_op) end # Find the index index = find_index(hash_key_field, range_key_field) raise Dynamoid::Errors::MissingIndex, "attempted to find #{[hash_key_field, range_key_field]}" if index.nil? # query opts = { hash_key: hash_key_field.to_s, hash_value: hash_key_value, index_name: index.name } if range_key_field opts[:range_key] = range_key_field opts[range_op_mapped] = range_key_value end = opts.merge(.reject { |key, _| key == :range }) Dynamoid.adapter.query(table_name, ).flat_map { |i| i }.map do |item| from_database(item) end end |
#find_by_composite_key(hash_key, range_key, options = {}) ⇒ Object
Find one object directly by hash and range keys.
189 190 191 192 193 |
# File 'lib/dynamoid/finders.rb', line 189 def find_by_composite_key(hash_key, range_key, = {}) ActiveSupport::Deprecation.warn('[Dynamoid] .find_by_composite_key is deprecated! Call .find instead of') _find_by_id(hash_key, .merge(range_key: range_key)) end |
#find_by_id(id, options = {}) ⇒ Dynamoid::Document
Find one object directly by primary key.
113 114 115 116 117 |
# File 'lib/dynamoid/finders.rb', line 113 def find_by_id(id, = {}) ActiveSupport::Deprecation.warn('[Dynamoid] .find_by_id is deprecated! Call .find instead of', caller[1..-1]) _find_by_id(id, ) end |