class ROM::DynamoDB::Dataset
Attributes
@attr_reader [Hash] config the configuration to apply to the underlying
DynamoDB client.
@attr_reader [Hash] config the configuration to apply to the underlying
DynamoDB client.
@attr_reader [Hash] config the configuration to apply to the underlying
DynamoDB client.
This array of hashes is built up by chaining the relational methods together and when the query is finally executed, all the hashes in this array are merged and send to the underlying DynamoDB
client.
Public Class Methods
# File lib/rom/dynamodb/dataset.rb, line 40 def initialize(name:, operation: :query, config: {}, queries: []) @name = name @operation = operation @config = config @queries = queries end
Public Instance Methods
Retrieves all matching range keys within the composite key after the value provided.
@deprecated Use {#where} instead.
@!macro rom.dynamodb.dataset.chain_note
@!macro rom.dynamodb.dataset.series_note
@deprecated Use {#where} instead.
@example Given users = relation.equal(:id, "mammal").after(:legs, 0).to_a
users #=> [{id: "mammal", legs: 2, name: "Human"}, ...]
@param key [Symbol] the key to match the provided value against. @param after the value to match range values after @param predicate [String] the query predicate to apply to @return [self] the {Dataset} object the method was performed on.DynamoDB
.# File lib/rom/dynamodb/dataset.rb, line 240
def after(key, after, predicate = :ge)
restrict_by(key, predicate, [after])
end
Performs a traversal of the index in ascending order on the range key. DynamoDB
will return the results in the order in which they have been stored.
@note This is the default behaviour.
@!macro rom.dynamodb.dataset.chain_note @!macro rom.dynamodb.dataset.order_note
@return [self] the {Dataset} object the method was performed on.
# File lib/rom/dynamodb/dataset.rb, line 69 def ascending restrict(scan_index_forward: true) end
# File lib/rom/dynamodb/dataset.rb, line 256 def batch_get(query = nil) append(:batch_get) { query } end
Retreives all matching range keys within the composite key before the value provided.
@deprecated Use {#where} instead.
@!macro rom.dynamodb.dataset.chain_note
@!macro rom.dynamodb.dataset.series_note
# File lib/rom/dynamodb/dataset.rb, line 252 def before(key, before, predicate = :le) restrict_by(key, predicate, [before]) end
Retrieves all matching range keys within the composite key for the index between two points.
@deprecated Use {#where} instead.
@!macro rom.dynamodb.dataset.chain_note
@!macro rom.dynamodb.dataset.series_note
# File lib/rom/dynamodb/dataset.rb, line 272 def build(parts = queries) parts.inject(:deep_merge).merge(table_name: name) end
# File lib/rom/dynamodb/dataset.rb, line 302 def connection @connection ||= Aws::DynamoDB::Client.new(config) end
# File lib/rom/dynamodb/dataset.rb, line 283 def create(hash) payload = build([{ item: hash }]) connection.put_item(payload).attributes end
# File lib/rom/dynamodb/dataset.rb, line 288 def delete(hash) payload = build([{ key: hash }]) connection.delete_item(payload).data end
Performs a traversal of the index in descending order on the range key. DynamoDB
reads the results in reverse order by sort key value, and then returns the results to the client.
@note This is the default behaviour.
@!macro rom.dynamodb.dataset.chain_note @!macro rom.dynamodb.dataset.order_note
@return [self] the {Dataset} object the method was performed on.
# File lib/rom/dynamodb/dataset.rb, line 83 def descending restrict(scan_index_forward: false) end
# File lib/rom/dynamodb/dataset.rb, line 298 def each(&block) each_item(build, &block) end
Retrieves a key present within the composite key for this index by its exact value.
@deprecated Use {#where} instead.
@!macro rom.dynamodb.dataset.chain_note
@example Given relation.equal(:id, 1).one! #=> { id: 1, ... }
@example Given relation.equal(:id, 1).equal(:created_at, Time.now.to_f).one! #=> { id: 1, ... }
@param key [Symbol] the key to match the provided value against. @param val the value to match against the key in the index. @param predicate [Symbol] the query predicate to apply to @return [self] the {Dataset} object the method was performed on.DynamoDB
.# File lib/rom/dynamodb/dataset.rb, line 194
def equal(key, val, predicate = :eq)
restrict_by(key, predicate, [val])
end
# File lib/rom/dynamodb/dataset.rb, line 306 def execute(query) @response ||= case operation when :batch_get connection.send(operation, { request_items: { name => query } }) else connection.send(operation, query).data end end
The name of an index to query. This index can be any local secondary index or global secondary index on the table.
@!macro rom.dynamodb.dataset.chain_note
@param name [String] the name of the index to query.
@return [self] the {Dataset} object the method was performed on.
# File lib/rom/dynamodb/dataset.rb, line 55 def index(name) restrict(index_name: name) end
# File lib/rom/dynamodb/dataset.rb, line 293 def information payload = build([{}]) connection.describe_table(payload).table end
Limits the number of results returned by the query or scan operation.
The maximum number of items to evaluate (not necessarily the number of matching items). If DynamoDB
processes the number of items up to the limit while processing the results, it stops the operation and returns the matching values up to that point.
If there are more matches to be returned, the {#last_evaluated_key}
@!macro rom.dynamodb.dataset.chain_note
@return [self] the {Dataset} object the method was performed on.
# File lib/rom/dynamodb/dataset.rb, line 99 def limit(num = nil) append { { limit: num } unless num.nil? } end
The composite key of the first item that the resulting query will evaluate. Use this method if you have a populated {#last_evaluated_key}.
@!macro rom.dynamodb.dataset.chain_note
@note When applying an offset, it must include the same composite key of
which the index you are querying is composed from. Therefore, if your index has both a hash and range key, the key you provide must also have these.
@param key [Hash] the composite offset key matching the queryable index.
@return [self] the {Dataset} object the method was performed on.
# File lib/rom/dynamodb/dataset.rb, line 116 def offset(key) append { { exclusive_start_key: key } unless key.nil? } end
# File lib/rom/dynamodb/dataset.rb, line 260 def restrict(query = nil) append(:query) { query } end
# File lib/rom/dynamodb/dataset.rb, line 264 def retrieve(query = nil) append(:get_item) { query } end
# File lib/rom/dynamodb/dataset.rb, line 268 def scan(query = nil) append(:scan) { query } end
Selects one or more keys to retrieve from the table.
These keys can include scalars, sets, or elements of a JSON document.
@!macro rom.dynamodb.dataset.chain_note
@param keys [Array<String>] an array of string expressions to apply
to the query
@return [self] the {Dataset} object the method was performed on.
# File lib/rom/dynamodb/dataset.rb, line 131 def select(keys) restrict(projection_expression: keys.collect(&:to_s).join(",")) end
# File lib/rom/dynamodb/dataset.rb, line 276 def update(key, hash, action = 'PUT') data = hash.delete_if { |k, _| key.keys.include?(k) } update = to_update_structure(data) payload = build([update, { key: key }]) connection.update_item(payload).data end
Restricts keys within a composite key, by values using a specific operand.
You can compose where clauses using compartive operators from inside a block, allowing a greater level of flexibility.
Multiple where clauses can be chained, or multiple predicates defined inside an array within a single clause. See the examples below.
@!macro rom.dynamodb.dataset.chain_note
@note The following operands are supported, :>=, :>, :<, :<=, :== and :between
Private Instance Methods
# File lib/rom/dynamodb/dataset.rb, line 337 def append(operation = :query, &block) result = block.call if result[:key_condition_expression] end if result args = { name: name, config: config, operation: operation } self.class.new(args.merge(queries: queries + [result].flatten)) else self end end
# File lib/rom/dynamodb/dataset.rb, line 326 def each_item(body, &block) case operation when :get_item block.call execute(body).item when :batch_get execute(body).responses[name.to_s].each(&block) else execute(body).items.each(&block) end end
# File lib/rom/dynamodb/dataset.rb, line 317 def restrict_by(key, verb, list) restrict(key_conditions: { key => { comparison_operator: verb.to_s.upcase, attribute_value_list: list } }) end
# File lib/rom/dynamodb/dataset.rb, line 350 def to_update_structure(hash) { expression_attribute_values: Hash[hash.map { |k, v| [":#{k}_u", v] }], expression_attribute_names: Hash[hash.map { |k, v| ["##{k}_u", k] }], update_expression: "SET " + hash.map { |k, v| "##{k}_u = :#{k}_u" }.join(", "), } end