Class: Dynamodb::Local

Inherits:
Object
  • Object
show all
Extended by:
DynamoDBSchema
Defined in:
lib/dynamodb/local.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dynamodbObject (readonly)

Returns the value of attribute dynamodb.



11
12
13
# File 'lib/dynamodb/local.rb', line 11

def dynamodb
  @dynamodb
end

Class Method Details

.build_table_attrs(klass) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dynamodb/local.rb', line 38

def build_table_attrs(klass)
  params =
    {
      attribute_definitions: klass.attribute_definitions,
      key_schema: klass.key_schema,
      provisioned_throughput: provisioned_throughput
    }

  params.merge!(
    local_secondary_indexes: klass.local_indexes
  ) unless klass.local_indexes.empty?

  unless klass.global_indexes.empty?
    global_indexes_hash = klass.global_indexes.map do |x|
      x.merge({ provisioned_throughput: provisioned_throughput })
    end
    params.merge!(
      global_secondary_indexes: global_indexes_hash
    )
  end

  params
end

.create_table(table_name, klass, &block) ⇒ Object



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

def create_table(table_name, klass, &block)
  params = build_table_attrs(klass)
  params.merge!(yield) if block_given? # merge overrides
  @dynamodb.create_table(table_name, params)
rescue Aws::DynamoDB::Errors => e
  splat_error("Unable to create DynamoDB tables:", e.message)
end

.provisioned_throughputObject



62
63
64
65
66
67
# File 'lib/dynamodb/local.rb', line 62

def provisioned_throughput
  {
    read_capacity_units: 10,
    write_capacity_units: 10
  }
end

.resetObject



13
14
15
16
17
18
19
20
# File 'lib/dynamodb/local.rb', line 13

def reset
  @dynamodb ||= Dynamodb

  teardown
  build_tables # DynamoDBSchema#build_tables
rescue => e
  splat_error("Unable to reset DynamoDB:", e.message)
end

.splat_error(title, message) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dynamodb/local.rb', line 69

def splat_error(title, message)
  puts <<-HEREDOC
    ##############################

    #{title}

    #{message}

    ##############################
  HEREDOC
end

.teardownObject



22
23
24
25
26
27
28
# File 'lib/dynamodb/local.rb', line 22

def teardown
  @dynamodb.list_tables.each do |table|
    @dynamodb.delete_table(table)
  end
rescue => e
  splat_error("Unable to teardown DynamoDB tables:", e.message)
end