References: https://github.com/Dynamoid/dynamoid
install gem 'dynamoid' and 'aws-sdk'.
- key
- secret
- reason
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
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
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
rake dynamoid:create_tables # (command will create your table 'development_users')
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)
1) Download latest version from - https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html
i) Extract whether you want
ii) Naviate into extracted directory
iii) CMD: java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
```
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
Model.where(primary_index: value, secondaryindex: value)
Dynamodb::DiwaliTicket.where(reference: 'blah', user_id: '1').last