Method: Dynamoid::Indexes::ClassMethods#global_secondary_index

Defined in:
lib/dynamoid/indexes.rb

#global_secondary_index(options = {}) ⇒ Object

Defines a Global Secondary index on a table. Keys can be specified as hash-only, or hash & range.

Parameters:

  • options (Hash) (defaults to: {})

    options to pass for this table

Options Hash (options):

  • :name (Symbol)

    the name for the index; this still gets namespaced. If not specified, will use a default name.

  • :hash_key (Symbol)

    the index hash key column.

  • :range_key (Symbol)

    the index range key column (if applicable).

  • :projected_attributes (Symbol, Array<Symbol>)

    table attributes to project for this index. Can be :keys_only, :all or an array of included fields. If not specified, defaults to :keys_only.

  • :read_capacity (Integer)

    set the read capacity for the index; does not work on existing indexes.

  • :write_capacity (Integer)

    set the write capacity for the index; does not work on existing indexes.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dynamoid/indexes.rb', line 32

def global_secondary_index(options = {})
  unless options.present?
    raise Dynamoid::Errors::InvalidIndex, 'empty index definition'
  end

  unless options[:hash_key].present?
    raise Dynamoid::Errors::InvalidIndex, 'A global secondary index requires a :hash_key to be specified'
  end

  index_opts = {
    read_capacity: Dynamoid::Config.read_capacity,
    write_capacity: Dynamoid::Config.write_capacity
  }.merge(options)

  index_opts[:dynamoid_class] = self
  index_opts[:type] = :global_secondary

  index = Dynamoid::Indexes::Index.new(index_opts)
  gsi_key = index_key(options[:hash_key], options[:range_key])
  global_secondary_indexes[gsi_key] = index
  self
end