Module: Dynamoid::Indexes::ClassMethods

Defined in:
lib/dynamoid/indexes.rb

Instance Method Summary collapse

Instance Method Details

#find_index(hash, range = nil) ⇒ Dynamoid::Indexes::Index?

Returns an index by its hash key and optional range key.

It works only for indexes without explicit name declared.



152
153
154
155
# File 'lib/dynamoid/indexes.rb', line 152

def find_index(hash, range = nil)
  index = indexes[index_key(hash, range)]
  index
end

#global_secondary_index(options = {}) ⇒ Object

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

class Post
  include Dynamoid::Document

  field :category

  global_secondary_indexes hash_key: :category
end

The full example with all the options being specified:

global_secondary_indexes hash_key: :category,
                         range_key: :created_at,
                         name: 'posts_category_created_at_index',
                         projected_attributes: :all,
                         read_capacity: 100,
                         write_capacity: 20

Global secondary index should be declared after fields for mentioned hash key and optional range key are declared (with method field)

The only mandatory option is hash_key. Raises Dynamoid::Errors::InvalidIndex exception if passed incorrect options.

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dynamoid/indexes.rb', line 56

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

#index_key(hash, range = nil) ⇒ String

Generates a convenient lookup key name for a hash/range index. Should normally not be used directly.



185
186
187
188
189
# File 'lib/dynamoid/indexes.rb', line 185

def index_key(hash, range = nil)
  name = hash.to_s
  name += "_#{range}" if range.present?
  name
end

#index_name(hash, range = nil) ⇒ String

Generates a default index name.



196
197
198
# File 'lib/dynamoid/indexes.rb', line 196

def index_name(hash, range = nil)
  "#{table_name}_index_#{index_key(hash, range)}"
end

#indexed_hash_keysArray[String]

Returns an array of hash keys for all the declared Glocal Secondary Indexes.



212
213
214
215
216
# File 'lib/dynamoid/indexes.rb', line 212

def indexed_hash_keys
  global_secondary_indexes.map do |_name, index|
    index.hash_key.to_s
  end
end

#indexesHash<String, Object>

Convenience method to return all indexes on the table.



204
205
206
# File 'lib/dynamoid/indexes.rb', line 204

def indexes
  local_secondary_indexes.merge(global_secondary_indexes)
end

#is_global_secondary_index?(hash, range = nil) ⇒ Boolean

Returns true iff the provided hash key combo is a global secondary index.



175
176
177
# File 'lib/dynamoid/indexes.rb', line 175

def is_global_secondary_index?(hash, range = nil)
  global_secondary_indexes[index_key(hash, range)].present?
end

#is_local_secondary_index?(hash, range = nil) ⇒ Boolean

Returns true iff the provided hash key combo is a local secondary index.



164
165
166
# File 'lib/dynamoid/indexes.rb', line 164

def is_local_secondary_index?(hash, range = nil)
  local_secondary_indexes[index_key(hash, range)].present?
end

#local_secondary_index(options = {}) ⇒ Object

Defines a local secondary index on a table. Will use the same primary hash key as the table.

class Comment
  include Dynamoid::Document

  table hash_key: :post_id
  range :created_at, :datetime
  field :author_id

  local_secondary_indexes hash_key: :author_id
end

The full example with all the options being specified:

local_secondary_indexes range_key: :created_at,
                        name: 'posts_created_at_index',
                        projected_attributes: :all

Local secondary index should be declared after fields for mentioned hash key and optional range key are declared (with method field) as well as after table method call.

The only mandatory option is range_key. Raises Dynamoid::Errors::InvalidIndex exception if passed incorrect options.

Options Hash (options):

  • name (Symbol)

    the name for the index; this still gets namespaced. If not specified, a name is automatically generated.

  • range_key (Symbol)

    the range key column for the index.

  • 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.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dynamoid/indexes.rb', line 114

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

  primary_hash_key = hash_key
  primary_range_key = range_key
  index_range_key = options[:range_key]

  unless index_range_key.present?
    raise Dynamoid::Errors::InvalidIndex, 'A local secondary index '\
      'requires a :range_key to be specified'
  end

  if primary_range_key.present? && index_range_key == primary_range_key
    raise Dynamoid::Errors::InvalidIndex, 'A local secondary index'\
      ' must use a different :range_key than the primary key'
  end

  index_opts = options.merge(
    dynamoid_class: self,
    type: :local_secondary,
    hash_key: primary_hash_key
  )

  index = Dynamoid::Indexes::Index.new(index_opts)
  key = index_key(primary_hash_key, index_range_key)
  local_secondary_indexes[key] = index
  self
end