Module: DynamoRecord::Persistence::ClassMethods

Defined in:
lib/dynamo_record/persistence.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



10
11
12
13
14
15
16
# File 'lib/dynamo_record/persistence.rb', line 10

def client
  @client ||= Aws::DynamoDB::Client.new(
                access_key_id: DynamoRecord::Config.access_key_id,
                secret_access_key: DynamoRecord::Config.secret_access_key,
                region: DynamoRecord::Config.region
              )
end

#create_table(opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dynamo_record/persistence.rb', line 22

def create_table(opts = {})
  table_name = opts[:table_name] || self.table_name
  read_capacity = opts[:read_capacity] || DynamoRecord::Config.read_capacity_units
  write_capacity = opts[:write_capacity] || DynamoRecord::Config.write_capacity_units

  # Default id
  attribute_definitions = [{attribute_name: "id", attribute_type: "S"}]

  # Global indexes
  indexes = []
  attributes.each do |key, value|
    indexes << key if value[:options][:index]
  end

  global_secondary_indexes = []
  indexes.each do |index|
    index_definition = {}
    index_definition[:index_name] = "#{index}-index"
    index_definition[:key_schema] = [{ attribute_name: index, key_type: 'HASH' }]
    index_definition[:projection] = { projection_type: 'ALL'}
    index_definition[:provisioned_throughput] = { read_capacity_units: read_capacity, write_capacity_units: write_capacity }
    global_secondary_indexes << index_definition
    attribute_definitions << {attribute_name: index.to_s, 
                              attribute_type: attributes[index][:type] == :string ? 'S' : 'N'} # only String and Number are supported. Not Binary
  end

  options = {
    attribute_definitions: attribute_definitions,
    table_name: table_name,
    key_schema: [
      {
        attribute_name: "id",
        key_type: "HASH",
      },
    ],
    provisioned_throughput: {
      read_capacity_units: read_capacity,
      write_capacity_units: write_capacity,
    }
  }

  options.merge!(global_secondary_indexes: global_secondary_indexes) unless global_secondary_indexes.empty?

  response = self.client.create_table(options)
end

#default_optionsObject



18
19
20
# File 'lib/dynamo_record/persistence.rb', line 18

def default_options
  { table_name: self.table_name }
end

#table_nameObject



6
7
8
# File 'lib/dynamo_record/persistence.rb', line 6

def table_name
  @table_name ||= DynamoRecord::Config.namespace ? "#{DynamoRecord::Config.namespace}_#{base_class.name.downcase.pluralize}" : base_class.name.downcase.pluralize
end