Module: Aws::Record::Query::QueryClassMethods

Included in:
Aws::Record
Defined in:
lib/aws-record/record/query.rb

Instance Method Summary collapse

Instance Method Details

#query(opts) ⇒ Aws::Record::ItemCollection

This method calls Aws::DynamoDB::Client#query, populating the :table_name parameter from the model class, and combining this with the other parameters you provide.

Examples:

A query with key and filter expressions:

# Example model class
class ExampleTable
  include Aws::Record
  string_attr  :uuid, hash_key: true
  integer_attr :id,   range_key: true
  string_attr  :body
end

query = ExampleTable.query(
  key_condition_expression: "#H = :h AND #R > :r",
  filter_expression: "contains(#B, :b)",
  expression_attribute_names: {
    "#H" => "uuid",
    "#R" => "id",
    "#B" => "body"
  },
  expression_attribute_values: {
    ":h" => "123456789uuid987654321",
    ":r" => 100,
    ":b" => "some substring"
  }
)

# You can enumerate over your results.
query.each do |r|
  puts "UUID: #{r.uuid}\nID: #{r.id}\nBODY: #{r.body}\n"
end

Parameters:

  • opts (Hash)

    options to pass on to the client call to #query. See the documentation above in the AWS SDK for Ruby V2.

Returns:


63
64
65
66
# File 'lib/aws-record/record/query.rb', line 63

def query(opts)
  query_opts = opts.merge(table_name: table_name)
  ItemCollection.new(:query, query_opts, self, dynamodb_client)
end

#scan(opts = {}) ⇒ Aws::Record::ItemCollection

This method calls Aws::DynamoDB::Client#scan, populating the :table_name parameter from the model class, and combining this with the other parameters you provide.

Examples:

A scan with a filter expression:

# Example model class
class ExampleTable
  include Aws::Record
  string_attr  :uuid, hash_key: true
  integer_attr :id,   range_key: true
  string_attr  :body
end

scan = ExampleTable.scan(
  filter_expression: "contains(#B, :b)",
  expression_attribute_names: {
    "#B" => "body"
  },
  expression_attribute_values: {
    ":b" => "some substring"
  }
)

# You can enumerate over your results.
scan.each do |r|
  puts "UUID: #{r.uuid}\nID: #{r.id}\nBODY: #{r.body}\n"
end

Parameters:

  • opts (Hash) (defaults to: {})

    options to pass on to the client call to #scan. See the documentation above in the AWS SDK for Ruby V2.

Returns:


101
102
103
104
# File 'lib/aws-record/record/query.rb', line 101

def scan(opts = {})
  scan_opts = opts.merge(table_name: table_name)
  ItemCollection.new(:scan, scan_opts, self, dynamodb_client)
end