Class: DynamodbModel::Migration::Dsl::BaseSecondaryIndex

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/dynamodb_model/migration/dsl/base_secondary_index.rb

Direct Known Subclasses

GlobalSecondaryIndex, LocalSecondaryIndex

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common

#adjust_schema_and_attributes, #partition_key, #provisioned_throughput, #sort_key

Constructor Details

#initialize(action, index_name = nil, &block) ⇒ BaseSecondaryIndex

Returns a new instance of BaseSecondaryIndex.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dynamodb_model/migration/dsl/base_secondary_index.rb', line 8

def initialize(action, index_name=nil, &block)
  @action = action.to_sym
  # for gsi action can be: :create, :update, :delete
  # for lsi action is always: :create
  @index_name = index_name
  @block = block

  # Dsl fills these atttributes in as methods are called within
  # the block
  @key_schema = []
  @attribute_definitions = []
  # default provisioned_throughput
  @provisioned_throughput = {
    read_capacity_units: 5,
    write_capacity_units: 5
  }
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



6
7
8
# File 'lib/dynamodb_model/migration/dsl/base_secondary_index.rb', line 6

def action
  @action
end

#attribute_definitionsObject

Returns the value of attribute attribute_definitions.



6
7
8
# File 'lib/dynamodb_model/migration/dsl/base_secondary_index.rb', line 6

def attribute_definitions
  @attribute_definitions
end

#index_nameObject

Returns the value of attribute index_name.



7
8
9
# File 'lib/dynamodb_model/migration/dsl/base_secondary_index.rb', line 7

def index_name
  @index_name
end

#key_schemaObject

Returns the value of attribute key_schema.



6
7
8
# File 'lib/dynamodb_model/migration/dsl/base_secondary_index.rb', line 6

def key_schema
  @key_schema
end

Instance Method Details

#conventional_index_nameObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dynamodb_model/migration/dsl/base_secondary_index.rb', line 30

def conventional_index_name
  # @partition_key_identifier and @sort_key_identifier are set as immediately
  # when the partition_key and sort_key methods are called in the dsl block.
  # Usually look like this:
  #
  #   @partition_key_identifier: post_id:string
  #   @sort_key_identifier: updated_at:string
  #
  # We strip the :string portion in case it is provided
  #
  partition_key = @partition_key_identifier.split(':').first
  sort_key = @sort_key_identifier.split(':').first if @sort_key_identifier
  [partition_key, sort_key, "index"].compact.join('-')
end

#evaluateObject



45
46
47
48
49
# File 'lib/dynamodb_model/migration/dsl/base_secondary_index.rb', line 45

def evaluate
  return if @evaluated
  @block.call(self) if @block
  @evaluated = true
end

#paramsObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dynamodb_model/migration/dsl/base_secondary_index.rb', line 51

def params
  evaluate # lazy evaluation: wait until as long as possible before evaluating code block

  params = { index_name: index_name } # required for all actions

  if @action == :create
    params[:key_schema] = @key_schema # required for create action
    # hardcode to ALL for now
    params[:projection] = { # required
      projection_type: "ALL", # accepts ALL, KEYS_ONLY, INCLUDE
      # non_key_attributes: ["NonKeyAttributeName"],
    }
  end

  if [:create, :update].include?(@action)
    params[:provisioned_throughput] = @provisioned_throughput
  end

  params
end