-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
671b8cb
commit df083ea
Showing
6 changed files
with
174 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.DS_Store | ||
/.bundle/ | ||
/.yardoc | ||
/Gemfile.lock | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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(:logs) do | ||
# Key Schema: host<Hash>, timestamp<Range> | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
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<Hash> | ||
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 # => <Hash> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters