Class: Stax::Aws::DynamoDB

Inherits:
Sdk
  • Object
show all
Defined in:
lib/stax/aws/dynamodb.rb

Constant Summary

Constants inherited from Sdk

Sdk::RETRY_LIMIT

Class Method Summary collapse

Methods inherited from Sdk

paginate

Class Method Details

.clientObject



10
11
12
# File 'lib/stax/aws/dynamodb.rb', line 10

def client
  @_client ||= ::Aws::DynamoDB::Client.new
end

.count(opt) ⇒ Object



61
62
63
64
65
66
67
# 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) ⇒ Object



97
98
99
100
101
102
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



18
19
20
21
22
# 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) ⇒ Object



24
25
26
# 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) ⇒ Object



32
33
34
# File 'lib/stax/aws/dynamodb.rb', line 32

def key_schema(name)
  client.describe_table(table_name: name).table.key_schema
end

.keys(name) ⇒ Object

key schema as a hash



37
38
39
40
41
# 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 = {}) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object



28
29
30
# File 'lib/stax/aws/dynamodb.rb', line 28

def lsi(name)
  client.describe_table(table_name: name).table.local_secondary_indexes || []
end

.put(opt) ⇒ Object



81
82
83
# File 'lib/stax/aws/dynamodb.rb', line 81

def put(opt)
  client.put_item(opt)
end

.query(opt) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# 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) ⇒ Object



104
105
106
107
108
109
# 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) ⇒ Object



53
54
55
56
57
58
59
# 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) ⇒ Object



14
15
16
# File 'lib/stax/aws/dynamodb.rb', line 14

def table(name)
  client.describe_table(table_name: name).table
end