Module: Dynamini::BatchOperations

Included in:
Base
Defined in:
lib/dynamini/batch_operations.rb

Instance Method Summary collapse

Instance Method Details

#batch_delete(ids) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/dynamini/batch_operations.rb', line 30

def batch_delete(ids)
  deletes = ids.map{ |id| { delete_request: { key: { hash_key => id } } } }

  deletes.each_slice(25) do |slice|
    client.batch_write_item(request_items: { table_name => slice })
  end
end

#batch_find(ids = []) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dynamini/batch_operations.rb', line 17

def batch_find(ids = [])
  return OpenStruct.new(found: [], not_found: []) if ids.length < 1
  objects = []
  key_structure = ids.map { |i| {hash_key => i} }
  key_structure.each_slice(100) do |keys|
    response = dynamo_batch_get(keys)
    response.responses[table_name].each do |item|
      objects << new(item.symbolize_keys, false)
    end
  end
  OpenStruct.new(found: objects, not_found: ids - objects.map(&hash_key))
end

#import(models, options = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/dynamini/batch_operations.rb', line 6

def import(models, options = {})
  # Max batch size is 25, per Dynamo BatchWriteItem docs

  models.each_slice(25) do |batch|
    batch.each do |model|
      model.send(:generate_timestamps!) unless options[:skip_timestamps]
    end
    dynamo_batch_save(batch)
  end
end

#scan(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/dynamini/batch_operations.rb', line 38

def scan(options = {})
  validate_scan_options(options)
  response = dynamo_scan(options)
  OpenStruct.new(
    items: response.items.map { |i| new(i.symbolize_keys, false) },
    last_evaluated_key: response.last_evaluated_key
  )
end