Module: DynamoRecord::Persistence::ClassMethods

Defined in:
lib/dynamo_record/persistence.rb

Instance Method Summary collapse

Instance Method Details

#clientObject



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

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,
                compute_checksums: DynamoRecord::Config.compute_checksums
              )
end

#create(attrs) ⇒ Object



82
83
84
85
86
# File 'lib/dynamo_record/persistence.rb', line 82

def create(attrs)
  object = self.new(attrs)
  object.save
  object
end

#create_table(opts = {}) ⇒ Object



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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dynamo_record/persistence.rb', line 28

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

  attribute_definitions = []
  key_schema = []

  # Default id hash key
  attribute_definitions << {attribute_name: 'id', 
                            attribute_type: 'S'}
  key_schema << {attribute_name: 'id',
                  key_type: "HASH"}

  if range_key
    attribute_definitions << {attribute_name: range_key.to_s, 
                              attribute_type: dynamodb_type(attributes[range_key][:type])}
    key_schema << {attribute_name: range_key.to_s,
                    key_type: "RANGE"}
  end

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

  local_secondary_indexes = []
  indexes.each do |index|
    index_definition = {}
    index_definition[:index_name] = "#{index}-index"
    index_definition[:key_schema] = [{ attribute_name: 'id', key_type: 'HASH' },
                                     { attribute_name: index, key_type: 'RANGE' }]
    index_definition[:projection] = { projection_type: 'ALL'}
    local_secondary_indexes << index_definition
    attribute_definitions << {attribute_name: index.to_s, 
                              attribute_type: dynamodb_type(attributes[index][:type])}
  end

  options = {
    attribute_definitions: attribute_definitions,
    table_name: table_name,
    key_schema: key_schema,
    provisioned_throughput: {
      read_capacity_units: read_capacity,
      write_capacity_units: write_capacity,
    }
  }

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

  response = self.client.create_table(options)
end

#default_optionsObject



20
21
22
# File 'lib/dynamo_record/persistence.rb', line 20

def default_options
  { table_name: self.table_name }
end

#describe_tableObject



24
25
26
# File 'lib/dynamo_record/persistence.rb', line 24

def describe_table
  self.client.describe_table(default_options)
end

#table_nameObject



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

def table_name
  name = ActiveSupport::Inflector.tableize(base_class.name)
  @table_name ||= DynamoRecord::Config.namespace ? "#{DynamoRecord::Config.namespace}_#{name}" : name
end