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 = []
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
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
|