class Stax::Aws::DynamoDB

Public Class Methods

client() click to toggle source
# File lib/stax/aws/dynamodb.rb, line 10
def client
  @_client ||= ::Aws::DynamoDB::Client.new
end
count(opt) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 61
def count(opt)
  total = 0
  do_scan(opt.merge(select: 'COUNT')) do |r|
    total += r.count
  end
  return total
end
create_backup(table_name, backup_name) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 97
def create_backup(table_name, backup_name)
  client.create_backup(
    table_name:  table_name,
    backup_name: backup_name,
  ).backup_details
end
do_scan(opt) { |r| ... } click to toggle source
# File lib/stax/aws/dynamodb.rb, line 43
def do_scan(opt)
  exclusive_start_key = nil
  loop do
    r = client.scan(opt.merge(exclusive_start_key: exclusive_start_key))
    yield r
    exclusive_start_key = r.last_evaluated_key
    break unless exclusive_start_key
  end
end
global_table(name) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 18
def global_table(name)
  client.describe_global_table(global_table_name: name)&.global_table_description
rescue ::Aws::DynamoDB::Errors::GlobalTableNotFoundException
  nil
end
gsi(name) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 24
def gsi(name)
  client.describe_table(table_name: name).table.global_secondary_indexes || []
end
key_schema(name) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 32
def key_schema(name)
  client.describe_table(table_name: name).table.key_schema
end
keys(name) click to toggle source

key schema as a hash

# File lib/stax/aws/dynamodb.rb, line 37
def keys(name)
  key_schema(name).each_with_object({}) do |s, h|
    h[s.key_type.downcase.to_sym] = s.attribute_name
  end
end
list_backups(opt = {}) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 85
def list_backups(opt = {})
  last_arn = nil
  backups = []
  loop do
    r = client.list_backups(opt.merge(exclusive_start_backup_arn: last_arn))
    backups += r.backup_summaries
    last_arn = r.last_evaluated_backup_arn
    break unless last_arn
  end
  backups
end
lsi(name) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 28
def lsi(name)
  client.describe_table(table_name: name).table.local_secondary_indexes || []
end
put(opt) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 81
def put(opt)
  client.put_item(opt)
end
query(opt) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 69
def query(opt)
  exclusive_start_key = nil
  loop do
    r = client.query(opt.merge(exclusive_start_key: exclusive_start_key))
    r.items.each do |item|
      puts JSON.generate(item)
    end
    exclusive_start_key = r.last_evaluated_key
    break unless exclusive_start_key
  end
end
restore_backup(table_name, backup_arn) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 104
def restore_backup(table_name, backup_arn)
  client.restore_table_from_backup(
    target_table_name: table_name,
    backup_arn:        backup_arn,
  ).table_description
end
scan(opt) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 53
def scan(opt)
  do_scan(opt) do |r|
    r.items.each do |item|
      puts JSON.generate(item)
    end
  end
end
table(name) click to toggle source
# File lib/stax/aws/dynamodb.rb, line 14
def table(name)
  client.describe_table(table_name: name).table
end