Skip to content

Instantly share code, notes, and snippets.

@snmmaurya
Last active February 7, 2020 11:37
Show Gist options
  • Save snmmaurya/6ba397b5d58977714ff5cdc6c1643c14 to your computer and use it in GitHub Desktop.
Save snmmaurya/6ba397b5d58977714ff5cdc6c1643c14 to your computer and use it in GitHub Desktop.
rails dynamodb implementation

Using ORM + ODM in a single project

References: https://github.com/Dynamoid/dynamoid

Steps to start with dynamoid -

install gem 'dynamoid' and 'aws-sdk'.

Configure your dynamodb under aws DynamoDB service. get required credentials -

  1. key
  2. secret
  3. reason

Step 1 - create an initializer file config/initializers/dynamodb.rb with following content

Dynamoid.configure do |config|
  config.namespace = "namespace"
  config.access_key = 'access_key'
  config.secret_key = 'secret_key'
  config.region = 'reason'
  config.models_dir = "./app/models/dynamodb" # model directory path (required at the time of table creation)
end

Explanation

config.namespace: prefix of table name, if you specify your table name 'users' it would be created with the name 'namespace_users'. very use full to separate table under each environment.

config.models_dir: model directory whether you organize your models

Step 2 - Create your model under app/models/dynamodb - user.rb

class User
  include Dynamoid::Document

  table name: :users, key: :user_id
  field :name, :string
  field :email, :string
  field :status, :boolean
  range :timestamp, :number # sort key
end

Step 3 - go to root directory of your project run rake to create tables-

rake dynamoid:create_tables # (command will create your table 'development_users')

Queries-

User.create!(name: 'cherry', email: 'cherry@justest.com')  # Insert 

User.where(user_id: 'user_id').last  # Read

user.update(column: 'value') # Update

user.delete # Delete


# How to Order -
  User.where(email: 'cherry@justest.com')
  # Fetched data always sorted in ascending order by sort key in our case - timestamp
  # if you want to sort it in descending order -
  User.where(email: 'cherry@justest.com').scan_index_forward(false)

# How to limit -
User.where(email: 'cherry@justest.com').record_limit(10)

Test with rspec

i) Extract whether you want
ii) Naviate into extracted directory
iii) CMD: java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

2) Create file spec/supports/dynamoid_heper.rb

```
Dynamoid.configure do |config|
  config.namespace = Rails.env
  config.access_key = Settings.dynamodb.access_key
  config.secret_key = Settings.dynamodb.secret_key
  config.region = 'ap-south-1'
  config.models_dir = "./app/models/dynamodb"
  config.endpoint = 'http://localhost:8000'
end

module DynamoidHelper
  def self.reset
    Dynamoid.adapter.list_tables.each do |table|
      if table =~ /^#{Dynamoid::Config.namespace}/
        Dynamoid.adapter.delete_table(table)
      end
    end
    Dynamoid.adapter.tables.clear
    Dir[File.join(File.join(Dynamoid.config.models_dir), '*.rb')].sort.each { |file| require file }
    Dynamoid.included_models.each(&:create_table)
  end
end

RSpec.configure do |config|
  config.before(:each) do
    DynamoidHelper.reset
  end
end

# Reduce noise in test output
Dynamoid.logger.level = Logger::FATAL
```

Now its ready to run spec test CAUTION- you specs may be very slow, so should use (DynamoidHelper.reset) only on required cases

How to use secondary index

Model.where(primary_index: value, secondaryindex: value)
Dynamodb::DiwaliTicket.where(reference: 'blah', user_id: '1').last
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment