aws / aws-record-ruby

Official repository for the aws-record gem, an abstraction for Amazon DynamoDB.
Apache License 2.0
321 stars 42 forks source link

Support queries yielding heterogeneous results #108

Closed alextwoods closed 4 years ago

alextwoods commented 4 years ago

Issue: #107

Description of changes: Add #multi_model_filter to BuildableSearch which takes either a passed Proc/lambda or a block. Given the raw item data that model_filter returns the class (or nil to skip) to use for that item.

Example Usage

class Model_A
  include Aws::Record
  set_table_name(TABLE_NAME)

  string_attr :uuid, hash_key: true
  string_attr :class_name, range_key: true

  string_attr :attr_a
end

class Model_B
  include Aws::Record
  set_table_name(TABLE_NAME)

  string_attr :uuid, hash_key: true
  string_attr :class_name, range_key: true

  string_attr :attr_b
end

# use multi_model_filter to create a query on TABLE_NAME
items = Model_A.build_scan.multi_model_filter do |raw_item_attributes|
  case raw_item_attributes['class_name']
  when "A" then Model_A
  when "B" then Model_B
  else
    nil
  end
end.complete!

Of note:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

alextwoods commented 4 years ago

Was out last week - but will pick this back up this week and add integ tests.

alextwoods commented 4 years ago

Have had to prioritize other work - but returning to this now. I will merge this tomorrow unless there are any other comments.