rom-dynamo 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzVjNmE0OWM2ODc1N2ZkNDM4ZDAzYjliNWYzODE0MDZmZWUxYzFkOA==
5
+ data.tar.gz: !binary |-
6
+ ZTBlOWU4ZGY1NGZiYjM1MmI3MDg1NjMxYmE0NWE5NjViODUxY2JlNw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MDBkZmMwNzcyM2VlODE1NzQ1Y2ExOTgzNDUwMjhkNDA3ZmYzMGM4YzYxYWFl
10
+ ODg1ZDlkYWEwNGEzODg2Mzk2YjFjMjI0MjUyYTAyODRiNTg3YjQyYjIxNWQ0
11
+ ZGQ1ZjNmNTVhZDkxOWE4ZTk1NGNkMjFmNTE1MmFkYmUzMDliYTM=
12
+ data.tar.gz: !binary |-
13
+ ZGY0ZGRmOThiZDBhYzRiMzA2NjdjMDhhZWM4MzRiMGNhYWEwMzhmYzRiMjhk
14
+ NDdlZWNhMzE2NjMyOTFkMTY4NWUyMDk4ZmU3YmUxM2I5MWI3NDdjNGU4Mzdk
15
+ MTIxNjllNjc1ZTU3NjEyODBhZDc5YjljOGFkNzI1NjNlYTk5YzU=
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.env
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # From rom-dynamo.gemspec
4
+ gemspec
5
+
6
+ # For development/testing
7
+ gem 'dotenv'
8
+ gem 'rom', :github => 'rom-rb/rom'
9
+ gem 'virtus'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Michael Rykov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,72 @@
1
+ [gem]: https://rubygems.org/gems/rom-dynamo
2
+ [aws]: https://github.com/aws/aws-sdk-core-ruby
3
+
4
+ # Rom::Dynamo
5
+
6
+ [![Gem Version](https://badge.fury.io/rb/rom-dynamo.svg)][gem]
7
+
8
+ AWS DynamoDB support for [Ruby Object Mapper](https://github.com/rom-rb/rom).
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'rom-dynamo'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install rom-dynamo
25
+
26
+ ## Usage
27
+
28
+ ROM-Dynamo uses [aws-sdk-core][aws] library, so you will need to initialize that first:
29
+
30
+ Aws.config.merge!({
31
+ credentials: Aws::Credentials.new(AWS_ACCESS, AWS_SECRET),
32
+ region: 'us-east-1'
33
+ })
34
+
35
+ To connect, use the following URL to specify the AWS region and table name prefix. In this case, accessing `photos` will map to the table `table-name-prefix-photos`:
36
+
37
+ dynamo://region/table-name-prefix-/
38
+
39
+ So a sample setup will be:
40
+
41
+ rom = ROM.setup(:dynamo, 'dynamo://us-east-1/development_app_/') do
42
+ relation(:photos) do
43
+ # This will call GetItem API directly
44
+ def by_id(id)
45
+ restrict(id: id)
46
+ end
47
+
48
+ # This will first query a Global Secondary Index
49
+ def all_for_user(id)
50
+ index_restrict('user-to-id', user_id: id)
51
+ end
52
+ end
53
+
54
+ commands(:photos) do
55
+ define(:create) { result(:one) }
56
+ define(:delete) { result(:one) }
57
+ end
58
+ end
59
+
60
+ ## Development
61
+
62
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it ( https://github.com/rykov/rom-dynamo/fork )
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rom/dynamo"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1 @@
1
+ require 'rom/dynamo'
@@ -0,0 +1,8 @@
1
+ require 'rom'
2
+ require 'aws-sdk-core'
3
+ require "rom/dynamo/version"
4
+ require 'rom/dynamo/relation'
5
+ require 'rom/dynamo/commands'
6
+ require 'rom/dynamo/repository'
7
+
8
+ ROM.register_adapter(:dynamo, Rom::Dynamo)
@@ -0,0 +1,26 @@
1
+ require 'rom/commands'
2
+
3
+ module Rom
4
+ module Dynamo
5
+ module Commands
6
+ # DynamoDB create command
7
+ class Create < ROM::Commands::Create
8
+ def execute(tuple)
9
+ attributes = input[tuple]
10
+ validator.call(attributes)
11
+ relation.insert(attributes.to_h)
12
+ []
13
+ end
14
+ end
15
+
16
+ # DynamoDB delete command
17
+ class Delete < ROM::Commands::Delete
18
+ def execute
19
+ target.to_a.tap do |tuples|
20
+ tuples.each { |t| relation.delete(t) }
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,140 @@
1
+ module Rom
2
+ module Dynamo
3
+ class Relation < ROM::Relation
4
+ include Enumerable
5
+ forward :restrict, :index_restrict
6
+
7
+ def insert(*args)
8
+ dataset.insert(*args)
9
+ self
10
+ end
11
+
12
+ def delete(*args)
13
+ dataset.delete(*args)
14
+ self
15
+ end
16
+ end
17
+
18
+ class Dataset
19
+ include Equalizer.new(:name, :connection)
20
+ attr_reader :name, :connection
21
+ alias_method :ddb, :connection
22
+
23
+ def initialize(name, ddb, conditions = nil)
24
+ @name, @connection = name, ddb
25
+ @conditions = conditions || {}
26
+ end
27
+
28
+ ############# READ #############
29
+ def each(&block)
30
+ each_item({
31
+ consistent_read: true,
32
+ key_conditions: @conditions
33
+ }, &block)
34
+ end
35
+
36
+ def restrict(query = nil)
37
+ return self if query.nil?
38
+ conds = query_to_conditions(query)
39
+ conds = @conditions.merge(conds)
40
+ dup_as(Dataset, conditions: conds)
41
+ end
42
+
43
+ def index_restrict(index, query)
44
+ conds = query_to_conditions(query)
45
+ conds = @conditions.merge(conds)
46
+ dup_as(GlobalIndexDataset, index: index, conditions: conds)
47
+ end
48
+
49
+ ############# WRITE #############
50
+ def insert(hash)
51
+ connection.put_item({
52
+ table_name: name,
53
+ item: hash
54
+ })
55
+ end
56
+
57
+ def delete(hash)
58
+ connection.delete_item({
59
+ table_name: name,
60
+ key: hash_to_key(hash),
61
+ expected: to_expected(hash),
62
+ })
63
+ end
64
+
65
+ ############# HELPERS #############
66
+ private
67
+ def each_item(options, &block)
68
+ puts "Querying #{name} ...\nWith: #{options.inspect}"
69
+ connection.query(options.merge({
70
+ table_name: name
71
+ })).each_page do |page|
72
+ page[:items].each(&block)
73
+ end
74
+ end
75
+
76
+ def query_to_conditions(query)
77
+ Hash[query.map do |key, value|
78
+ [key, {
79
+ attribute_value_list: [value],
80
+ comparison_operator: "EQ"
81
+ }]
82
+ end]
83
+ end
84
+
85
+ def to_expected(hash)
86
+ hash && Hash[hash.map do |k, v|
87
+ [k, { value: v }]
88
+ end]
89
+ end
90
+
91
+ def hash_to_key(hash)
92
+ table_keys.each_with_object({}) do |k, out|
93
+ out[k] = hash[k] if hash.has_key?(k)
94
+ end
95
+ end
96
+
97
+ def table_keys
98
+ @table_keys ||= begin
99
+ resp = ddb.describe_table(table_name: name)
100
+ keys = resp.first[:table][:key_schema]
101
+ keys.map(&:attribute_name)
102
+ end
103
+ end
104
+
105
+ def dup_as(klass, opts = {})
106
+ table_keys # To populate keys once at top-level Dataset
107
+ vars = [:@name, :@connection, :@conditions, :@table_keys]
108
+ klass.allocate.tap do |out|
109
+ vars.each { |k| out.instance_variable_set(k, instance_variable_get(k)) }
110
+ opts.each { |k, v| out.instance_variable_set("@#{k}", v) }
111
+ end
112
+ end
113
+ end
114
+
115
+ # Dataset queried via a Global Index
116
+ class GlobalIndexDataset < Dataset
117
+ attr_accessor :index
118
+
119
+ def each(&block)
120
+ # Pull record IDs from Global Index
121
+ keys = []; each_item({
122
+ key_conditions: @conditions,
123
+ index_name: @index
124
+ }) { |hash| keys << hash_to_key(hash) }
125
+
126
+ # Bail if we have nothing
127
+ return if keys.empty?
128
+
129
+ # Query for the actual records
130
+ ddb.batch_get_item({
131
+ request_items: { name => { keys: keys } },
132
+ }).each_page do |page|
133
+ out = page[:responses][name]
134
+ out.each(&block)
135
+ end
136
+ end
137
+
138
+ end
139
+ end
140
+ end
@@ -0,0 +1,34 @@
1
+ require 'uri'
2
+ require 'rom/repository'
3
+
4
+ module Rom
5
+ module Dynamo
6
+ class Repository < ROM::Repository
7
+ def initialize(uri)
8
+ uri = URI.parse(uri)
9
+ @connection = Aws::DynamoDB::Client.new(region: uri.host)
10
+ @prefix = uri.path.gsub('/', '')
11
+ @datasets = {}
12
+ end
13
+
14
+ def use_logger(logger)
15
+ @logger = logger
16
+ end
17
+
18
+ def dataset(name)
19
+ name = "#{@prefix}#{name}"
20
+ @datasets[name] ||= Dataset.new(name, @connection)
21
+ end
22
+
23
+ def dataset?(name)
24
+ name = "#{@prefix}#{name}"
25
+ list = connection.list_tables
26
+ list[:table_names].include?(name)
27
+ end
28
+
29
+ def [](name)
30
+ @datasets[name]
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ module Rom
2
+ module Dynamo
3
+ VERSION = "0.1.4"
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rom/dynamo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rom-dynamo"
8
+ spec.version = Rom::Dynamo::VERSION
9
+ spec.authors = ["Michael Rykov"]
10
+ spec.email = ["mrykov@gmail.com"]
11
+
12
+ #if spec.respond_to?(:metadata)
13
+ # spec.metadata['allowed_push_host'] = "https://push.fury.io"
14
+ #end
15
+
16
+ spec.summary = %q{DynamoDB adapter for Ruby Object Mapper}
17
+ spec.description = %q{DynamoDB adapter for Ruby Object Mapper}
18
+ spec.homepage = "https://github.com/rykov/rom-dynamo"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ # Runtime
27
+ spec.add_runtime_dependency "rom", "~> 0.5"
28
+ spec.add_runtime_dependency "aws-sdk-core"
29
+
30
+ # Development
31
+ spec.add_development_dependency "bundler", "~> 1.8"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rom-dynamo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.4
5
+ platform: ruby
6
+ authors:
7
+ - Michael Rykov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rom
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-sdk-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: DynamoDB adapter for Ruby Object Mapper
70
+ email:
71
+ - mrykov@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - .travis.yml
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/rom-dynamo.rb
86
+ - lib/rom/dynamo.rb
87
+ - lib/rom/dynamo/commands.rb
88
+ - lib/rom/dynamo/relation.rb
89
+ - lib/rom/dynamo/repository.rb
90
+ - lib/rom/dynamo/version.rb
91
+ - rom-dynamo.gemspec
92
+ homepage: https://github.com/rykov/rom-dynamo
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: DynamoDB adapter for Ruby Object Mapper
116
+ test_files: []