dynamoid 3.4.0 → 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +94 -3
  3. data/README.md +52 -26
  4. data/lib/dynamoid.rb +1 -0
  5. data/lib/dynamoid/adapter.rb +15 -6
  6. data/lib/dynamoid/adapter_plugin/aws_sdk_v3.rb +48 -36
  7. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/batch_get_item.rb +13 -1
  8. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb +9 -8
  9. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb +5 -4
  10. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/middleware/backoff.rb +2 -2
  11. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/middleware/limit.rb +2 -3
  12. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/middleware/start_key.rb +2 -2
  13. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/query.rb +4 -2
  14. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/scan.rb +6 -3
  15. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/table.rb +1 -0
  16. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/until_past_table_status.rb +2 -1
  17. data/lib/dynamoid/application_time_zone.rb +1 -0
  18. data/lib/dynamoid/associations.rb +182 -19
  19. data/lib/dynamoid/associations/association.rb +10 -2
  20. data/lib/dynamoid/associations/belongs_to.rb +2 -1
  21. data/lib/dynamoid/associations/has_and_belongs_to_many.rb +2 -1
  22. data/lib/dynamoid/associations/has_many.rb +2 -1
  23. data/lib/dynamoid/associations/has_one.rb +2 -1
  24. data/lib/dynamoid/associations/many_association.rb +68 -23
  25. data/lib/dynamoid/associations/single_association.rb +31 -4
  26. data/lib/dynamoid/components.rb +1 -0
  27. data/lib/dynamoid/config.rb +5 -5
  28. data/lib/dynamoid/config/backoff_strategies/constant_backoff.rb +1 -0
  29. data/lib/dynamoid/config/backoff_strategies/exponential_backoff.rb +1 -0
  30. data/lib/dynamoid/config/options.rb +1 -0
  31. data/lib/dynamoid/criteria.rb +9 -1
  32. data/lib/dynamoid/criteria/chain.rb +422 -46
  33. data/lib/dynamoid/criteria/ignored_conditions_detector.rb +3 -3
  34. data/lib/dynamoid/criteria/key_fields_detector.rb +32 -11
  35. data/lib/dynamoid/criteria/nonexistent_fields_detector.rb +3 -2
  36. data/lib/dynamoid/criteria/overwritten_conditions_detector.rb +1 -1
  37. data/lib/dynamoid/dirty.rb +119 -64
  38. data/lib/dynamoid/document.rb +125 -43
  39. data/lib/dynamoid/dumping.rb +9 -0
  40. data/lib/dynamoid/dynamodb_time_zone.rb +1 -0
  41. data/lib/dynamoid/errors.rb +2 -0
  42. data/lib/dynamoid/fields.rb +217 -36
  43. data/lib/dynamoid/fields/declare.rb +86 -0
  44. data/lib/dynamoid/finders.rb +69 -32
  45. data/lib/dynamoid/identity_map.rb +6 -0
  46. data/lib/dynamoid/indexes.rb +86 -17
  47. data/lib/dynamoid/loadable.rb +2 -2
  48. data/lib/dynamoid/log/formatter.rb +26 -0
  49. data/lib/dynamoid/middleware/identity_map.rb +1 -0
  50. data/lib/dynamoid/persistence.rb +496 -104
  51. data/lib/dynamoid/persistence/import.rb +1 -0
  52. data/lib/dynamoid/persistence/save.rb +1 -0
  53. data/lib/dynamoid/persistence/update_fields.rb +5 -2
  54. data/lib/dynamoid/persistence/update_validations.rb +18 -0
  55. data/lib/dynamoid/persistence/upsert.rb +5 -3
  56. data/lib/dynamoid/primary_key_type_mapping.rb +1 -0
  57. data/lib/dynamoid/railtie.rb +1 -0
  58. data/lib/dynamoid/tasks.rb +3 -1
  59. data/lib/dynamoid/tasks/database.rb +1 -0
  60. data/lib/dynamoid/type_casting.rb +12 -2
  61. data/lib/dynamoid/undumping.rb +8 -0
  62. data/lib/dynamoid/validations.rb +6 -1
  63. data/lib/dynamoid/version.rb +1 -1
  64. metadata +48 -74
  65. data/.coveralls.yml +0 -1
  66. data/.document +0 -5
  67. data/.gitignore +0 -74
  68. data/.rspec +0 -2
  69. data/.rubocop.yml +0 -71
  70. data/.rubocop_todo.yml +0 -55
  71. data/.travis.yml +0 -44
  72. data/Appraisals +0 -22
  73. data/Gemfile +0 -8
  74. data/Rakefile +0 -46
  75. data/Vagrantfile +0 -29
  76. data/docker-compose.yml +0 -7
  77. data/dynamoid.gemspec +0 -57
  78. data/gemfiles/rails_4_2.gemfile +0 -9
  79. data/gemfiles/rails_5_0.gemfile +0 -8
  80. data/gemfiles/rails_5_1.gemfile +0 -8
  81. data/gemfiles/rails_5_2.gemfile +0 -8
  82. data/gemfiles/rails_6_0.gemfile +0 -8
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Dynamoid
4
4
  module Config
5
+ # @private
5
6
  module BackoffStrategies
6
7
  # Truncated binary exponential backoff algorithm
7
8
  # See https://en.wikipedia.org/wiki/Exponential_backoff
@@ -4,6 +4,7 @@
4
4
  module Dynamoid
5
5
  module Config
6
6
  # Encapsulates logic for setting options.
7
+ # @private
7
8
  module Options
8
9
  # Get the defaults or initialize a new empty hash.
9
10
  #
@@ -7,13 +7,21 @@ module Dynamoid
7
7
  module Criteria
8
8
  extend ActiveSupport::Concern
9
9
 
10
+ # @private
10
11
  module ClassMethods
11
- %i[where all first last each record_limit scan_limit batch start scan_index_forward find_by_pages project].each do |meth|
12
+ %i[where consistent all first last delete_all destroy_all each record_limit scan_limit batch start scan_index_forward find_by_pages project pluck].each do |meth|
12
13
  # Return a criteria chain in response to a method that will begin or end a chain. For more information,
13
14
  # see Dynamoid::Criteria::Chain.
14
15
  #
15
16
  # @since 0.2.0
16
17
  define_method(meth) do |*args, &blk|
18
+ # Don't use keywork arguments delegating (with **kw). It works in
19
+ # different way in different Ruby versions: <= 2.6, 2.7, 3.0 and in some
20
+ # future 3.x versions. Providing that there are no downstream methods
21
+ # with keyword arguments in Chain.
22
+ #
23
+ # https://eregon.me/blog/2019/11/10/the-delegation-challenge-of-ruby27.html
24
+
17
25
  chain = Dynamoid::Criteria::Chain.new(self)
18
26
  if args
19
27
  chain.send(meth, *args, &blk)
@@ -22,26 +22,74 @@ module Dynamoid
22
22
  @consistent_read = false
23
23
  @scan_index_forward = true
24
24
 
25
- # Honor STI and :type field if it presents
26
- type = @source.inheritance_field
27
- if @source.attributes.key?(type)
28
- @query[:"#{type}.in"] = @source.deep_subclasses.map(&:name) << @source.name
29
- end
30
-
31
25
  # we should re-initialize keys detector every time we change query
32
26
  @key_fields_detector = KeyFieldsDetector.new(@query, @source)
33
27
  end
34
28
 
35
- # The workhorse method of the criteria chain. Each key in the passed in hash will become another criteria that the
36
- # ultimate query must match. A key can either be a symbol or a string, and should be an attribute name or
37
- # an attribute name with a range operator.
29
+ # Returns a chain which is a result of filtering current chain with the specified conditions.
30
+ #
31
+ # It accepts conditions in the form of a hash.
32
+ #
33
+ # Post.where(links_count: 2)
34
+ #
35
+ # A key could be either string or symbol.
36
+ #
37
+ # In order to express conditions other than equality predicates could be used.
38
+ # Predicate should be added to an attribute name to form a key +'created_at.gt' => Date.yesterday+
39
+ #
40
+ # Currently supported following predicates:
41
+ # - +gt+ - greater than
42
+ # - +gte+ - greater or equal
43
+ # - +lt+ - less than
44
+ # - +lte+ - less or equal
45
+ # - +ne+ - not equal
46
+ # - +between+ - an attribute value is greater than the first value and less than the second value
47
+ # - +in+ - check an attribute in a list of values
48
+ # - +begins_with+ - check for a prefix in string
49
+ # - +contains+ - check substring or value in a set or array
50
+ # - +not_contains+ - check for absence of substring or a value in set or array
51
+ # - +null+ - attribute doesn't exists in an item
52
+ # - +not_null+ - attribute exists in an item
53
+ #
54
+ # All the predicates match operators supported by DynamoDB's
55
+ # {ComparisonOperator}[https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html#DDB-Type-Condition-ComparisonOperator]
56
+ #
57
+ # Post.where('size.gt' => 1000)
58
+ # Post.where('size.gte' => 1000)
59
+ # Post.where('size.lt' => 35000)
60
+ # Post.where('size.lte' => 35000)
61
+ # Post.where('author.ne' => 'John Doe')
62
+ # Post.where('created_at.between' => [Time.now - 3600, Time.now])
63
+ # Post.where('category.in' => ['tech', 'fashion'])
64
+ # Post.where('title.begins_with' => 'How long')
65
+ # Post.where('tags.contains' => 'Ruby')
66
+ # Post.where('tags.not_contains' => 'Ruby on Rails')
67
+ # Post.where('legacy_attribute.null' => true)
68
+ # Post.where('optional_attribute.not_null' => true)
69
+ #
70
+ # There are some limitations for a sort key. Only following predicates
71
+ # are supported - +gt+, +gte+, +lt+, +lte+, +between+, +begins_with+.
72
+ #
73
+ # +where+ without argument will return the current chain.
74
+ #
75
+ # Multiple calls can be chained together and conditions will be merged:
76
+ #
77
+ # Post.where('size.gt' => 1000).where('title' => 'some title')
78
+ #
79
+ # It's equivalent to:
80
+ #
81
+ # Post.where('size.gt' => 1000, 'title' => 'some title')
82
+ #
83
+ # But only one condition can be specified for a certain attribute. The
84
+ # last specified condition will override all the others. Only condition
85
+ # 'size.lt' => 200 will be used in following examples:
38
86
  #
39
- # @example A simple criteria
40
- # where(:name => 'Josh')
87
+ # Post.where('size.gt' => 100, 'size.lt' => 200)
88
+ # Post.where('size.gt' => 100).where('size.lt' => 200)
41
89
  #
42
- # @example A more complicated criteria
43
- # where(:name => 'Josh', 'created_at.gt' => DateTime.now - 1.day)
90
+ # Internally +where+ performs either +Scan+ or +Query+ operation.
44
91
  #
92
+ # @return [Dynamoid::Criteria::Chain]
45
93
  # @since 0.2.0
46
94
  def where(args)
47
95
  detector = IgnoredConditionsDetector.new(args)
@@ -62,11 +110,18 @@ module Dynamoid
62
110
  query.update(args.symbolize_keys)
63
111
 
64
112
  # we should re-initialize keys detector every time we change query
65
- @key_fields_detector = KeyFieldsDetector.new(@query, @source)
113
+ @key_fields_detector = KeyFieldsDetector.new(@query, @source, forced_index_name: @forced_index_name)
66
114
 
67
115
  self
68
116
  end
69
117
 
118
+ # Turns on strongly consistent reads.
119
+ #
120
+ # By default reads are eventually consistent.
121
+ #
122
+ # Post.where('size.gt' => 1000).consistent
123
+ #
124
+ # @return [Dynamoid::Criteria::Chain]
70
125
  def consistent
71
126
  @consistent_read = true
72
127
  self
@@ -74,11 +129,39 @@ module Dynamoid
74
129
 
75
130
  # Returns all the records matching the criteria.
76
131
  #
132
+ # Since +where+ and most of the other methods return a +Chain+
133
+ # the only way to get a result as a collection is to call the +all+
134
+ # method. It returns +Enumerator+ which could be used directly or
135
+ # transformed into +Array+
136
+ #
137
+ # Post.all # => Enumerator
138
+ # Post.where(links_count: 2).all # => Enumerator
139
+ # Post.where(links_count: 2).all.to_a # => Array
140
+ #
141
+ # When the result set is too large DynamoDB divides it into separate
142
+ # pages. While an enumerator iterates over the result models each page
143
+ # is loaded lazily. So even an extra large result set can be loaded and
144
+ # processed with considerably small memory footprint and throughput
145
+ # consumption.
146
+ #
147
+ # @return [Enumerator::Lazy]
77
148
  # @since 0.2.0
78
149
  def all
79
150
  records
80
151
  end
81
152
 
153
+ # Returns the actual number of items in a table matching the criteria.
154
+ #
155
+ # Post.where(links_count: 2).count
156
+ #
157
+ # Internally it uses either `Scan` or `Query` DynamoDB's operation so it
158
+ # costs like all the matching items were read from a table.
159
+ #
160
+ # The only difference is that items are read by DynemoDB but not actually
161
+ # loaded on the client side. DynamoDB returns only count of items after
162
+ # filtering.
163
+ #
164
+ # @return [Integer]
82
165
  def count
83
166
  if @key_fields_detector.key_present?
84
167
  count_via_query
@@ -87,27 +170,72 @@ module Dynamoid
87
170
  end
88
171
  end
89
172
 
90
- # Returns the last fetched record matched the criteria
91
- # Enumerable doesn't implement `last`, only `first`
92
- # So we have to implement it ourselves
173
+ # Returns the first item matching the criteria.
174
+ #
175
+ # Post.where(links_count: 2).first
176
+ #
177
+ # Applies `record_limit(1)` to ensure only a single record is fetched
178
+ # when no non-key conditions are present and `scan_limit(1)` when no
179
+ # conditions are present at all.
180
+ #
181
+ # If used without criteria it just returns the first item of some
182
+ # arbitrary order.
93
183
  #
184
+ # Post.first
185
+ #
186
+ # @return [Model|nil]
187
+ def first(*args)
188
+ n = args.first || 1
189
+
190
+ return scan_limit(n).to_a.first(*args) if @query.blank?
191
+ return super if @key_fields_detector.non_key_present?
192
+
193
+ record_limit(n).to_a.first(*args)
194
+ end
195
+
196
+ # Returns the last item matching the criteria.
197
+ #
198
+ # Post.where(links_count: 2).last
199
+ #
200
+ # DynamoDB doesn't support ordering by some arbitrary attribute except a
201
+ # sort key. So this method is mostly useful during development and
202
+ # testing.
203
+ #
204
+ # If used without criteria it just returns the last item of some arbitrary order.
205
+ #
206
+ # Post.last
207
+ #
208
+ # It isn't efficient from the performance point of view as far as it reads and
209
+ # loads all the filtered items from DynamoDB.
210
+ #
211
+ # @return [Model|nil]
94
212
  def last
95
213
  all.to_a.last
96
214
  end
97
215
 
98
- # Destroys all the records matching the criteria.
216
+ # Deletes all the items matching the criteria.
217
+ #
218
+ # Post.where(links_count: 2).delete_all
219
+ #
220
+ # If called without criteria then it deletes all the items in a table.
99
221
  #
222
+ # Post.delete_all
223
+ #
224
+ # It loads all the items either with +Scan+ or +Query+ operation and
225
+ # deletes them in batch with +BatchWriteItem+ operation. +BatchWriteItem+
226
+ # is limited by request size and items count so it's quite possible the
227
+ # deletion will require several +BatchWriteItem+ calls.
100
228
  def delete_all
101
229
  ids = []
102
230
  ranges = []
103
231
 
104
232
  if @key_fields_detector.key_present?
105
- Dynamoid.adapter.query(source.table_name, range_query).flat_map{ |i| i }.collect do |hash|
233
+ Dynamoid.adapter.query(source.table_name, range_query).flat_map { |i| i }.collect do |hash|
106
234
  ids << hash[source.hash_key.to_sym]
107
235
  ranges << hash[source.range_key.to_sym] if source.range_key
108
236
  end
109
237
  else
110
- Dynamoid.adapter.scan(source.table_name, scan_query, scan_opts).flat_map{ |i| i }.collect do |hash|
238
+ Dynamoid.adapter.scan(source.table_name, scan_query, scan_opts).flat_map { |i| i }.collect do |hash|
111
239
  ids << hash[source.hash_key.to_sym]
112
240
  ranges << hash[source.range_key.to_sym] if source.range_key
113
241
  end
@@ -117,53 +245,259 @@ module Dynamoid
117
245
  end
118
246
  alias destroy_all delete_all
119
247
 
120
- # The record limit is the limit of evaluated records returned by the
121
- # query or scan.
248
+ # Set the record limit.
249
+ #
250
+ # The record limit is the limit of evaluated items returned by the
251
+ # +Query+ or +Scan+. In other words it's how many items should be
252
+ # returned in response.
253
+ #
254
+ # Post.where(links_count: 2).record_limit(1000) # => 1000 models
255
+ # Post.record_limit(1000) # => 1000 models
256
+ #
257
+ # It could be very inefficient in terms of HTTP requests in pathological
258
+ # cases. DynamoDB doesn't support out of the box the limits for items
259
+ # count after filtering. So it's possible to make a lot of HTTP requests
260
+ # to find items matching criteria and skip not matching. It means that
261
+ # the cost (read capacity units) is unpredictable.
262
+ #
263
+ # Because of such issues with performance and cost it's mostly useful in
264
+ # development and testing.
265
+ #
266
+ # When called without criteria it works like +scan_limit+.
267
+ #
268
+ # @return [Dynamoid::Criteria::Chain]
122
269
  def record_limit(limit)
123
270
  @record_limit = limit
124
271
  self
125
272
  end
126
273
 
127
- # The scan limit which is the limit of records that DynamoDB will
128
- # internally query or scan. This is different from the record limit
129
- # as with filtering DynamoDB may look at N scanned records but return 0
130
- # records if none pass the filter.
274
+ # Set the scan limit.
275
+ #
276
+ # The scan limit is the limit of records that DynamoDB will internally
277
+ # read with +Query+ or +Scan+. It's different from the record limit as
278
+ # with filtering DynamoDB may look at N scanned items but return 0
279
+ # items if none passes the filter. So it can return less items than was
280
+ # specified with the limit.
281
+ #
282
+ # Post.where(links_count: 2).scan_limit(1000) # => 850 models
283
+ # Post.scan_limit(1000) # => 1000 models
284
+ #
285
+ # By contrast with +record_limit+ the cost (read capacity units) and
286
+ # performance is predictable.
287
+ #
288
+ # When called without criteria it works like +record_limit+.
289
+ #
290
+ # @return [Dynamoid::Criteria::Chain]
131
291
  def scan_limit(limit)
132
292
  @scan_limit = limit
133
293
  self
134
294
  end
135
295
 
296
+ # Set the batch size.
297
+ #
298
+ # The batch size is a number of items which will be lazily loaded one by one.
299
+ # When the batch size is set then items will be loaded batch by batch of
300
+ # the specified size instead of relying on the default paging mechanism
301
+ # of DynamoDB.
302
+ #
303
+ # Post.where(links_count: 2).batch(1000).all.each do |post|
304
+ # # process a post
305
+ # end
306
+ #
307
+ # It's useful to limit memory usage or throughput consumption
308
+ #
309
+ # @return [Dynamoid::Criteria::Chain]
136
310
  def batch(batch_size)
137
311
  @batch_size = batch_size
138
312
  self
139
313
  end
140
314
 
315
+ # Set the start item.
316
+ #
317
+ # When the start item is set the items will be loaded starting right
318
+ # after the specified item.
319
+ #
320
+ # Post.where(links_count: 2).start(post)
321
+ #
322
+ # It can be used to implement an own pagination mechanism.
323
+ #
324
+ # Post.where(author_id: author_id).start(last_post).scan_limit(50)
325
+ #
326
+ # The specified start item will not be returned back in a result set.
327
+ #
328
+ # Actually it doesn't need all the item attributes to start - an item may
329
+ # have only the primary key attributes (partition and sort key if it's
330
+ # declared).
331
+ #
332
+ # Post.where(links_count: 2).start(Post.new(id: id))
333
+ #
334
+ # It also supports a +Hash+ argument with the keys attributes - a
335
+ # partition key and a sort key (if it's declared).
336
+ #
337
+ # Post.where(links_count: 2).start(id: id)
338
+ #
339
+ # @return [Dynamoid::Criteria::Chain]
141
340
  def start(start)
142
341
  @start = start
143
342
  self
144
343
  end
145
344
 
345
+ # Reverse the sort order.
346
+ #
347
+ # By default the sort order is ascending (by the sort key value). Set a
348
+ # +false+ value to reverse the order.
349
+ #
350
+ # Post.where(id: id, 'views_count.gt' => 1000).scan_index_forward(false)
351
+ #
352
+ # It works only for queries with a partition key condition e.g. +id:
353
+ # 'some-id'+ which internally performs +Query+ operation.
354
+ #
355
+ # @return [Dynamoid::Criteria::Chain]
146
356
  def scan_index_forward(scan_index_forward)
147
357
  @scan_index_forward = scan_index_forward
148
358
  self
149
359
  end
150
360
 
151
- # Allows you to use the results of a search as an enumerable over the results found.
361
+ # Force the index name to use for queries.
362
+ #
363
+ # By default allows the library to select the most appropriate index.
364
+ # Sometimes you have more than one index which will fulfill your query's
365
+ # needs. When this case occurs you may want to force an order. This occurs
366
+ # when you are searching by hash key, but not specifying a range key.
367
+ #
368
+ # class Comment
369
+ # include Dynamoid::Document
370
+ #
371
+ # table key: :post_id
372
+ # range_key :author_id
373
+ #
374
+ # field :post_date, :datetime
375
+ #
376
+ # global_secondary_index name: :time_sorted_comments, hash_key: :post_id, range_key: post_date, projected_attributes: :all
377
+ # end
378
+ #
379
+ #
380
+ # Comment.where(post_id: id).with_index(:time_sorted_comments).scan_index_forward(false)
381
+ #
382
+ # @return [Dynamoid::Criteria::Chain]
383
+ def with_index(index_name)
384
+ raise Dynamoid::Errors::InvalidIndex, "Unknown index #{index_name}" unless @source.find_index_by_name(index_name)
385
+
386
+ @forced_index_name = index_name
387
+ @key_fields_detector = KeyFieldsDetector.new(@query, @source, forced_index_name: index_name)
388
+ self
389
+ end
390
+
391
+ # Allows to use the results of a search as an enumerable over the results
392
+ # found.
393
+ #
394
+ # Post.each do |post|
395
+ # end
396
+ #
397
+ # Post.all.each do |post|
398
+ # end
399
+ #
400
+ # Post.where(links_count: 2).each do |post|
401
+ # end
402
+ #
403
+ # It works similar to the +all+ method so results are loaded lazily.
152
404
  #
153
405
  # @since 0.2.0
154
406
  def each(&block)
155
407
  records.each(&block)
156
408
  end
157
409
 
410
+ # Iterates over the pages returned by DynamoDB.
411
+ #
412
+ # DynamoDB has its own paging machanism and divides a large result set
413
+ # into separate pages. The +find_by_pages+ method provides access to
414
+ # these native DynamoDB pages.
415
+ #
416
+ # The pages are loaded lazily.
417
+ #
418
+ # Post.where('views_count.gt' => 1000).find_by_pages do |posts, options|
419
+ # # process posts
420
+ # end
421
+ #
422
+ # It passes as block argument an +Array+ of models and a Hash with options.
423
+ #
424
+ # Options +Hash+ contains only one option +:last_evaluated_key+. The last
425
+ # evaluated key is a Hash with key attributes of the last item processed by
426
+ # DynamoDB. It can be used to resume querying using the +start+ method.
427
+ #
428
+ # posts, options = Post.where('views_count.gt' => 1000).find_by_pages.first
429
+ # last_key = options[:last_evaluated_key]
430
+ #
431
+ # # ...
432
+ #
433
+ # Post.where('views_count.gt' => 1000).start(last_key).find_by_pages do |posts, options|
434
+ # end
435
+ #
436
+ # If it's called without a block then it returns an +Enumerator+.
437
+ #
438
+ # enum = Post.where('views_count.gt' => 1000).find_by_pages
439
+ #
440
+ # enum.each do |posts, options|
441
+ # # process posts
442
+ # end
443
+ #
444
+ # @return [Enumerator::Lazy]
158
445
  def find_by_pages(&block)
159
446
  pages.each(&block)
160
447
  end
161
448
 
449
+ # Select only specified fields.
450
+ #
451
+ # It takes one or more field names and returns a collection of models with only
452
+ # these fields set.
453
+ #
454
+ # Post.where('views_count.gt' => 1000).select(:title)
455
+ # Post.where('views_count.gt' => 1000).select(:title, :created_at)
456
+ # Post.select(:id)
457
+ #
458
+ # It can be used to avoid loading large field values and to decrease a
459
+ # memory footprint.
460
+ #
461
+ # @return [Dynamoid::Criteria::Chain]
162
462
  def project(*fields)
163
463
  @project = fields.map(&:to_sym)
164
464
  self
165
465
  end
166
466
 
467
+ # Select only specified fields.
468
+ #
469
+ # It takes one or more field names and returns an array of either values
470
+ # or arrays of values.
471
+ #
472
+ # Post.pluck(:id) # => ['1', '2']
473
+ # Post.pluck(:title, :title) # => [['1', 'Title #1'], ['2', 'Title#2']]
474
+ #
475
+ # Post.where('views_count.gt' => 1000).pluck(:title)
476
+ #
477
+ # There are some differences between +pluck+ and +project+. +pluck+
478
+ # - doesn't instantiate models
479
+ # - it isn't chainable and returns +Array+ instead of +Chain+
480
+ #
481
+ # It deserializes values if a field type isn't supported by DynamoDB natively.
482
+ #
483
+ # It can be used to avoid loading large field values and to decrease a
484
+ # memory footprint.
485
+ #
486
+ # @return [Array]
487
+ def pluck(*args)
488
+ fields = args.map(&:to_sym)
489
+ @project = fields
490
+
491
+ if fields.many?
492
+ items.map do |item|
493
+ fields.map { |key| Undumping.undump_field(item[key], source.attributes[key]) }
494
+ end.to_a
495
+ else
496
+ key = fields.first
497
+ items.map { |item| Undumping.undump_field(item[key], source.attributes[key]) }.to_a
498
+ end
499
+ end
500
+
167
501
  private
168
502
 
169
503
  # The actual records referenced by the association.
@@ -172,7 +506,12 @@ module Dynamoid
172
506
  #
173
507
  # @since 0.2.0
174
508
  def records
175
- pages.lazy.flat_map { |i| i }
509
+ pages.lazy.flat_map { |items, _| items }
510
+ end
511
+
512
+ # Raw items like they are stored before type casting
513
+ def items
514
+ raw_pages.lazy.flat_map { |items, _| items }
176
515
  end
177
516
 
178
517
  # Arrays of records, sized based on the actual pages produced by DynamoDB
@@ -181,11 +520,19 @@ module Dynamoid
181
520
  #
182
521
  # @since 3.1.0
183
522
  def pages
523
+ raw_pages.lazy.map do |items, options|
524
+ models = items.map { |i| source.from_database(i) }
525
+ [models, options]
526
+ end.each
527
+ end
528
+
529
+ # Pages of items before type casting
530
+ def raw_pages
184
531
  if @key_fields_detector.key_present?
185
- pages_via_query
532
+ raw_pages_via_query
186
533
  else
187
534
  issue_scan_warning if Dynamoid::Config.warn_on_scan && query.present?
188
- pages_via_scan
535
+ raw_pages_via_scan
189
536
  end
190
537
  end
191
538
 
@@ -194,13 +541,12 @@ module Dynamoid
194
541
  # @return [Enumerator] an iterator of the found pages. An array of records
195
542
  #
196
543
  # @since 3.1.0
197
- def pages_via_query
544
+ def raw_pages_via_query
198
545
  Enumerator.new do |y|
199
546
  Dynamoid.adapter.query(source.table_name, range_query).each do |items, metadata|
200
- page = items.map { |h| source.from_database(h) }
201
547
  options = metadata.slice(:last_evaluated_key)
202
548
 
203
- y.yield page, options
549
+ y.yield items, options
204
550
  end
205
551
  end
206
552
  end
@@ -210,13 +556,12 @@ module Dynamoid
210
556
  # @return [Enumerator] an iterator of the found pages. An array of records
211
557
  #
212
558
  # @since 3.1.0
213
- def pages_via_scan
559
+ def raw_pages_via_scan
214
560
  Enumerator.new do |y|
215
561
  Dynamoid.adapter.scan(source.table_name, scan_query, scan_opts).each do |items, metadata|
216
- page = items.map { |h| source.from_database(h) }
217
562
  options = metadata.slice(:last_evaluated_key)
218
563
 
219
- y.yield page, options
564
+ y.yield items, options
220
565
  end
221
566
  end
222
567
  end
@@ -300,6 +645,13 @@ module Dynamoid
300
645
 
301
646
  def range_query
302
647
  opts = {}
648
+ query = self.query
649
+
650
+ # Honor STI and :type field if it presents
651
+ if @source.attributes.key?(@source.inheritance_field) &&
652
+ @key_fields_detector.hash_key.to_sym != @source.inheritance_field.to_sym
653
+ query.update(sti_condition)
654
+ end
303
655
 
304
656
  # Add hash key
305
657
  opts[:hash_key] = @key_fields_detector.hash_key
@@ -307,15 +659,7 @@ module Dynamoid
307
659
 
308
660
  # Add range key
309
661
  if @key_fields_detector.range_key
310
- opts[:range_key] = @key_fields_detector.range_key
311
- if query[@key_fields_detector.range_key].present?
312
- value = type_cast_condition_parameter(@key_fields_detector.range_key, query[@key_fields_detector.range_key])
313
- opts.update(range_eq: value)
314
- end
315
-
316
- query.keys.select { |k| k.to_s =~ /^#{@key_fields_detector.range_key}\./ }.each do |key|
317
- opts.merge!(range_hash(key))
318
- end
662
+ add_range_key_to_range_query(query, opts)
319
663
  end
320
664
 
321
665
  (query.keys.map(&:to_sym) - [@key_fields_detector.hash_key.to_sym, @key_fields_detector.range_key.try(:to_sym)])
@@ -332,7 +676,19 @@ module Dynamoid
332
676
  opts.merge(query_opts).merge(consistent_opts)
333
677
  end
334
678
 
335
- # TODO casting should be operator aware
679
+ def add_range_key_to_range_query(query, opts)
680
+ opts[:range_key] = @key_fields_detector.range_key
681
+ if query[@key_fields_detector.range_key].present?
682
+ value = type_cast_condition_parameter(@key_fields_detector.range_key, query[@key_fields_detector.range_key])
683
+ opts.update(range_eq: value)
684
+ end
685
+
686
+ query.keys.select { |k| k.to_s =~ /^#{@key_fields_detector.range_key}\./ }.each do |key|
687
+ opts.merge!(range_hash(key))
688
+ end
689
+ end
690
+
691
+ # TODO: casting should be operator aware
336
692
  # e.g. for NULL operator value should be boolean
337
693
  # and isn't related to an attribute own type
338
694
  def type_cast_condition_parameter(key, value)
@@ -393,6 +749,13 @@ module Dynamoid
393
749
  end
394
750
 
395
751
  def scan_query
752
+ query = self.query
753
+
754
+ # Honor STI and :type field if it presents
755
+ if sti_condition
756
+ query.update(sti_condition)
757
+ end
758
+
396
759
  {}.tap do |opts|
397
760
  query.keys.map(&:to_sym).each do |key|
398
761
  if key.to_s.include?('.')
@@ -407,6 +770,7 @@ module Dynamoid
407
770
 
408
771
  def scan_opts
409
772
  opts = {}
773
+ opts[:index_name] = @key_fields_detector.index_name if @key_fields_detector.index_name
410
774
  opts[:record_limit] = @record_limit if @record_limit
411
775
  opts[:scan_limit] = @scan_limit if @scan_limit
412
776
  opts[:batch_size] = @batch_size if @batch_size
@@ -415,6 +779,18 @@ module Dynamoid
415
779
  opts[:project] = @project
416
780
  opts
417
781
  end
782
+
783
+ def sti_condition
784
+ condition = {}
785
+ type = @source.inheritance_field
786
+
787
+ if @source.attributes.key?(type)
788
+ class_names = @source.deep_subclasses.map(&:name) << @source.name
789
+ condition[:"#{type}.in"] = class_names
790
+ end
791
+
792
+ condition
793
+ end
418
794
  end
419
795
  end
420
796
  end