Class: AWS::DynamoDB::TableCollection

Inherits:
Object
  • Object
show all
Includes:
Core::Collection::WithLimitAndNextToken
Defined in:
lib/aws/dynamo_db/table_collection.rb

Overview

Represents the tables in your account. Each table is represented by an instance of the Table class.

Schemas

Before you can operate on items in a table you must specify the schema. You do this by calling #hash_key= (and optionally #range_key=) on a table.

table = dynamo_db.tables['mytable']
table.hash_key = [:id, :string]

Examples:

Creating a Table

table = dynamo_db.tables.create('mytable', 10, 10, :hash_key => { :id => :string })

Enumerating Tables

dynamo_db.tables.each {|table| puts table.name }

Getting a Table by Name

table = dynamo_db.tables['mytable']

Instance Method Summary collapse

Methods included from Core::Collection

#each, #each_batch, #enum, #first, #in_groups_of, #page

Instance Method Details

#[](name) ⇒ Table

References a table by name.

dynamo_db.tables["MyTable"]

Parameters:

  • name (String)

Returns:

  • (Table)

    Returns the table with the given name.



110
111
112
# File 'lib/aws/dynamo_db/table_collection.rb', line 110

def [] name
  Table.new(name, :config => config)
end

#create(name, read_capacity_units, write_capacity_units, options = {}) ⇒ Table

Note:

Creating a table is an eventualy consistent operation. You can not interact with the table until its status (AWS::DynamoDB::Table#status) is :active.

Creates a new table.

table = dynamo_db.tables.create('mytable', 25, 25,
  :hash_key => { :id => :string })

Parameters:

  • name (String)

    The name of the table.

  • read_capacity_units (Integer)

    Sets the minimum number of reads supported before read requests are throttled.

  • write_capacity_units (Integer)

    Sets the minimum number of writes supported before writes requests are throttled.

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

Options Hash (options):

  • :hash_key (Hash)

    A hash key is a combination of an attribute name and type. If you want to have the hash key on the string attribute username you would call #create with:

    :hash_key => { :username => :string }
    

    The other supported types are :number and :binary. If you wanted to set the hash key on a numeric (integer) attribute then you could call #create with:

    :hash_key => { :id => :number }
    

    All tables require a hash key. If :hash_key is not provided then a default hash key will be provided. The default hash key is:

    :hash_key => { :id => :string }
    
  • :range_key (String)

    You can setup a table to use composite keys by providing a :range_key. Range keys are configured the same way as hash keys. They are useful for ordering items that share the same hash key.

Returns:

  • (Table)

    The newly created table.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/aws/dynamo_db/table_collection.rb', line 87

def create name, read_capacity_units, write_capacity_units, options = {}

  client_opts = {
    :table_name => name.to_s,
    :key_schema => key_schema(options),
    :provisioned_throughput => {
      :read_capacity_units => read_capacity_units,
      :write_capacity_units => write_capacity_units,
    },
  }

  response = client.create_table(client_opts)

  Table.new(name, :config => config)

end