Class: Rom::Dynamo::Dataset

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rom/dynamo/dataset.rb

Direct Known Subclasses

BatchGetDataset, GlobalIndexDataset

Constant Summary collapse

EmptyQuery =
{ key_conditions: {}.freeze }.freeze

Instance Method Summary collapse

Instance Method Details

#batch_restrict(keys) ⇒ Object



36
37
38
39
40
# File 'lib/rom/dynamo/dataset.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) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/rom/dynamo/dataset.rb', line 68

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) ⇒ Object

ENUMERATE ###########



18
19
20
21
# File 'lib/rom/dynamo/dataset.rb', line 18

def each(&block)
  return enum_for(:each) if block.nil?
  each_page { |p| p.items.each(&block) }
end

#each_page(&block) ⇒ Object



23
24
25
26
27
# File 'lib/rom/dynamo/dataset.rb', line 23

def each_page(&block)
  return enum_for(:each_page) if block.nil?
  result = start_query(consistent_read: true)
  result.each_page(&block)
end

#index_restrict(index, query) ⇒ Object



42
43
44
# File 'lib/rom/dynamo/dataset.rb', line 42

def index_restrict(index, query)
  dup_with_query(GlobalIndexDataset, query, index_name: index.to_s)
end

#insert(hash) ⇒ Object

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



63
64
65
66
# File 'lib/rom/dynamo/dataset.rb', line 63

def insert(hash)
  opts = { table_name: name, item: stringify_keys(hash) }
  connection.put_item(opts).attributes
end

#limit(limit) ⇒ Object

PAGINATE #############



48
49
50
51
# File 'lib/rom/dynamo/dataset.rb', line 48

def limit(limit)
  opts = limit.nil? ? {} : { limit: limit.to_i }
  dup_with_query(self.class, nil, opts)
end

#offset(key) ⇒ Object



53
54
55
56
# File 'lib/rom/dynamo/dataset.rb', line 53

def offset(key)
  opts = key.nil? ? {} : { exclusive_start_key: key }
  dup_with_query(self.class, nil, opts)
end

#restrict(query = nil) ⇒ Object

QUERY #############



31
32
33
34
# File 'lib/rom/dynamo/dataset.rb', line 31

def restrict(query = nil)
  return self if query.nil?
  dup_with_query(self.class, query)
end

#reversedObject



58
59
60
# File 'lib/rom/dynamo/dataset.rb', line 58

def reversed
  dup_with_query(self.class, nil, scan_index_forward: false)
end

#update(keys, hash) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/rom/dynamo/dataset.rb', line 77

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