Skip to content

rykov/rom-dynamo

Repository files navigation

rom-dynamo

Gem Version Build Status

AWS DynamoDB adapter for ROM (Ruby Object Mapper).

Installation

gem 'rom-dynamo'

Usage

ROM-Dynamo uses the aws-sdk-dynamodb library. Configure AWS credentials before connecting:

Aws.config.merge!(
  credentials: Aws::Credentials.new(AWS_ACCESS, AWS_SECRET),
  region: 'us-east-1'
)

Connect using a URI that specifies the AWS region and an optional table name prefix:

dynamo://REGION/TABLE_PREFIX/

For example, with the URI below, accessing photos maps to the DynamoDB table myapp_photos in us-east-1 region:

dynamo://us-east-1/myapp_/

Relations

rom = ROM.container(:dynamo, 'dynamo://us-east-1/myapp_/') do |config|
  config.relation(:photos) do
    schema(infer: true)

    def by_id(id)
      restrict(id: id)
    end

    def all_for_user(user_id)
      index_restrict('user-to-id', user_id: user_id)
    end
  end
end

Repository

class PhotosRepo < ROM::Repository[:photos]
  commands :create, update: :by_id, delete: :by_id

  def by_id(id)
    photos.restrict(id: id).one
  end
end

repo = PhotosRepo.new(rom)
repo.create({ id: 1, user_id: 42, url: 'https://example.com/photo.jpg' })
repo.by_id(1)

Compatibility

  • Ruby >= 2.4 (MRI and JRuby)
  • ROM >= 5.0, < 6.0
  • aws-sdk-dynamodb ~> 1.0

Links

License

MIT

About

AWS DynamoDB adapter for Ruby Object Mapper

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors