dynamoid 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Dynamoid.gemspec +116 -0
- data/Gemfile +19 -0
- data/Gemfile.lock +58 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +86 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/dynamoid.rb +32 -0
- data/lib/dynamoid/adapter.rb +24 -0
- data/lib/dynamoid/adapter/aws_sdk.rb +100 -0
- data/lib/dynamoid/adapter/local.rb +77 -0
- data/lib/dynamoid/associations.rb +54 -0
- data/lib/dynamoid/associations/association.rb +80 -0
- data/lib/dynamoid/associations/belongs_to.rb +39 -0
- data/lib/dynamoid/associations/has_and_belongs_to_many.rb +34 -0
- data/lib/dynamoid/associations/has_many.rb +33 -0
- data/lib/dynamoid/associations/has_one.rb +36 -0
- data/lib/dynamoid/attributes.rb +37 -0
- data/lib/dynamoid/components.rb +25 -0
- data/lib/dynamoid/config.rb +19 -0
- data/lib/dynamoid/config/options.rb +74 -0
- data/lib/dynamoid/document.rb +35 -0
- data/lib/dynamoid/errors.rb +8 -0
- data/lib/dynamoid/fields.rb +32 -0
- data/lib/dynamoid/finders.rb +62 -0
- data/lib/dynamoid/indexes.rb +59 -0
- data/lib/dynamoid/persistence.rb +35 -0
- data/lib/dynamoid/relations.rb +21 -0
- data/spec/app/models/address.rb +5 -0
- data/spec/app/models/magazine.rb +6 -0
- data/spec/app/models/sponsor.rb +6 -0
- data/spec/app/models/subscription.rb +6 -0
- data/spec/app/models/user.rb +13 -0
- data/spec/dynamoid/adapter/aws_sdk_spec.rb +123 -0
- data/spec/dynamoid/adapter/local_spec.rb +150 -0
- data/spec/dynamoid/adapter_spec.rb +13 -0
- data/spec/dynamoid/associations/association_spec.rb +71 -0
- data/spec/dynamoid/associations/belongs_to_spec.rb +50 -0
- data/spec/dynamoid/associations/has_and_belongs_to_many_spec.rb +30 -0
- data/spec/dynamoid/associations/has_many_spec.rb +28 -0
- data/spec/dynamoid/associations/has_one_spec.rb +37 -0
- data/spec/dynamoid/associations_spec.rb +16 -0
- data/spec/dynamoid/attributes_spec.rb +43 -0
- data/spec/dynamoid/document_spec.rb +38 -0
- data/spec/dynamoid/fields_spec.rb +26 -0
- data/spec/dynamoid/finders_spec.rb +114 -0
- data/spec/dynamoid/indexes_spec.rb +54 -0
- data/spec/dynamoid/persistence_spec.rb +55 -0
- data/spec/dynamoid/relations_spec.rb +6 -0
- data/spec/dynamoid_spec.rb +5 -0
- data/spec/spec_helper.rb +52 -0
- metadata +204 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Dynamoid.gemspec
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "dynamoid"
|
8
|
+
s.version = "0.0.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Josh Symonds"]
|
12
|
+
s.date = "2012-02-26"
|
13
|
+
s.description = "Dynamoid is an ORM for Amazon's DynamoDB that supports offline development, associations, querying, and everything else you'd expect from an ActiveRecord-style replacement."
|
14
|
+
s.email = "josh@joshsymonds.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Dynamoid.gemspec",
|
23
|
+
"Gemfile",
|
24
|
+
"Gemfile.lock",
|
25
|
+
"LICENSE.txt",
|
26
|
+
"README.markdown",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/dynamoid.rb",
|
30
|
+
"lib/dynamoid/adapter.rb",
|
31
|
+
"lib/dynamoid/adapter/aws_sdk.rb",
|
32
|
+
"lib/dynamoid/adapter/local.rb",
|
33
|
+
"lib/dynamoid/associations.rb",
|
34
|
+
"lib/dynamoid/associations/association.rb",
|
35
|
+
"lib/dynamoid/associations/belongs_to.rb",
|
36
|
+
"lib/dynamoid/associations/has_and_belongs_to_many.rb",
|
37
|
+
"lib/dynamoid/associations/has_many.rb",
|
38
|
+
"lib/dynamoid/associations/has_one.rb",
|
39
|
+
"lib/dynamoid/attributes.rb",
|
40
|
+
"lib/dynamoid/components.rb",
|
41
|
+
"lib/dynamoid/config.rb",
|
42
|
+
"lib/dynamoid/config/options.rb",
|
43
|
+
"lib/dynamoid/document.rb",
|
44
|
+
"lib/dynamoid/errors.rb",
|
45
|
+
"lib/dynamoid/fields.rb",
|
46
|
+
"lib/dynamoid/finders.rb",
|
47
|
+
"lib/dynamoid/indexes.rb",
|
48
|
+
"lib/dynamoid/persistence.rb",
|
49
|
+
"lib/dynamoid/relations.rb",
|
50
|
+
"spec/app/models/address.rb",
|
51
|
+
"spec/app/models/magazine.rb",
|
52
|
+
"spec/app/models/sponsor.rb",
|
53
|
+
"spec/app/models/subscription.rb",
|
54
|
+
"spec/app/models/user.rb",
|
55
|
+
"spec/dynamoid/adapter/aws_sdk_spec.rb",
|
56
|
+
"spec/dynamoid/adapter/local_spec.rb",
|
57
|
+
"spec/dynamoid/adapter_spec.rb",
|
58
|
+
"spec/dynamoid/associations/association_spec.rb",
|
59
|
+
"spec/dynamoid/associations/belongs_to_spec.rb",
|
60
|
+
"spec/dynamoid/associations/has_and_belongs_to_many_spec.rb",
|
61
|
+
"spec/dynamoid/associations/has_many_spec.rb",
|
62
|
+
"spec/dynamoid/associations/has_one_spec.rb",
|
63
|
+
"spec/dynamoid/associations_spec.rb",
|
64
|
+
"spec/dynamoid/attributes_spec.rb",
|
65
|
+
"spec/dynamoid/document_spec.rb",
|
66
|
+
"spec/dynamoid/fields_spec.rb",
|
67
|
+
"spec/dynamoid/finders_spec.rb",
|
68
|
+
"spec/dynamoid/indexes_spec.rb",
|
69
|
+
"spec/dynamoid/persistence_spec.rb",
|
70
|
+
"spec/dynamoid/relations_spec.rb",
|
71
|
+
"spec/dynamoid_spec.rb",
|
72
|
+
"spec/spec_helper.rb"
|
73
|
+
]
|
74
|
+
s.homepage = "http://github.com/Veraticus/Dynamoid"
|
75
|
+
s.licenses = ["MIT"]
|
76
|
+
s.require_paths = ["lib"]
|
77
|
+
s.rubygems_version = "1.8.10"
|
78
|
+
s.summary = "Dynamoid is an ORM for Amazon's DynamoDB"
|
79
|
+
|
80
|
+
if s.respond_to? :specification_version then
|
81
|
+
s.specification_version = 3
|
82
|
+
|
83
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
84
|
+
s.add_runtime_dependency(%q<activemodel>, [">= 0"])
|
85
|
+
s.add_runtime_dependency(%q<tzinfo>, [">= 0"])
|
86
|
+
s.add_runtime_dependency(%q<aws-sdk>, [">= 0"])
|
87
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
88
|
+
s.add_development_dependency(%q<rake>, [">= 0"])
|
89
|
+
s.add_development_dependency(%q<rspec>, [">= 0"])
|
90
|
+
s.add_development_dependency(%q<bundler>, [">= 0"])
|
91
|
+
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
92
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
93
|
+
else
|
94
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
95
|
+
s.add_dependency(%q<tzinfo>, [">= 0"])
|
96
|
+
s.add_dependency(%q<aws-sdk>, [">= 0"])
|
97
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
98
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
99
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
100
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
101
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
102
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
103
|
+
end
|
104
|
+
else
|
105
|
+
s.add_dependency(%q<activemodel>, [">= 0"])
|
106
|
+
s.add_dependency(%q<tzinfo>, [">= 0"])
|
107
|
+
s.add_dependency(%q<aws-sdk>, [">= 0"])
|
108
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
109
|
+
s.add_dependency(%q<rake>, [">= 0"])
|
110
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
111
|
+
s.add_dependency(%q<bundler>, [">= 0"])
|
112
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
113
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
data/Gemfile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
gem 'activemodel'
|
3
|
+
gem 'tzinfo'
|
4
|
+
gem 'aws-sdk'
|
5
|
+
|
6
|
+
# Add dependencies required to use your gem here.
|
7
|
+
# Example:
|
8
|
+
# gem "activesupport", ">= 2.3.5"
|
9
|
+
|
10
|
+
# Add dependencies to develop your gem here.
|
11
|
+
# Include everything needed to run rake, tests, features, etc.
|
12
|
+
group :development do
|
13
|
+
gem "mocha"
|
14
|
+
gem "rake"
|
15
|
+
gem "rspec"
|
16
|
+
gem "bundler"
|
17
|
+
gem "jeweler"
|
18
|
+
gem "rcov"
|
19
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.1.3)
|
5
|
+
activesupport (= 3.1.3)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
i18n (~> 0.6)
|
8
|
+
activesupport (3.1.3)
|
9
|
+
multi_json (~> 1.0)
|
10
|
+
aws-sdk (1.3.5)
|
11
|
+
httparty (~> 0.7)
|
12
|
+
json (~> 1.4)
|
13
|
+
nokogiri (>= 1.4.4)
|
14
|
+
uuidtools (~> 2.1)
|
15
|
+
builder (3.0.0)
|
16
|
+
diff-lcs (1.1.3)
|
17
|
+
git (1.2.5)
|
18
|
+
httparty (0.8.1)
|
19
|
+
multi_json
|
20
|
+
multi_xml
|
21
|
+
i18n (0.6.0)
|
22
|
+
jeweler (1.6.4)
|
23
|
+
bundler (~> 1.0)
|
24
|
+
git (>= 1.2.5)
|
25
|
+
rake
|
26
|
+
json (1.6.5)
|
27
|
+
metaclass (0.0.1)
|
28
|
+
mocha (0.10.0)
|
29
|
+
metaclass (~> 0.0.1)
|
30
|
+
multi_json (1.0.4)
|
31
|
+
multi_xml (0.4.1)
|
32
|
+
nokogiri (1.5.0)
|
33
|
+
rake (0.9.2.2)
|
34
|
+
rcov (0.9.11)
|
35
|
+
rspec (2.8.0)
|
36
|
+
rspec-core (~> 2.8.0)
|
37
|
+
rspec-expectations (~> 2.8.0)
|
38
|
+
rspec-mocks (~> 2.8.0)
|
39
|
+
rspec-core (2.8.0)
|
40
|
+
rspec-expectations (2.8.0)
|
41
|
+
diff-lcs (~> 1.1.2)
|
42
|
+
rspec-mocks (2.8.0)
|
43
|
+
tzinfo (0.3.31)
|
44
|
+
uuidtools (2.1.2)
|
45
|
+
|
46
|
+
PLATFORMS
|
47
|
+
ruby
|
48
|
+
|
49
|
+
DEPENDENCIES
|
50
|
+
activemodel
|
51
|
+
aws-sdk
|
52
|
+
bundler
|
53
|
+
jeweler
|
54
|
+
mocha
|
55
|
+
rake
|
56
|
+
rcov
|
57
|
+
rspec
|
58
|
+
tzinfo
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Josh Symonds
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
# Dynamoid
|
2
|
+
|
3
|
+
Dynamoid is an ORM for Amazon's DynamoDB for Ruby applications. It provides similar functionality to ActiveRecord and improves on Amazon's existing [HashModel](http://docs.amazonwebservices.com/AWSRubySDK/latest/AWS/Record/HashModel.html) by providing better searching tools, native association support, and a local adapter for offline development.
|
4
|
+
|
5
|
+
## Warning!
|
6
|
+
|
7
|
+
I'm still working on this gem a lot. You can only use the old-school ActiveRecord style finders like ```find_all_by_<attribute_name>``` or directly finding by an ID.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Installing Dynamoid is pretty simple. First include the Gem in your Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'dynamoid'
|
15
|
+
```
|
16
|
+
|
17
|
+
Then you need to initialize it to get it going, so put code similar to this somewhere (a Rails initializer would be a great place for this if you're using Rails):
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
Dynamoid.configure do |config|
|
21
|
+
config.adapter = 'local' # This adapter allows offline development without connecting to the DynamoDB servers.
|
22
|
+
# config.adapter = 'aws_sdk' # This adapter establishes a connection to the DynamoDB servers using's Amazon's own awful AWS gem.
|
23
|
+
# config.access_key = 'access_key' # If connecting to DynamoDB, your access key is required.
|
24
|
+
# config.secret_key = 'secret_key' # So is your secret key.
|
25
|
+
config.namespace = "dynamoid_#{Rails.application.class.parent_name}_#{Rails.env}" # To namespace tables created by Dynamoid from other tables you might have.
|
26
|
+
config.warn_on_scan = true # Output a warning to stdout when you perform a scan rather than a query on a table
|
27
|
+
end
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
Once you have the configuration set up, just define models like this:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class User
|
35
|
+
include Dynamoid::Document # Documents automatically receive an 'id' field: you don't have to specify it.
|
36
|
+
|
37
|
+
field :name # Every field you have on the object must be specified here.
|
38
|
+
field :email # If you have fields that aren't specified they won't be attached to the object as methods.
|
39
|
+
|
40
|
+
index :name # Only specify indexes if you intend to perform queries on the specified fields.
|
41
|
+
index :email # Fields without indexes enjoy extremely poor performance as they must use
|
42
|
+
index [:name, :email] # scan rather than query.
|
43
|
+
|
44
|
+
has_many :addresses # Associations do not accept any options presently. The referenced
|
45
|
+
# model name must match exactly and the foreign key is always id.
|
46
|
+
belongs_to :group # If they detect a matching association on
|
47
|
+
# the referenced model they'll auto-update that association.
|
48
|
+
has_one :role # Contrary to ActiveRecord, all associations are stored on the object,
|
49
|
+
# even if it seems like they'd be a foreign key association.
|
50
|
+
has_and_belongs_to_many :friends
|
51
|
+
# There's no concept of embedding models yet but it's coming!
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
### Usage
|
56
|
+
|
57
|
+
Right now, you can only do a couple things with this amazing functionality:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
u = User.new(:name => 'Josh')
|
61
|
+
u.email = 'josh@joshsymonds.com'
|
62
|
+
u.save
|
63
|
+
|
64
|
+
address = u.addresses.create
|
65
|
+
address.city = 'Chicago'
|
66
|
+
address.save
|
67
|
+
|
68
|
+
u == User.find(u.id)
|
69
|
+
u == User.find_by_name('Josh')
|
70
|
+
u.addresses == User.find_by_name_and_email('Josh','josh@joshsymonds.com').addresses
|
71
|
+
```
|
72
|
+
|
73
|
+
Not super exciting yet, true... but it's getting there!
|
74
|
+
|
75
|
+
## Credits
|
76
|
+
|
77
|
+
Dynamoid borrows code, structure, and even its name very liberally from the truly amazing [Mongoid](https://github.com/mongoid/mongoid). Without Mongoid to crib from none of this would have been possible, and I hope they don't mind me reusing their very awesome ideas to make DynamoDB just as accessible to the Ruby world as MongoDB.
|
78
|
+
|
79
|
+
## Running the tests
|
80
|
+
|
81
|
+
The tests can be run in the simple predictable way with ```rake```. However, if you provide environment variables for ACCESS_KEY and SECRET_KEY, the tests will use the aws_sdk adapter rather than the local adapter: ```ACCESS_KEY=<accesskey> SECRET_KEY=<secretkey> rake```. Keep in mind this takes much, much longer than the local tests.
|
82
|
+
|
83
|
+
## Copyright
|
84
|
+
|
85
|
+
Copyright (c) 2012 Josh Symonds. See LICENSE.txt for further details.
|
86
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "dynamoid"
|
18
|
+
gem.homepage = "http://github.com/Veraticus/Dynamoid"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = "Dynamoid is an ORM for Amazon's DynamoDB"
|
21
|
+
gem.description = "Dynamoid is an ORM for Amazon's DynamoDB that supports offline development, associations, querying, and everything else you'd expect from an ActiveRecord-style replacement."
|
22
|
+
gem.email = "josh@joshsymonds.com"
|
23
|
+
gem.authors = ["Josh Symonds"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "dynamoid #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
data/lib/dynamoid.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require "delegate"
|
2
|
+
require "time"
|
3
|
+
require "securerandom"
|
4
|
+
require "active_support/core_ext"
|
5
|
+
require 'active_support/json'
|
6
|
+
require "active_support/inflector"
|
7
|
+
require "active_support/lazy_load_hooks"
|
8
|
+
require "active_support/time_with_zone"
|
9
|
+
require "active_model"
|
10
|
+
|
11
|
+
require 'dynamoid/errors'
|
12
|
+
require 'dynamoid/attributes'
|
13
|
+
require 'dynamoid/fields'
|
14
|
+
require 'dynamoid/indexes'
|
15
|
+
require 'dynamoid/associations'
|
16
|
+
require 'dynamoid/persistence'
|
17
|
+
require 'dynamoid/finders'
|
18
|
+
require 'dynamoid/config'
|
19
|
+
require 'dynamoid/components'
|
20
|
+
require 'dynamoid/document'
|
21
|
+
require 'dynamoid/adapter'
|
22
|
+
|
23
|
+
module Dynamoid
|
24
|
+
extend self
|
25
|
+
|
26
|
+
def configure
|
27
|
+
block_given? ? yield(Config) : Config
|
28
|
+
Dynamoid::Adapter.reconnect!
|
29
|
+
end
|
30
|
+
alias :config :configure
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
module Dynamoid #:nodoc:
|
3
|
+
|
4
|
+
module Adapter
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def adapter
|
8
|
+
reconnect! unless @adapter
|
9
|
+
@adapter
|
10
|
+
end
|
11
|
+
|
12
|
+
def reconnect!
|
13
|
+
require "dynamoid/adapter/#{Dynamoid::Config.adapter}" unless Dynamoid::Adapter.const_defined?(Dynamoid::Config.adapter.camelcase)
|
14
|
+
@adapter = Dynamoid::Adapter.const_get(Dynamoid::Config.adapter.camelcase)
|
15
|
+
@adapter.connect! if @adapter.respond_to?(:connect!)
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(method, *args)
|
19
|
+
return @adapter.send(method, *args) if @adapter.respond_to?(method)
|
20
|
+
super
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'aws'
|
2
|
+
|
3
|
+
module Dynamoid
|
4
|
+
module Adapter
|
5
|
+
module AwsSdk
|
6
|
+
extend self
|
7
|
+
@@connection = nil
|
8
|
+
|
9
|
+
def connect!
|
10
|
+
@@connection = AWS::DynamoDB.new(:access_key_id => Dynamoid::Config.access_key, :secret_access_key => Dynamoid::Config.secret_key)
|
11
|
+
end
|
12
|
+
|
13
|
+
def connection
|
14
|
+
@@connection
|
15
|
+
end
|
16
|
+
|
17
|
+
# BatchGetItem
|
18
|
+
def batch_get_item(options)
|
19
|
+
batch = AWS::DynamoDB::BatchGet.new(:config => @@connection.config)
|
20
|
+
hash = Hash.new{|h, k| h[k] = []}
|
21
|
+
return hash if options.all?{|k, v| v.empty?}
|
22
|
+
options.each do |t, ids|
|
23
|
+
batch.table(t, :all, Array(ids)) unless ids.nil? || ids.empty?
|
24
|
+
end
|
25
|
+
batch.each do |table_name, attributes|
|
26
|
+
hash[table_name] << attributes.symbolize_keys!
|
27
|
+
end
|
28
|
+
hash
|
29
|
+
end
|
30
|
+
|
31
|
+
# CreateTable
|
32
|
+
def create_table(table_name, key)
|
33
|
+
table = @@connection.tables.create(table_name, 10, 5, :hash_key => {key.to_sym => :string})
|
34
|
+
sleep 0.5 while table.status == :creating
|
35
|
+
return table
|
36
|
+
end
|
37
|
+
|
38
|
+
# DeleteItem
|
39
|
+
def delete_item(table_name, key)
|
40
|
+
table = @@connection.tables[table_name]
|
41
|
+
table.load_schema
|
42
|
+
result = table.items[key]
|
43
|
+
result.delete unless result.attributes.to_h.empty?
|
44
|
+
true
|
45
|
+
end
|
46
|
+
|
47
|
+
# DeleteTable
|
48
|
+
def delete_table(table_name)
|
49
|
+
@@connection.tables[table_name].delete
|
50
|
+
end
|
51
|
+
|
52
|
+
# DescribeTable
|
53
|
+
|
54
|
+
# GetItem
|
55
|
+
def get_item(table_name, key)
|
56
|
+
table = @@connection.tables[table_name]
|
57
|
+
table.load_schema
|
58
|
+
result = table.items[key].attributes.to_h
|
59
|
+
if result.empty?
|
60
|
+
nil
|
61
|
+
else
|
62
|
+
result.symbolize_keys!
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# ListTables
|
67
|
+
def list_tables
|
68
|
+
@@connection.tables.collect(&:name)
|
69
|
+
end
|
70
|
+
|
71
|
+
# PutItem
|
72
|
+
def put_item(table_name, object)
|
73
|
+
table = @@connection.tables[table_name]
|
74
|
+
table.load_schema
|
75
|
+
table.items.create(object.delete_if{|k, v| v.nil? || v.empty?})
|
76
|
+
end
|
77
|
+
|
78
|
+
# Query
|
79
|
+
def query(table_name, id)
|
80
|
+
get_item(table_name, id)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Scan
|
84
|
+
def scan(table_name, scan_hash)
|
85
|
+
table = @@connection.tables[table_name]
|
86
|
+
table.load_schema
|
87
|
+
results = []
|
88
|
+
table.items.select do |data|
|
89
|
+
attributes = data.attributes.symbolize_keys!
|
90
|
+
results << attributes if scan_hash.all?{|k, v| !attributes[k].nil? && attributes[k] == v}
|
91
|
+
end
|
92
|
+
results
|
93
|
+
end
|
94
|
+
|
95
|
+
# UpdateItem
|
96
|
+
|
97
|
+
# UpdateTable
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|