Class: Dynamoid::AdapterPlugin::AwsSdkV3::CreateTable

Inherits:
Object
  • Object
show all
Defined in:
lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, table_name, key, options) ⇒ CreateTable

Returns a new instance of CreateTable.



12
13
14
15
16
17
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb', line 12

def initialize(client, table_name, key, options)
  @client = client
  @table_name = table_name
  @key = key
  @options = options
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb', line 10

def client
  @client
end

#keyObject (readonly)

Returns the value of attribute key.



10
11
12
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb', line 10

def key
  @key
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb', line 10

def options
  @options
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



10
11
12
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb', line 10

def table_name
  @table_name
end

Instance Method Details

#callObject



19
20
21
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb', line 19

def call
  billing_mode = options[:billing_mode]
  read_capacity = options[:read_capacity] || Dynamoid::Config.read_capacity
  write_capacity = options[:write_capacity] || Dynamoid::Config.write_capacity

  secondary_indexes = options.slice(
    :local_secondary_indexes,
    :global_secondary_indexes
  )
  ls_indexes = options[:local_secondary_indexes]
  gs_indexes = options[:global_secondary_indexes]

  key_schema = {
    hash_key_schema: { key => options[:hash_key_type] || :string },
    range_key_schema: options[:range_key]
  }
  attribute_definitions = build_all_attribute_definitions(
    key_schema,
    secondary_indexes
  )
  key_schema = aws_key_schema(
    key_schema[:hash_key_schema],
    key_schema[:range_key_schema]
  )

  client_opts = {
    table_name: table_name,
    key_schema: key_schema,
    attribute_definitions: attribute_definitions
  }

  if billing_mode == :on_demand
    client_opts[:billing_mode] = 'PAY_PER_REQUEST'
  else
    client_opts[:billing_mode] = 'PROVISIONED'
    client_opts[:provisioned_throughput] = {
      read_capacity_units: read_capacity,
      write_capacity_units: write_capacity
    }
  end

  if ls_indexes.present?
    client_opts[:local_secondary_indexes] = ls_indexes.map do |index|
      index_to_aws_hash(index)
    end
  end

  if gs_indexes.present?
    client_opts[:global_secondary_indexes] = gs_indexes.map do |index|
      index_to_aws_hash(index)
    end
  end
  resp = client.create_table(client_opts)
  options[:sync] = true if (!options.key?(:sync) && ls_indexes.present?) || gs_indexes.present?

  if options[:sync]
    status = PARSE_TABLE_STATUS.call(resp, :table_description)
    if status == TABLE_STATUSES[:creating]
      UntilPastTableStatus.new(client, table_name, :creating).call
    end
  end

  # Response to original create_table, which, if options[:sync]
  # may have an outdated table_description.table_status of "CREATING"
  resp
end