https://github.com/davidkelley/rom-dynamodb
ROM DynamoDB Adapter
https://github.com/davidkelley/rom-dynamodb
aws dynamodb gem rom rom-dynamodb ruby
Last synced: 3 months ago
JSON representation
ROM DynamoDB Adapter
- Host: GitHub
- URL: https://github.com/davidkelley/rom-dynamodb
- Owner: davidkelley
- License: mit
- Created: 2017-01-13T08:37:56.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-22T18:00:16.000Z (over 8 years ago)
- Last Synced: 2025-10-08T04:53:19.501Z (7 months ago)
- Topics: aws, dynamodb, gem, rom, rom-dynamodb, ruby
- Language: Ruby
- Size: 49.8 KB
- Stars: 6
- Watchers: 3
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# ROM DynamoDB Adapter
[](https://badge.fury.io/rb/rom-dynamodb) [](https://github.com/davidkelley/rom-dynamodb) [](http://www.rubydoc.info/github/davidkelley/rom-dynamodb) [](#license) [](https://gitter.im/rom-rb/chat)
[](https://codeclimate.com/github/davidkelley/rom-dynamodb) [](https://coveralls.io/github/davidkelley/rom-dynamodb?branch=master) [](https://gemnasium.com/github.com/davidkelley/rom-dynamodb)
[](https://travis-ci.org/davidkelley/rom-dynamodb) [](http://inch-ci.org/github/davidkelley/rom-dynamodb)
---
This adapter uses [ROM (>= 2.0.0)](http://rom-rb.org/) to provide an easy-to-use, clean and understandable interface for [AWS DynamoDB](https://aws.amazon.com/documentation/dynamodb/).
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'rom-dynamodb'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install rom-dynamodb
## Usage
The following container setup is for demonstration purposes only. You should follow the standard way of integrating ROM into your environment, as [documented here](http://rom-rb.org/learn/advanced/flat-style-setup/).
```ruby
require 'rom/dynamodb'
TABLE = "my-dynamodb-users-table"
# any other AWS::DynamoDB::Client options
credentials = { region: 'us-east-1' }
container = ROM.container(:dynamodb, credentials) do |rom|
rom.relation(:users) do
# Key Schema: id
dataset TABLE
def by_id(val)
where { id == val }
end
end
rom.commands(:users) do
FILTER = Functions[:symbolize_keys] >> Functions[:accept_keys, [:id]]
define(:create) do
KEYS = %w(id name)
result :one
input Functions[:accept_keys, KEYS]
end
define(:delete) do
result :one
input FILTER
end
define(:update) do
result :one
input FILTER
end
end
end
relation = container.relation(:users)
relation.count # => 1234
relation.where { id == 1 }.one! # => { id: 1, name: "David" }
relation.info # => DynamoDB Table Information
relation.status # => :active
# create a new user
create = container.commands[:users][:create]
user = create.call({ id: 2, name: "James" })
# update an existing user
update = container.commands[:users][:update]
update.by_id(user[:id]).call(name: "Mark")
relation.where(id: user[:id]) { id == id }.one! # => { id: 2, name: "Mark" }
# delete an existing user
delete = container.commands[:users][:delete]
delete.by_id(user[:id]).call
```
---
#### Querying a composite key DynamoDB Table
```ruby
container = ROM.container(:dynamodb, credentials) do |rom|
rom.relation(:logs) do
# Key Schema: host, timestamp
dataset "my-logs-table"
def by_host(ip)
where { host == ip }
end
def after_timestamp(time)
where { timestamp > time }
end
def before_timestamp(time)
where { timestamp < time }
end
end
rom.commands(:logs) do
define(:create) do
KEYS = %w(host timestamp message)
result :one
input Functions[:accept_keys, KEYS]
end
end
end
num_of_logs = 20
host = "192.168.0.1"
logs = (1..num_of_logs).to_a.collect do |i|
{ host: host, timestamp: Time.now.to_f + (i * 60), message: "some message" }
end
# create fake logs
container.commands[:logs][:create].call(logs)
relation = container.relation(:logs)
relation.count == num_of_logs # => true
all = relation.where(ip: host) { host == ip }.after(0).to_a # => [{host: "192.168.0.1", ... }, ...]
all.size # => 20
before = relation.where(ip: host) { [host == ip, timestamp < (Time.now.to_f + 60 * 60)] }.limit(1).to_a
before.size # => 1
before.first == logs.first # => true
offset = { ip: host, timestamp: logs[-2][:timestamp] }
last = relation.where(ip: host) { ip == host }.descending.after(0).offset(offset).limit(1).one!
last == logs.last # => true
```
---
## Development
All development takes place inside [Docker Compose](). Run the following commands to get setup:
```
$ docker-compose pull
$ docker-compose build
```
You can then begin developing, running RSpec tests with the following command:
```
$ docker-compose run rom rspec [args...]
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/davidkelley/rom-dynamo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).