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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dynamodb/local.rb', line 45

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



37
38
39
40
41
42
43
# File 'lib/dynamodb/local.rb', line 37

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

.init_dynamodbObject



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

def init_dynamodb
  @dynamodb ||= Dynamodb
  # Note: Set database to point to dynamodb local
  @dynamodb.config = { endpoint: "http://localhost:10070" }
  @dynamodb.reset_client # reset the connection
end

.provisioned_throughputObject



69
70
71
72
73
74
# File 'lib/dynamodb/local.rb', line 69

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

.resetObject



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

def reset
  init_dynamodb

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

.splat_error(title, message) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dynamodb/local.rb', line 76

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

    #{title}

    #{message}

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

.teardownObject



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

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