Module: Dynamoid::Finders::ClassMethods
- Defined in:
- lib/dynamoid/finders.rb
Instance Method Summary collapse
-
#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.
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.
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/dynamoid/finders.rb', line 291 def method_missing(method, *args) # Cannot use Symbol#start_with? because it was introduced in Ruby 2.7, but we support Ruby >= 2.3 if method.to_s.start_with?('find') Dynamoid.deprecator.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.include?('all') chain.all else chain.first end else super end end |
Instance Method Details
#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
52 53 54 55 56 57 58 |
# File 'lib/dynamoid/finders.rb', line 52 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.
80 81 82 83 84 |
# File 'lib/dynamoid/finders.rb', line 80 def find_all(ids, = {}) Dynamoid.deprecator.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.
209 210 211 212 213 214 215 |
# File 'lib/dynamoid/finders.rb', line 209 def find_all_by_composite_key(hash_key, = {}) Dynamoid.deprecator.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
239 240 241 242 243 244 245 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 |
# File 'lib/dynamoid/finders.rb', line 239 def find_all_by_secondary_index(hash, = {}) Dynamoid.deprecator.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 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 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 query_key_conditions = {} query_key_conditions[hash_key_field.to_sym] = [[:eq, hash_key_value]] if range_key_field query_key_conditions[range_key_field.to_sym] = [[range_key_op.to_sym, range_key_value]] end query_non_key_conditions = .except(*Dynamoid::AdapterPlugin::AwsSdkV3::Query::OPTIONS_KEYS) .except(:range) .symbolize_keys = .slice(*Dynamoid::AdapterPlugin::AwsSdkV3::Query::OPTIONS_KEYS) [:index_name] = index.name Dynamoid.adapter.query(table_name, query_key_conditions, query_non_key_conditions, ) .flat_map { |i| i } .map { |item| from_database(item) } end |
#find_by_composite_key(hash_key, range_key, options = {}) ⇒ Object
Find one object directly by hash and range keys.
182 183 184 185 186 |
# File 'lib/dynamoid/finders.rb', line 182 def find_by_composite_key(hash_key, range_key, = {}) Dynamoid.deprecator.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.
102 103 104 105 106 |
# File 'lib/dynamoid/finders.rb', line 102 def find_by_id(id, = {}) Dynamoid.deprecator.warn('[Dynamoid] .find_by_id is deprecated! Call .find instead of') _find_by_id(id, ) end |