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.

Parameters:

  • hash (scalar) —

    the hash key used to declare an index

  • range (scalar) (defaults to: nil) —

    the range key used to declare an index (optional)

Returns:



182
183
184
# File 'lib/dynamoid/indexes.rb', line 182

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

#find_index_by_name(name) ⇒ Dynamoid::Indexes::Index?

Returns an index by its name

Parameters:

  • name (string, symbol) —

    the name of the index to lookup

Returns:



190
191
192
193
# File 'lib/dynamoid/indexes.rb', line 190

def find_index_by_name(name)
  string_name = name.to_s
  indexes.each_value.detect { |i| i.name.to_s == string_name }
end

#global_secondary_index(options = {}) ⇒ Class

Defines a Global Secondary Index (GSI) on a table. Keys can be specified as hash-only, or hash & range.

class Post
  include Dynamoid::Document

  field :category

  global_secondary_index hash_key: :category
end

The full example with all the options being specified:

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

Because global_secondary_index resolves the table name and key field types immediately when called, it must be declared after table, range, and the referenced field definitions.

Referenced :hash_key and :range_key fields must use a scalar type supported by DynamoDB keys (+:string+, :integer, :number, :date, :datetime, :binary, :serialized, or a custom class or module implementing serialization).

Parameters:

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

    the options to pass for this index

Options Hash (options):

  • :hash_key (Symbol) —

    the index partition (hash) key attribute (required).

  • :range_key (Symbol) —

    the index sort (range) key attribute (optional).

  • :name (Symbol, String) —

    explicit index name; used as-is without a table or namespace prefix. If omitted, a default name (+<table_name>_index_+) is generated.

  • :projected_attributes (Symbol, Array<Symbol>) —

    table attributes to project into this index. Can be :keys_only, :all, or an array of attribute names (e.g. [:title, :author]). Defaults to :keys_only.

  • :read_capacity (Integer) —

    read capacity units for the index (defaults to Dynamoid::Config.read_capacity). Only applies when creating a provisioned table; ignored when the table uses :on_demand capacity mode.

  • :write_capacity (Integer) —

    write capacity units for the index (defaults to Dynamoid::Config.write_capacity). Only applies when creating a provisioned table; ignored when the table uses :on_demand capacity mode.

Returns:

  • (Class) —

    self

Raises:

  • (Dynamoid::Errors::InvalidIndex) —

    if options is empty, :hash_key is missing, a referenced key field is not yet declared or is not a valid scalar key type, or :projected_attributes is invalid.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dynamoid/indexes.rb', line 76

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.

Parameters:

  • hash (Symbol) —

    hash key name.

  • range (Symbol) (defaults to: nil) —

    range key name.

Returns:

  • (String) —

    returns "hash" if hash only, "hash_range" otherwise.



223
224
225
226
227
# File 'lib/dynamoid/indexes.rb', line 223

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.

Parameters:

  • hash (Symbol) —

    hash key name.

  • range (Symbol) (defaults to: nil) —

    range key name.

Returns:

  • (String) —

    index name of the form "table_name_index_index_key".



234
235
236
# File 'lib/dynamoid/indexes.rb', line 234

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

#indexed_hash_keys ⇒ Array[String]

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

Returns:

  • (Array[String]) —

    array of hash keys



250
251
252
253
254
# File 'lib/dynamoid/indexes.rb', line 250

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

#indexes ⇒ Hash<String, Object>

Convenience method to return all indexes on the table.

Returns:

  • (Hash<String, Object>) —

    the combined hash of global and local secondary indexes.



242
243
244
# File 'lib/dynamoid/indexes.rb', line 242

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.

Parameters:

  • hash (Symbol) —

    hash key name.

  • range (Symbol) (defaults to: nil) —

    range key name.

Returns:

  • (Boolean) —

    true iff provided keys correspond to a global secondary index.



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

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.

Parameters:

  • hash (Symbol) —

    hash key name.

  • range (Symbol) (defaults to: nil) —

    range key name.

Returns:

  • (Boolean) —

    true iff provided keys correspond to a local secondary index.



202
203
204
# File 'lib/dynamoid/indexes.rb', line 202

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

#local_secondary_index(options = {}) ⇒ Class

Defines a Local Secondary Index (LSI) on a table. Uses the same primary partition (hash) key as the table and requires an alternate :range_key.

class Comment
  include Dynamoid::Document

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

  local_secondary_index range_key: :author_id
end

The full example with all the options being specified:

local_secondary_index range_key: :author_id,
                      name: 'comments_author_id_index',
                      projected_attributes: :all

Because local_secondary_index resolves the table name, primary keys, and field types immediately when called, it must be declared after table, range, and the referenced field definition.

The referenced :range_key field must differ from the table's primary sort key and use a scalar type supported by DynamoDB keys (+:string+, :integer, :number, :date, :datetime, :binary, :serialized, or a custom class or module implementing serialization).

Parameters:

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

    options to pass for this index

Options Hash (options):

  • :range_key (Symbol) —

    the index sort (range) key attribute (required).

  • :name (Symbol, String) —

    explicit index name; used as-is without a table or namespace prefix. If omitted, a default name (+<table_name>_index_+) is generated.

  • :projected_attributes (Symbol, Array<Symbol>) —

    table attributes to project into this index. Can be :keys_only, :all, or an array of attribute names (e.g. [:title, :author]). Defaults to :keys_only.

Returns:

  • (Class) —

    self

Raises:

  • (Dynamoid::Errors::InvalidIndex) —

    if options is empty, :range_key is missing or matches the table's primary range key, a referenced key field is not yet declared or is not a valid scalar key type, or :projected_attributes is invalid.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/dynamoid/indexes.rb', line 144

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