DynamoRecord

Provides helpful rake tasks and model extensions on top of aws-record.

Installation

Add this line to your application's Gemfile:

gem 'dynamo-record'

And then execute:

$ bundle

Or install it yourself as:

$ gem install dynamo-record

Usage

Models

In app/models, create a class that includes DynamoRecord::Model and contains one or more of the following attribute declarations:

  • integer_attr
  • float_attr
  • map_attr
  • composite_string_attr
  • composite_integer_attr

An example file:

class Model1
  include DynamoRecord::Model
  composite_string_attr(
    :model_key,
    hash_key: true,
    parts: [:model_id, :account_id]
  )
  integer_attr :position, range_key: true
  string_attr  :user_id
  float_attr   :score
  map_attr     :map_value
  composite_string_attr :quiz_key, parts: [:quiz_id]
end

The partition key is labeled with hash_key: true. The sort key is labeled with range_key: true.

A secondary index can be defined as follows:

  global_secondary_index(
    :secondary_idx,
    hash_key: :user_id,
    range_key: :score,
    projection: {
      projection_type: 'INCLUDE',
      non_key_attributes: [
        :map_value
      ]
    }
  )

The full documentation for projection_type and non_key_attributes can be found here.

Migrations

Dynamo migration files are stored in db/dynamo_migrate. The name of the file follows the style of standard Rails migration files like YYYYMMDDHHMMSS_create_model_1.rb. A migration file that creates a Dynamo table looks like:

module DynamoMigrate
  class CreateModel1 < DynamoRecord::TableMigration
    def self.up
      migrate(Model1) do |migration|
        migration.create!(
          provisioned_throughput: {
            read_capacity_units: 1,
            write_capacity_units: 1
          },
          global_secondary_index_throughput: {
            secondary_idx: {
              read_capacity_units: 1,
              write_capacity_units: 1
            }
          }
        )
      end
    end
  end
end

Note that the throughput of the table can be configured separately from the throughput of the secondary index.

A migration file that creates a Dynamo stream looks like:

module DynamoMigrate
  class AddModel1Stream < DynamoRecord::TableMigration
    def self.update
      add_stream(Model1)
    end
  end
end