synamoid 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.gitignore +67 -0
- data/.rspec +2 -0
- data/.travis.yml +15 -0
- data/CHANGELOG.md +48 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/README.md +443 -0
- data/Rakefile +64 -0
- data/dynamoid.gemspec +53 -0
- data/lib/dynamoid.rb +53 -0
- data/lib/dynamoid/adapter.rb +190 -0
- data/lib/dynamoid/adapter_plugin/aws_sdk_v2.rb +892 -0
- data/lib/dynamoid/associations.rb +106 -0
- data/lib/dynamoid/associations/association.rb +116 -0
- data/lib/dynamoid/associations/belongs_to.rb +44 -0
- data/lib/dynamoid/associations/has_and_belongs_to_many.rb +40 -0
- data/lib/dynamoid/associations/has_many.rb +39 -0
- data/lib/dynamoid/associations/has_one.rb +39 -0
- data/lib/dynamoid/associations/many_association.rb +193 -0
- data/lib/dynamoid/associations/single_association.rb +69 -0
- data/lib/dynamoid/components.rb +37 -0
- data/lib/dynamoid/config.rb +58 -0
- data/lib/dynamoid/config/options.rb +78 -0
- data/lib/dynamoid/criteria.rb +29 -0
- data/lib/dynamoid/criteria/chain.rb +214 -0
- data/lib/dynamoid/dirty.rb +47 -0
- data/lib/dynamoid/document.rb +201 -0
- data/lib/dynamoid/errors.rb +66 -0
- data/lib/dynamoid/fields.rb +164 -0
- data/lib/dynamoid/finders.rb +199 -0
- data/lib/dynamoid/identity_map.rb +92 -0
- data/lib/dynamoid/indexes.rb +273 -0
- data/lib/dynamoid/middleware/identity_map.rb +16 -0
- data/lib/dynamoid/persistence.rb +359 -0
- data/lib/dynamoid/validations.rb +63 -0
- data/lib/dynamoid/version.rb +3 -0
- metadata +266 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a8ac7dcf3f6d93dc42cc10d5b0eecb940ac39297
|
4
|
+
data.tar.gz: 89f8d6b38bd81b5dfe823f2738a62031d4352dda
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 919a8a9b267f51c76a816a5360b2c30df74cc36d3c2a44be09ea8ce85c33c6aec80646b04ab73d32a387648319ff52aa8af9d85dcaef0fa2d78300ee9075e4df
|
7
|
+
data.tar.gz: 18eba82fb6d53c96a30129ebd18760659a13f72005cf9ceb29e7ab7021f85b59252553ec9c0320e89e2535732bd0a0890a16bab86a05eb4d2b32135eb352e872
|
data/.document
ADDED
data/.gitignore
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
.project
|
2
|
+
|
3
|
+
# rcov generated
|
4
|
+
coverage
|
5
|
+
|
6
|
+
# rdoc generated
|
7
|
+
rdoc
|
8
|
+
|
9
|
+
# yardoc generated
|
10
|
+
.yardoc
|
11
|
+
/_yardoc/
|
12
|
+
|
13
|
+
# bundler
|
14
|
+
/.bundle/
|
15
|
+
|
16
|
+
# jeweler generated
|
17
|
+
/pkg/
|
18
|
+
|
19
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
20
|
+
#
|
21
|
+
# * Create a file at ~/.gitignore
|
22
|
+
# * Include files you want ignored
|
23
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
24
|
+
#
|
25
|
+
# After doing this, these files will be ignored in all your git projects,
|
26
|
+
# saving you from having to 'pollute' every project you touch with them
|
27
|
+
#
|
28
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
29
|
+
#
|
30
|
+
# For MacOS:
|
31
|
+
#
|
32
|
+
#.DS_Store
|
33
|
+
|
34
|
+
# For TextMate
|
35
|
+
#*.tmproj
|
36
|
+
#tmtags
|
37
|
+
|
38
|
+
# For emacs:
|
39
|
+
#*~
|
40
|
+
#\#*
|
41
|
+
#.\#*
|
42
|
+
|
43
|
+
# For vim:
|
44
|
+
#*.swp
|
45
|
+
|
46
|
+
# For redcar:
|
47
|
+
#.redcar
|
48
|
+
|
49
|
+
# For rubinius:
|
50
|
+
#*.rbc
|
51
|
+
|
52
|
+
# for RVM
|
53
|
+
.rvmrc
|
54
|
+
|
55
|
+
# For RubyMine:
|
56
|
+
/.idea/
|
57
|
+
|
58
|
+
# For Ctags
|
59
|
+
.gemtags
|
60
|
+
.tags
|
61
|
+
.tags_sorted_by_file
|
62
|
+
|
63
|
+
Gemfile.lock
|
64
|
+
/doc/
|
65
|
+
/spec/reports/
|
66
|
+
/tmp/
|
67
|
+
/spec/DynamoDBLocal-latest/
|
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- ruby-2.0.0-p648
|
4
|
+
- ruby-2.1.10
|
5
|
+
- ruby-2.2.5
|
6
|
+
- ruby-2.3.1
|
7
|
+
- jruby-9.1.4.0
|
8
|
+
before_install: gem install bundler -v 1.12.5
|
9
|
+
install:
|
10
|
+
- wget http://dynamodb-local.s3-website-us-west-2.amazonaws.com/dynamodb_local_latest.zip --quiet -O spec/dynamodb_temp.zip
|
11
|
+
- unzip -qq spec/dynamodb_temp.zip -d spec/DynamoDBLocal-latest
|
12
|
+
- rm spec/dynamodb_temp.zip
|
13
|
+
script:
|
14
|
+
- bundle install
|
15
|
+
- bundle exec rake unattended_spec
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# 1.2.1
|
2
|
+
|
3
|
+
* Remove accidental Gemfile.lock; fix .gitignore (#95, @pboling)
|
4
|
+
* Allow options to put_items (#95, @alexperto)
|
5
|
+
* Support range key in secondary index queries (#95, @pboling)
|
6
|
+
* Better handling of options generally (#95, @pboling)
|
7
|
+
* Support for batch_delete_item API (#95, @pboling)
|
8
|
+
* Support for batch_write_item API (#95, @alexperto)
|
9
|
+
|
10
|
+
# 1.2.0
|
11
|
+
|
12
|
+
* Add create_table_syncronously, and sync: option to regular create_table (@pboling)
|
13
|
+
* make required for tables created with secondary indexes
|
14
|
+
* Expose and fix truncate method on adapter (#52, @pcorpet)
|
15
|
+
* Enable saving without updating timestamps (#58, @cignoir)
|
16
|
+
* Fix projected attributes by checking for :include (#56, @yoshida_tetsuhiro)
|
17
|
+
* Make behavior of association where method closer to AR by cloning instead of modifying (#51, @pcorpet)
|
18
|
+
* Add boolean field presence validator (#50, @pcorpet)
|
19
|
+
* Add association build method (#49, @pcorpet)
|
20
|
+
* Fix association create method (#47, #48, @pcorpet)
|
21
|
+
* Support range_between (#42, @ayemos)
|
22
|
+
* Fix problems with range query (#42, @ayemos)
|
23
|
+
* Don't prefix table names when namespace is nil (#40, @brenden)
|
24
|
+
* Added basic secondary index support (#34, @sumocoder)
|
25
|
+
* Fix query attribute behavior for booleans (#35, @amirmanji)
|
26
|
+
* Ignore unknown fields on model initialize (PR #33, @sumocoder)
|
27
|
+
|
28
|
+
# 1.1.0
|
29
|
+
|
30
|
+
* Added support for optimistic locking on delete (PR #29, @sumocoder)
|
31
|
+
* upgrade concurrent-ruby requirement to 1.0 (PR #31, @keithmgould)
|
32
|
+
|
33
|
+
# 1.0.0
|
34
|
+
|
35
|
+
* Add support for AWS SDK v2.
|
36
|
+
* Add support for custom class type for fields.
|
37
|
+
* Remove partitioning support.
|
38
|
+
* Remove support for Dynamoid's (pseudo)indexes, now that DynamoDB offers
|
39
|
+
local and global indexes.
|
40
|
+
* Rename :float field type to :number.
|
41
|
+
* Rename Chain#limit to Chain#eval_limit.
|
42
|
+
|
43
|
+
Housekeeping:
|
44
|
+
|
45
|
+
* Switch from `fake_dynamo` for unit tests to DynamoDBLocal. This is the new authoritative
|
46
|
+
implementation of DynamoDB for testing, and it supports AWS SDK v2.
|
47
|
+
* Use Travis CI to auto-run unit tests on multiple Rubies.
|
48
|
+
* Randomize spec order.
|
data/Gemfile
ADDED
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.md
ADDED
@@ -0,0 +1,443 @@
|
|
1
|
+
# Dynamoid
|
2
|
+
|
3
|
+
Dynamoid is an ORM for Amazon's DynamoDB for Ruby applications. It
|
4
|
+
provides similar functionality to ActiveRecord and improves on
|
5
|
+
Amazon's existing
|
6
|
+
[HashModel](http://docs.amazonwebservices.com/AWSRubySDK/latest/AWS/Record/HashModel.html)
|
7
|
+
by providing better searching tools and native association support.
|
8
|
+
|
9
|
+
DynamoDB is not like other document-based databases you might know, and is very different indeed from relational databases. It sacrifices anything beyond the simplest relational queries and transactional support to provide a fast, cost-efficient, and highly durable storage solution. If your database requires complicated relational queries and transaction support, then this modest Gem cannot provide them for you, and neither can DynamoDB. In those cases you would do better to look elsewhere for your database needs.
|
10
|
+
|
11
|
+
But if you want a fast, scalable, simple, easy-to-use database (and a Gem that supports it) then look no further!
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Installing Dynamoid is pretty simple. First include the Gem in your Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'dynamoid', '~> 1'
|
19
|
+
```
|
20
|
+
## Prerequisities
|
21
|
+
|
22
|
+
Dynamoid depends on the aws-sdk, and this is tested on the current version of aws-sdk (~> 2), rails (~> 4).
|
23
|
+
Hence the configuration as needed for aws to work will be dealt with by aws setup.
|
24
|
+
|
25
|
+
Here are the steps to setup aws-sdk.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'aws-sdk', '~>2'
|
29
|
+
```
|
30
|
+
|
31
|
+
(or) include the aws-sdk in your Gemfile.
|
32
|
+
|
33
|
+
**NOTE:** Dynamoid-1.0 doesn't support aws-sdk Version 1 (Use Dynamoid Major Version 0 for aws-sdk 1)
|
34
|
+
|
35
|
+
Configure AWS access:
|
36
|
+
[Reference](https://github.com/aws/aws-sdk-ruby)
|
37
|
+
|
38
|
+
For example, to configure AWS access:
|
39
|
+
|
40
|
+
Create config/initializers/aws.rb as follows:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
|
44
|
+
Aws.config.update({
|
45
|
+
region: 'us-west-2',
|
46
|
+
credentials: Aws::Credentials.new('REPLACE_WITH_ACCESS_KEY_ID', 'REPLACE_WITH_SECRET_ACCESS_KEY'),
|
47
|
+
})
|
48
|
+
|
49
|
+
```
|
50
|
+
|
51
|
+
For a full list of the DDB regions, you can go
|
52
|
+
[here](http://docs.aws.amazon.com/general/latest/gr/rande.html#ddb_region).
|
53
|
+
|
54
|
+
Then you need to initialize Dynamoid config to get it going. Put code similar to this somewhere (a Rails initializer would be a great place for this if you're using Rails):
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
Dynamoid.configure do |config|
|
58
|
+
config.adapter = 'aws_sdk_v2' # This adapter establishes a connection to the DynamoDB servers using Amazon's own AWS gem.
|
59
|
+
config.namespace = "dynamoid_app_development" # To namespace tables created by Dynamoid from other tables you might have. Set to nil to avoid namespacing.
|
60
|
+
config.warn_on_scan = true # Output a warning to the logger when you perform a scan rather than a query on a table.
|
61
|
+
config.read_capacity = 5 # Read capacity for your tables
|
62
|
+
config.write_capacity = 5 # Write capacity for your tables
|
63
|
+
config.endpoint = 'http://localhost:3000' # [Optional]. If provided, it communicates with the DB listening at the endpoint. This is useful for testing with [Amazon Local DB] (http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Tools.DynamoDBLocal.html).
|
64
|
+
end
|
65
|
+
|
66
|
+
```
|
67
|
+
|
68
|
+
Once you have the configuration set up, you need to move on to making models.
|
69
|
+
|
70
|
+
## Setup
|
71
|
+
|
72
|
+
You *must* include ```Dynamoid::Document``` in every Dynamoid model.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
class User
|
76
|
+
include Dynamoid::Document
|
77
|
+
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
### Table
|
82
|
+
|
83
|
+
Dynamoid has some sensible defaults for you when you create a new table, including the table name and the primary key column. But you can change those if you like on table creation.
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class User
|
87
|
+
include Dynamoid::Document
|
88
|
+
|
89
|
+
table :name => :awesome_users, :key => :user_id, :read_capacity => 5, :write_capacity => 5
|
90
|
+
end
|
91
|
+
```
|
92
|
+
|
93
|
+
These fields will not change an existing table: so specifying a new read_capacity and write_capacity here only works correctly for entirely new tables. Similarly, while Dynamoid will look for a table named `awesome_users` in your namespace, it won't change any existing tables to use that name; and if it does find a table with the correct name, it won't change its hash key, which it expects will be user_id. If this table doesn't exist yet, however, Dynamoid will create it with these options.
|
94
|
+
|
95
|
+
### Fields
|
96
|
+
|
97
|
+
You'll have to define all the fields on the model and the data type of each field. Every field on the object must be included here; if you miss any they'll be completely bypassed during DynamoDB's initialization and will not appear on the model objects.
|
98
|
+
|
99
|
+
By default, fields are assumed to be of type ```:string```. Other built-in types are
|
100
|
+
```:integer```, ```:number```, ```:set```, ```:array```, ```:datetime```, ```:boolean```, and ```:serialized```.
|
101
|
+
If built-in types do not suit you, you can use a custom field type represented by an arbitrary class, provided that the class supports a compatible serialization interface.
|
102
|
+
The primary use case for using a custom field type is to represent your business logic with high-level types, while ensuring portability or backward-compatibility of the serialized representation.
|
103
|
+
|
104
|
+
You get magic columns of id (string), created_at (datetime), and updated_at (datetime) for free.
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
class User
|
108
|
+
include Dynamoid::Document
|
109
|
+
|
110
|
+
field :name
|
111
|
+
field :email
|
112
|
+
field :rank, :integer
|
113
|
+
field :number, :number
|
114
|
+
field :joined_at, :datetime
|
115
|
+
field :hash, :serialized
|
116
|
+
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
You can optionally set a default value on a field using either a plain value or a lambda:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
field :actions_taken, :integer, {default: 0}
|
124
|
+
field :joined_at, :datetime, {default: ->(){Time.now}}
|
125
|
+
```
|
126
|
+
|
127
|
+
To use a custom type for a field, suppose you have a `Money` type.
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
class Money
|
131
|
+
# ... your business logic ...
|
132
|
+
|
133
|
+
def dynamoid_dump
|
134
|
+
"serialized representation as a string"
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.dynamoid_load(serialized_str)
|
138
|
+
# parse serialized representation and return a Money instance
|
139
|
+
Money.new(...)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
class User
|
144
|
+
include Dynamoid::Document
|
145
|
+
|
146
|
+
field :balance, Money
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
If you want to use a third-party class (which does not support `#dynamoid_dump` and `.dynamoid_load`)
|
151
|
+
as your field type, you can use an adapter class providing `.dynamoid_dump` and `.dynamoid_load` class methods
|
152
|
+
for your third-party class. (`.dynamoid_load` can remain the same from the previous example; here we just
|
153
|
+
add a level of indirection for serializing.) Example:
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
# Third-party Money class
|
157
|
+
class Money; end
|
158
|
+
|
159
|
+
class MoneyAdapter
|
160
|
+
def self.dynamoid_load(money_serialized_str)
|
161
|
+
Money.new(...)
|
162
|
+
end
|
163
|
+
|
164
|
+
def self.dynamoid_dump(money_obj)
|
165
|
+
money_obj.value.to_s
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
class User
|
170
|
+
include Dynamoid::Document
|
171
|
+
|
172
|
+
field :balance, MoneyAdapter
|
173
|
+
end
|
174
|
+
```
|
175
|
+
|
176
|
+
Lastly, you can control the data type of your custom-class-backed field at the DynamoDB level.
|
177
|
+
This is especially important if you want to use your custom field as a numeric range or for
|
178
|
+
number-oriented queries. By default custom fields are persisted as a string attribute, but
|
179
|
+
your custom class can override this with a `.dynamoid_field_type` class method, which would
|
180
|
+
return either `:string` or `:number`.
|
181
|
+
(DynamoDB supports some other attribute types, but Dynamoid does not yet.)
|
182
|
+
|
183
|
+
|
184
|
+
### Associations
|
185
|
+
|
186
|
+
Just like in ActiveRecord (or your other favorite ORM), Dynamoid uses associations to create links between models.
|
187
|
+
|
188
|
+
The only supported associations (so far) are ```has_many```, ```has_one```, ```has_and_belongs_to_many```, and ```belongs_to```. Associations are very simple to create: just specify the type, the name, and then any options you'd like to pass to the association. If there's an inverse association either inferred or specified directly, Dynamoid will update both objects to point at each other.
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
class User
|
192
|
+
include Dynamoid::Document
|
193
|
+
|
194
|
+
...
|
195
|
+
|
196
|
+
has_many :addresses
|
197
|
+
has_many :students, :class => User
|
198
|
+
belongs_to :teacher, :class_name => :user
|
199
|
+
belongs_to :group
|
200
|
+
has_one :role
|
201
|
+
has_and_belongs_to_many :friends, :inverse_of => :friending_users
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
class Address
|
206
|
+
include Dynamoid::Document
|
207
|
+
|
208
|
+
...
|
209
|
+
|
210
|
+
belongs_to :address # Automatically links up with the user model
|
211
|
+
|
212
|
+
end
|
213
|
+
```
|
214
|
+
|
215
|
+
Contrary to what you'd expect, association information is always contained on the object specifying the association, even if it seems like the association has a foreign key. This is a side effect of DynamoDB's structure: it's very difficult to find foreign keys without an index. Usually you won't find this to be a problem, but it does mean that association methods that build new models will not work correctly -- for example, ```user.addresses.new``` returns an address that is not associated to the user. We'll be correcting this soon.
|
216
|
+
|
217
|
+
### Validations
|
218
|
+
|
219
|
+
Dynamoid bakes in ActiveModel validations, just like ActiveRecord does.
|
220
|
+
|
221
|
+
```ruby
|
222
|
+
class User
|
223
|
+
include Dynamoid::Document
|
224
|
+
|
225
|
+
...
|
226
|
+
|
227
|
+
validates_presence_of :name
|
228
|
+
validates_format_of :email, :with => /@/
|
229
|
+
end
|
230
|
+
```
|
231
|
+
|
232
|
+
To see more usage and examples of ActiveModel validations, check out the [ActiveModel validation documentation](http://api.rubyonrails.org/classes/ActiveModel/Validations.html).
|
233
|
+
|
234
|
+
### Callbacks
|
235
|
+
|
236
|
+
Dynamoid also employs ActiveModel callbacks. Right now, callbacks are defined on ```save```, ```update```, ```destroy```, which allows you to do ```before_``` or ```after_``` any of those.
|
237
|
+
|
238
|
+
```ruby
|
239
|
+
class User
|
240
|
+
include Dynamoid::Document
|
241
|
+
|
242
|
+
...
|
243
|
+
|
244
|
+
before_save :set_default_password
|
245
|
+
after_create :notify_friends
|
246
|
+
after_destroy :delete_addresses
|
247
|
+
end
|
248
|
+
```
|
249
|
+
|
250
|
+
## Usage
|
251
|
+
|
252
|
+
### Object Creation
|
253
|
+
|
254
|
+
Dynamoid's syntax is generally very similar to ActiveRecord's. Making new objects is simple:
|
255
|
+
|
256
|
+
```ruby
|
257
|
+
u = User.new(:name => 'Josh')
|
258
|
+
u.email = 'josh@joshsymonds.com'
|
259
|
+
u.save
|
260
|
+
```
|
261
|
+
|
262
|
+
Save forces persistence to the datastore: a unique ID is also assigned, but it is a string and not an auto-incrementing number.
|
263
|
+
|
264
|
+
```ruby
|
265
|
+
u.id # => "3a9f7216-4726-4aea-9fbc-8554ae9292cb"
|
266
|
+
```
|
267
|
+
|
268
|
+
To use associations, you use association methods very similar to ActiveRecord's:
|
269
|
+
|
270
|
+
```ruby
|
271
|
+
address = u.addresses.create
|
272
|
+
address.city = 'Chicago'
|
273
|
+
address.save
|
274
|
+
```
|
275
|
+
|
276
|
+
### Querying
|
277
|
+
|
278
|
+
Querying can be done in one of three ways:
|
279
|
+
|
280
|
+
```ruby
|
281
|
+
Address.find(address.id) # Find directly by ID.
|
282
|
+
Address.where(:city => 'Chicago').all # Find by any number of matching criteria... though presently only "where" is supported.
|
283
|
+
Address.find_by_city('Chicago') # The same as above, but using ActiveRecord's older syntax.
|
284
|
+
```
|
285
|
+
|
286
|
+
And you can also query on associations:
|
287
|
+
|
288
|
+
```ruby
|
289
|
+
u.addresses.where(:city => 'Chicago').all
|
290
|
+
```
|
291
|
+
|
292
|
+
But keep in mind Dynamoid -- and document-based storage systems in general -- are not drop-in replacements for existing relational databases. The above query does not efficiently perform a conditional join, but instead finds all the user's addresses and naively filters them in Ruby. For large associations this is a performance hit compared to relational database engines.
|
293
|
+
|
294
|
+
You can also limit the number of evaluated records, or select a record from which to start, to support pagination:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
Address.eval_limit(5).start(address) # Only 5 addresses.
|
298
|
+
```
|
299
|
+
|
300
|
+
For large queries that return many rows, Dynamoid can use AWS' support for requesting documents in batches:
|
301
|
+
|
302
|
+
```ruby
|
303
|
+
#Do some maintenance on the entire table without flooding DynamoDB
|
304
|
+
Address.all(batch_size: 100).each { |address| address.do_some_work; sleep(0.01) }
|
305
|
+
Address.limit(10_000).batch(100). each { … } #batch specified as part of a chain
|
306
|
+
```
|
307
|
+
|
308
|
+
### Consistent Reads
|
309
|
+
|
310
|
+
Querying supports consistent reading. By default, DynamoDB reads are eventually consistent: if you do a write and then a read immediately afterwards, the results of the previous write may not be reflected. If you need to do a consistent read (that is, you need to read the results of a write immediately) you can do so, but keep in mind that consistent reads are twice as expensive as regular reads for DynamoDB.
|
311
|
+
|
312
|
+
```ruby
|
313
|
+
Address.find(address.id, :consistent_read => true) # Find an address, ensure the read is consistent.
|
314
|
+
Address.where(:city => 'Chicago').consistent.all # Find all addresses where the city is Chicago, with a consistent read.
|
315
|
+
```
|
316
|
+
|
317
|
+
### Range Finding
|
318
|
+
|
319
|
+
If you have a range index, Dynamoid provides a number of additional other convenience methods to make your life a little easier:
|
320
|
+
|
321
|
+
```ruby
|
322
|
+
User.where("created_at.gt" => DateTime.now - 1.day).all
|
323
|
+
User.where("created_at.lt" => DateTime.now - 1.day).all
|
324
|
+
```
|
325
|
+
|
326
|
+
It also supports .gte and .lte. Turning those into symbols and allowing a Rails SQL-style string syntax is in the works. You can only have one range argument per query, because of DynamoDB's inherent limitations, so use it sensibly!
|
327
|
+
|
328
|
+
## Concurrency
|
329
|
+
|
330
|
+
Dynamoid supports basic, ActiveRecord-like optimistic locking on save operations. Simply add a `lock_version` column to your table like so:
|
331
|
+
|
332
|
+
```ruby
|
333
|
+
class MyTable
|
334
|
+
...
|
335
|
+
|
336
|
+
field :lock_version, :integer
|
337
|
+
|
338
|
+
...
|
339
|
+
end
|
340
|
+
```
|
341
|
+
|
342
|
+
In this example, all saves to `MyTable` will raise an `Dynamoid::Errors::StaleObjectError` if a concurrent process loaded, edited, and saved the same row. Your code should trap this exception, reload the row (so that it will pick up the newest values), and try the save again.
|
343
|
+
|
344
|
+
Calls to `update` and `update!` also increment the `lock_version`, however they do not check the existing value. This guarantees that a update operation will raise an exception in a concurrent save operation, however a save operation will never cause an update to fail. Thus, `update` is useful & safe only for doing atomic operations (e.g. increment a value, add/remove from a set, etc), but should not be used in a read-modify-write pattern.
|
345
|
+
|
346
|
+
## Test Environment
|
347
|
+
|
348
|
+
In test environment you will most likely want to clean the database between test runs to keep tests completely isolated. This can be achieved like so
|
349
|
+
|
350
|
+
```ruby
|
351
|
+
module DynamoidReset
|
352
|
+
def self.all
|
353
|
+
Dynamoid.adapter.list_tables.each do |table|
|
354
|
+
# Only delete tables in our namespace
|
355
|
+
if table =~ /^#{Dynamoid::Config.namespace}/
|
356
|
+
Dynamoid.adapter.delete_table(table)
|
357
|
+
end
|
358
|
+
end
|
359
|
+
Dynamoid.adapter.tables.clear
|
360
|
+
# Recreate all tables to avoid unexpected errors
|
361
|
+
Dynamoid.included_models.each(&:create_table)
|
362
|
+
end
|
363
|
+
end
|
364
|
+
|
365
|
+
# Reduce noise in test output
|
366
|
+
Dynamoid.logger.level = Logger::FATAL
|
367
|
+
```
|
368
|
+
|
369
|
+
If you're using RSpec you can invoke the above like so:
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
RSpec.configure do |config|
|
373
|
+
config.before(:each) do
|
374
|
+
DynamoidReset.all
|
375
|
+
end
|
376
|
+
end
|
377
|
+
```
|
378
|
+
|
379
|
+
In Rails, you may also want to ensure you do not delete non-test data accidentally by adding the following to your test environment setup:
|
380
|
+
|
381
|
+
```ruby
|
382
|
+
raise "Tests should be run in 'test' environment only" if Rails.env != 'test'
|
383
|
+
Dynamoid.configure do |config|
|
384
|
+
config.namespace = "#{Rails.application.railtie_name}_#{Rails.env}"
|
385
|
+
end
|
386
|
+
```
|
387
|
+
|
388
|
+
## Credits
|
389
|
+
|
390
|
+
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.
|
391
|
+
|
392
|
+
Also, without contributors the project wouldn't be nearly as awesome. So many thanks to:
|
393
|
+
|
394
|
+
* [Logan Bowers](https://github.com/loganb)
|
395
|
+
* [Lane LaRue](https://github.com/luxx)
|
396
|
+
* [Craig Heneveld](https://github.com/cheneveld)
|
397
|
+
* [Anantha Kumaran](https://github.com/ananthakumaran)
|
398
|
+
* [Jason Dew](https://github.com/jasondew)
|
399
|
+
* [Luis Arias](https://github.com/luisantonioa)
|
400
|
+
* [Stefan Neculai](https://github.com/stefanneculai)
|
401
|
+
* [Philip White](https://github.com/philipmw) *
|
402
|
+
* [Peeyush Kumar](https://github.com/peeyush1234)
|
403
|
+
* [Sumanth Ravipati](https://github.com/sumocoder)
|
404
|
+
* [Pascal Corpet](https://github.com/pcorpet)
|
405
|
+
* [Brian Glusman](https://github.com/bglusman) *
|
406
|
+
* [Peter Boling](https://github.com/pboling) *
|
407
|
+
|
408
|
+
\* Current Maintianers
|
409
|
+
|
410
|
+
## Running the tests
|
411
|
+
|
412
|
+
Running the tests is fairly simple. You should have an instance of DynamoDB running locally. Follow this steps to be able to run the tests:
|
413
|
+
|
414
|
+
* First download and unpack the latest version of DynamoDB.
|
415
|
+
|
416
|
+
```shell
|
417
|
+
bin/setup
|
418
|
+
```
|
419
|
+
|
420
|
+
* Start the local instance of DynamoDB to listen in ***8000*** port
|
421
|
+
|
422
|
+
```shell
|
423
|
+
bin/start_dynamodblocal
|
424
|
+
```
|
425
|
+
|
426
|
+
* and lastly, use `rake` to run the tests.
|
427
|
+
|
428
|
+
```shell
|
429
|
+
rake
|
430
|
+
```
|
431
|
+
|
432
|
+
[![Build Status](https://travis-ci.org/Dynamoid/Dynamoid.svg)](https://travis-ci.org/Dynamoid/Dynamoid)
|
433
|
+
[![Coverage Status](https://coveralls.io/repos/Dynamoid/Dynamoid/badge.svg?branch=master&service=github)](https://coveralls.io/github/Dynamoid/Dynamoid?branch=master)
|
434
|
+
|
435
|
+
## Copyright
|
436
|
+
|
437
|
+
Copyright (c) 2012 Josh Symonds.
|
438
|
+
|
439
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
440
|
+
|
441
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
442
|
+
|
443
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|