class Rom::Dynamo::Dataset

Constants

EmptyQuery

Public Instance Methods

batch_restrict(keys) click to toggle source
# File lib/rom/dynamo/relation.rb, line 36
def batch_restrict(keys)
  dup_as(BatchGetDataset, keys: keys.map do |k|
    Hash[table_keys.zip(k.is_a?(Array) ? k : [k])]
  end)
end
delete(hash) click to toggle source
# File lib/rom/dynamo/relation.rb, line 62
def delete(hash)
  hash = stringify_keys(hash)
  connection.delete_item({
    table_name: name,
    key: hash_to_key(hash),
    expected: to_expected(hash),
  }).attributes
end
each(&block) click to toggle source

READ #############

# File lib/rom/dynamo/relation.rb, line 24
def each(&block)
  block.nil? ? to_enum : begin
    result = start_query(consistent_read: true)
    result.each_page { |p| p[:items].each(&block) }
  end
end
index_restrict(index, query) click to toggle source
# File lib/rom/dynamo/relation.rb, line 42
def index_restrict(index, query)
  dup_with_query(GlobalIndexDataset, query, index_name: index.to_s)
end
insert(hash) click to toggle source

WRITE #############

# File lib/rom/dynamo/relation.rb, line 57
def insert(hash)
  opts = { table_name: name, item: stringify_keys(hash) }
  connection.put_item(opts).attributes
end
limit(limit) click to toggle source

PAGINATION #############

# File lib/rom/dynamo/relation.rb, line 48
def limit(limit)
  dup_with_query(self.class, nil, limit: limit.to_i)
end
restrict(query = nil) click to toggle source
# File lib/rom/dynamo/relation.rb, line 31
def restrict(query = nil)
  return self if query.nil?
  dup_with_query(Dataset, query)
end
reversed() click to toggle source
# File lib/rom/dynamo/relation.rb, line 52
def reversed
  dup_with_query(self.class, nil, scan_index_forward: false)
end
update(keys, hash) click to toggle source
# File lib/rom/dynamo/relation.rb, line 71
def update(keys, hash)
  connection.update_item({
    table_name: name, key: hash_to_key(stringify_keys(keys)),
    attribute_updates: hash.each_with_object({}) do |(k, v), out|
      out[k] = { value: dump_value(v), action: 'PUT' } if !keys[k]
    end
  }).attributes
end

Private Instance Methods

batch_get_each_item(keys, &block) click to toggle source

HELPERS #############

# File lib/rom/dynamo/relation.rb, line 82
def batch_get_each_item(keys, &block)
  !keys.empty? && ddb.batch_get_item({
    request_items: { name => { keys: keys } },
  }).each_page do |page|
    out = page[:responses][name]
    out.each(&block)
  end
end
dump_value(v) click to toggle source
# File lib/rom/dynamo/relation.rb, line 145
def dump_value(v)
  return v.new_offset(0).iso8601(6) if v.is_a?(DateTime)
  v.is_a?(Time) ? v.utc.iso8601(6) : v
end
dup_as(klass, opts = {}) click to toggle source
# File lib/rom/dynamo/relation.rb, line 134
def dup_as(klass, opts = {})
  table_keys # To populate keys once at top-level Dataset
  attrs = Dataset.dry_initializer.attributes(self)
  klass.new(attrs.merge(opts))
end
dup_with_query(klass, key_hash, opts = {}) click to toggle source
# File lib/rom/dynamo/relation.rb, line 91
def dup_with_query(klass, key_hash, opts = {})
  opts = @query.merge(opts)

  if key_hash && !key_hash.empty?
    conditions = @query[:key_conditions]
    opts[:key_conditions] = conditions.merge(Hash[
      key_hash.map do |key, value|
        [key, {
          attribute_value_list: [value],
          comparison_operator:  "EQ"
        }]
      end
    ]).freeze
  end

  dup_as(klass, query: opts.freeze)
end
hash_to_key(hash) click to toggle source
# File lib/rom/dynamo/relation.rb, line 115
def hash_to_key(hash)
  table_keys.each_with_object({}) do |k, out|
    out[k] = hash[k] if hash.has_key?(k)
  end
end
start_query(opts = {}, &block) click to toggle source
# File lib/rom/dynamo/relation.rb, line 128
def start_query(opts = {}, &block)
  opts = @query.merge(table_name: name).merge!(opts)
  puts "Querying DDB: #{opts.inspect}"
  ddb.query(opts)
end
stringify_keys(hash) click to toggle source

String modifiers

# File lib/rom/dynamo/relation.rb, line 141
def stringify_keys(hash)
  hash.each_with_object({}) { |(k, v), out| out[k.to_s] = v }
end
table_keys() click to toggle source
# File lib/rom/dynamo/relation.rb, line 121
def table_keys
  @table_keys ||= begin
    r = ddb.describe_table(table_name: name)
    r[:table][:key_schema].map(&:attribute_name)
  end
end
to_expected(hash) click to toggle source
# File lib/rom/dynamo/relation.rb, line 109
def to_expected(hash)
  hash && Hash[hash.map do |k, v|
    [k, { value: v }]
  end]
end