dynamodb-api 0.4.2 → 0.5.0
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.
- checksums.yaml +4 -4
- data/.gitignore +12 -0
- data/.travis.yml +4 -0
- data/Appraisals +7 -0
- data/CHANGELOG.md +12 -0
- data/README.md +47 -24
- data/dynamodb-api.gemspec +2 -1
- data/gemfiles/aws_sdk_2.gemfile +8 -0
- data/gemfiles/aws_sdk_3.gemfile +8 -0
- data/lib/dynamodb/api.rb +1 -0
- data/lib/dynamodb/api/query.rb +20 -9
- data/lib/dynamodb/api/relation.rb +9 -1
- data/lib/dynamodb/api/relation/expression_attribute_names.rb +36 -3
- data/lib/dynamodb/api/relation/filter_clause.rb +8 -0
- data/lib/dynamodb/api/relation/query_methods.rb +0 -5
- data/lib/dynamodb/api/version.rb +1 -1
- metadata +22 -5
- data/Gemfile.lock +0 -92
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1176f23f9b8202950b14fb9f093a89a0e8a71408b07d1eee64e1cfa2f4771f68
|
4
|
+
data.tar.gz: '018fe530f3c0df9d3f025154569dd1958b476e1d84051b01091c4dd7f5337c9b'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4da7e234eeff907f7382187281794ae788c57312b3e5d35c2ee9f7b4c2c686153d7111fa8b3633012c5dc6248c7cd41abb5e93ebf094e9d23dfd71a7cd5f270
|
7
|
+
data.tar.gz: 2a3e960e5d73b46d989ae006f18ecf8c4d5837b1e52d478f0b64c55ea608a04a5e7e3ccb9857e3839525629c9a6b906dc070f9ddcbb307638c5a64a421cd8aa3
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Appraisals
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [0.5.0] - 2018-09-30
|
8
|
+
### Added
|
9
|
+
- [#21](https://github.com/walkersumida/dynamodb-api/pull/21) Automatically add Expression Attribute Names
|
10
|
+
|
11
|
+
### Removed
|
12
|
+
- [#21](https://github.com/walkersumida/dynamodb-api/pull/21) `ex_attr` method
|
data/README.md
CHANGED
@@ -50,12 +50,12 @@ e.g.
|
|
50
50
|
|
51
51
|
cars table.
|
52
52
|
|
53
|
-
| maker_id(Partition key) | model | release_date(Sort key) |
|
54
|
-
|
55
|
-
|1 |Accord |0.19760508e8 |
|
56
|
-
|2 |CROWN |0.19550101e8 |
|
57
|
-
|3 |Model S |0.20120601e8 |
|
58
|
-
|1 |S2000 |0.19980101e8 |
|
53
|
+
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
54
|
+
|:---|:---|:---|:---|
|
55
|
+
|1 |Accord |0.19760508e8 |0 |
|
56
|
+
|2 |CROWN |0.19550101e8 |0 |
|
57
|
+
|3 |Model S |0.20120601e8 |0 |
|
58
|
+
|1 |S2000 |0.19980101e8 |1 |
|
59
59
|
|
60
60
|
### Query
|
61
61
|
https://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#query-instance_method
|
@@ -69,10 +69,10 @@ query.from('cars').index('index_maker_id_release_date').
|
|
69
69
|
items = query.all.items
|
70
70
|
```
|
71
71
|
|
72
|
-
| maker_id | model | release_date |
|
73
|
-
|
74
|
-
|1 |S2000 |0.19980101e8 |
|
75
|
-
|1 |Accord |0.19760508e8 |
|
72
|
+
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
73
|
+
|:---|:---|:---|:---|
|
74
|
+
|1 |S2000 |0.19980101e8 |1 |
|
75
|
+
|1 |Accord |0.19760508e8 |0 |
|
76
76
|
|
77
77
|
#### Partition key and Sort(Range) key
|
78
78
|
|
@@ -83,9 +83,9 @@ query.from('cars').index('index_maker_id_release_date').
|
|
83
83
|
items = query.all.items
|
84
84
|
```
|
85
85
|
|
86
|
-
| maker_id | model | release_date |
|
87
|
-
|
88
|
-
|1 |S2000 |0.19980101e8 |
|
86
|
+
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
87
|
+
|:---|:---|:---|:---|
|
88
|
+
|1 |S2000 |0.19980101e8 |1 |
|
89
89
|
|
90
90
|
#### Sorting
|
91
91
|
|
@@ -97,10 +97,10 @@ query.from('cars').index('index_maker_id_release_date').
|
|
97
97
|
items = query.all.items
|
98
98
|
```
|
99
99
|
|
100
|
-
| maker_id | model | release_date |
|
101
|
-
|
102
|
-
|1 |Accord |0.19760508e8 |
|
103
|
-
|1 |S2000 |0.19980101e8 |
|
100
|
+
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
101
|
+
|:---|:---|:---|:---|
|
102
|
+
|1 |Accord |0.19760508e8 |0 |
|
103
|
+
|1 |S2000 |0.19980101e8 |1 |
|
104
104
|
|
105
105
|
#### Filter
|
106
106
|
|
@@ -112,9 +112,9 @@ query.from('cars').index('index_maker_id_release_date').
|
|
112
112
|
items = query.all.items
|
113
113
|
```
|
114
114
|
|
115
|
-
| maker_id | model | release_date |
|
116
|
-
|
117
|
-
|1 |S2000 |0.19980101e8 |
|
115
|
+
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
116
|
+
|:---|:---|:---|:---|
|
117
|
+
|1 |S2000 |0.19980101e8 |1 |
|
118
118
|
|
119
119
|
#### Limit
|
120
120
|
|
@@ -127,15 +127,38 @@ query.from('cars').index('index_maker_id_release_date').
|
|
127
127
|
items = query.all.items
|
128
128
|
```
|
129
129
|
|
130
|
-
| maker_id | model | release_date |
|
131
|
-
|
132
|
-
|1 |Accord |0.19760508e8 |
|
130
|
+
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
131
|
+
|:---|:---|:---|:---|
|
132
|
+
|1 |Accord |0.19760508e8 |0 |
|
133
|
+
|
134
|
+
#### Expression Attribute Names
|
135
|
+
|
136
|
+
Words reserved in DynamoDB can not be used without special processing.
|
137
|
+
In `dynamodb-api` , you can omit the special processing by putting `#` at the beginning of the word.
|
138
|
+
Refer to the list of reserved words from [here](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html).
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
query = Dynamodb::Api::Query.new
|
142
|
+
query.from('cars').index('index_maker_id_release_date').
|
143
|
+
where(['maker_id', '=', 1]).
|
144
|
+
filter('#status = :status', ':status': 1)
|
145
|
+
items = query.all.items
|
146
|
+
```
|
147
|
+
|
148
|
+
| maker_id | model | release_date | status |
|
149
|
+
|:---|:---|:---|:---|
|
150
|
+
|1 |S2000 |0.19980101e8 |1 |
|
151
|
+
|
152
|
+
If you don't add `#` to a reserved word, the following error will occur:
|
153
|
+
|
154
|
+
Aws::DynamoDB::Errors::ValidationException:
|
155
|
+
Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: [reserved word]
|
133
156
|
|
134
157
|
## Development
|
135
158
|
|
136
159
|
- Run `docker-compose up` to run the dynamodb_local.
|
137
160
|
- After checking out the repo, run `bin/setup` to install dependencies.
|
138
|
-
- Run `rake spec` to run the tests.
|
161
|
+
- Run `bundle exec rake spec` to run the tests. Or run `bundle exec appraisal aws-sdk-* rake spec` to run the tests. Replase `*` with aws sdk major version.
|
139
162
|
- You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
140
163
|
|
141
164
|
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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
data/dynamodb-api.gemspec
CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
23
|
spec.require_paths = ['lib']
|
24
24
|
|
25
|
-
spec.add_runtime_dependency 'aws-sdk', '
|
25
|
+
spec.add_runtime_dependency 'aws-sdk', '>= 2'
|
26
26
|
spec.add_runtime_dependency('activesupport', '>= 4')
|
27
27
|
|
28
28
|
spec.add_development_dependency 'bundler', '~> 1.16'
|
@@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
31
31
|
spec.add_development_dependency 'pry'
|
32
32
|
spec.add_development_dependency 'rubocop-airbnb'
|
33
|
+
spec.add_development_dependency 'appraisal'
|
33
34
|
end
|
data/lib/dynamodb/api.rb
CHANGED
data/lib/dynamodb/api/query.rb
CHANGED
@@ -14,20 +14,22 @@ module Dynamodb
|
|
14
14
|
private
|
15
15
|
|
16
16
|
def build_query
|
17
|
-
build_params =
|
18
|
-
table_name: from_clause.name,
|
19
|
-
index_name: index_clause.name,
|
20
|
-
key_conditions: where_clause.key_conditions,
|
21
|
-
}.merge(build_filter_clause)
|
17
|
+
build_params = base_params.merge(build_filter_clause)
|
22
18
|
build_params[:scan_index_forward] = order_direct(order_clause)
|
23
19
|
build_params[:select] = select_name(select_clause)
|
24
|
-
|
25
|
-
build_params[:expression_attribute_names] = expression_attribute.names
|
26
|
-
end
|
20
|
+
build_expression_attribute_names(build_params)
|
27
21
|
build_params[:limit] = limit_clause.number if limit_clause&.number
|
28
22
|
build_params
|
29
23
|
end
|
30
24
|
|
25
|
+
def base_params
|
26
|
+
{
|
27
|
+
table_name: from_clause.name,
|
28
|
+
index_name: index_clause.name,
|
29
|
+
key_conditions: where_clause.key_conditions,
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
31
33
|
def order_direct(clause)
|
32
34
|
clause&.direct ? clause.direct : OrderClause.new.direct
|
33
35
|
end
|
@@ -37,12 +39,21 @@ module Dynamodb
|
|
37
39
|
end
|
38
40
|
|
39
41
|
def build_filter_clause
|
40
|
-
return {}
|
42
|
+
return {} if filter_clause&.expression.blank?
|
41
43
|
{
|
42
44
|
filter_expression: filter_clause.expression,
|
43
45
|
expression_attribute_values: filter_clause.values,
|
44
46
|
}
|
45
47
|
end
|
48
|
+
|
49
|
+
def build_expression_attribute_names(params)
|
50
|
+
if filter_clause&.reserved_words.present?
|
51
|
+
expression_attribute.add(filter_clause.reserved_words)
|
52
|
+
end
|
53
|
+
if expression_attribute.names.present?
|
54
|
+
params[:expression_attribute_names] = expression_attribute.names
|
55
|
+
end
|
56
|
+
end
|
46
57
|
end
|
47
58
|
end
|
48
59
|
end
|
@@ -12,8 +12,16 @@ module Dynamodb
|
|
12
12
|
attr_accessor :order_clause
|
13
13
|
attr_accessor :where_clause
|
14
14
|
attr_accessor :filter_clause
|
15
|
-
attr_accessor :
|
15
|
+
attr_accessor :attr_expression_attribute
|
16
16
|
attr_accessor :limit_clause
|
17
|
+
|
18
|
+
def expression_attribute
|
19
|
+
if attr_expression_attribute.nil?
|
20
|
+
self.attr_expression_attribute = Relation::ExpressionAttributeNames.new
|
21
|
+
else
|
22
|
+
attr_expression_attribute
|
23
|
+
end
|
24
|
+
end
|
17
25
|
end
|
18
26
|
end
|
19
27
|
end
|
@@ -4,10 +4,43 @@ module Dynamodb
|
|
4
4
|
module Api
|
5
5
|
module Relation
|
6
6
|
class ExpressionAttributeNames
|
7
|
-
|
7
|
+
attr_accessor :names
|
8
8
|
|
9
|
-
def initialize(names)
|
10
|
-
|
9
|
+
def initialize(names = {})
|
10
|
+
self.names = {}
|
11
|
+
|
12
|
+
case names.class.to_s
|
13
|
+
when 'String'
|
14
|
+
add(names)
|
15
|
+
when 'Array'
|
16
|
+
names.each do |name|
|
17
|
+
add(name)
|
18
|
+
end
|
19
|
+
when 'Hash'
|
20
|
+
self.names = names
|
21
|
+
else
|
22
|
+
raise "#{names.class} is not support"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def add(names)
|
27
|
+
_names = formatting(names)
|
28
|
+
self.names.merge!(_names)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def formatting(names)
|
34
|
+
_names = names.is_a?(Array) ? names : [names]
|
35
|
+
_names.each_with_object({}) do |name, hash|
|
36
|
+
next hash[name.to_sym] = remove_hash_tag(name) if name.is_a?(String)
|
37
|
+
next hash.merge!(name) if name.is_a?(Hash)
|
38
|
+
raise "#{name.class} is not support"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def remove_hash_tag(name)
|
43
|
+
name.gsub(/\#/, '')
|
11
44
|
end
|
12
45
|
end
|
13
46
|
end
|
@@ -6,10 +6,18 @@ module Dynamodb
|
|
6
6
|
class FilterClause # :nodoc:
|
7
7
|
attr_reader :expression
|
8
8
|
attr_reader :values
|
9
|
+
attr_reader :reserved_words
|
9
10
|
|
10
11
|
def initialize(expression, values)
|
11
12
|
@expression = expression
|
12
13
|
@values = values
|
14
|
+
@reserved_words = extract_reserved_words(@expression)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def extract_reserved_words(expression)
|
20
|
+
expression.scan(/\#\w+/)
|
13
21
|
end
|
14
22
|
end
|
15
23
|
end
|
data/lib/dynamodb/api/version.rb
CHANGED
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamodb-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- WalkerSumida
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
@@ -108,6 +108,20 @@ dependencies:
|
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: appraisal
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
111
125
|
description: aws dynamodb api
|
112
126
|
email:
|
113
127
|
- walkersumida@gmail.com
|
@@ -121,9 +135,10 @@ files:
|
|
121
135
|
- ".rubocop.yml"
|
122
136
|
- ".rubocop_todo.yml"
|
123
137
|
- ".travis.yml"
|
138
|
+
- Appraisals
|
139
|
+
- CHANGELOG.md
|
124
140
|
- CODE_OF_CONDUCT.md
|
125
141
|
- Gemfile
|
126
|
-
- Gemfile.lock
|
127
142
|
- LICENSE.txt
|
128
143
|
- README.md
|
129
144
|
- Rakefile
|
@@ -131,6 +146,8 @@ files:
|
|
131
146
|
- bin/setup
|
132
147
|
- docker-compose.yml
|
133
148
|
- dynamodb-api.gemspec
|
149
|
+
- gemfiles/aws_sdk_2.gemfile
|
150
|
+
- gemfiles/aws_sdk_3.gemfile
|
134
151
|
- lib/dynamodb/api.rb
|
135
152
|
- lib/dynamodb/api/adapter.rb
|
136
153
|
- lib/dynamodb/api/config.rb
|
data/Gemfile.lock
DELETED
@@ -1,92 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
dynamodb-api (0.4.2)
|
5
|
-
activesupport (>= 4)
|
6
|
-
aws-sdk (~> 2)
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: https://rubygems.org/
|
10
|
-
specs:
|
11
|
-
activesupport (5.2.0)
|
12
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
13
|
-
i18n (>= 0.7, < 2)
|
14
|
-
minitest (~> 5.1)
|
15
|
-
tzinfo (~> 1.1)
|
16
|
-
ast (2.4.0)
|
17
|
-
aws-sdk (2.11.93)
|
18
|
-
aws-sdk-resources (= 2.11.93)
|
19
|
-
aws-sdk-core (2.11.93)
|
20
|
-
aws-sigv4 (~> 1.0)
|
21
|
-
jmespath (~> 1.0)
|
22
|
-
aws-sdk-resources (2.11.93)
|
23
|
-
aws-sdk-core (= 2.11.93)
|
24
|
-
aws-sigv4 (1.0.3)
|
25
|
-
byebug (10.0.2)
|
26
|
-
coderay (1.1.2)
|
27
|
-
concurrent-ruby (1.0.5)
|
28
|
-
diff-lcs (1.3)
|
29
|
-
i18n (1.0.1)
|
30
|
-
concurrent-ruby (~> 1.0)
|
31
|
-
jaro_winkler (1.5.1)
|
32
|
-
jmespath (1.4.0)
|
33
|
-
method_source (0.9.0)
|
34
|
-
minitest (5.11.3)
|
35
|
-
parallel (1.12.1)
|
36
|
-
parser (2.5.1.2)
|
37
|
-
ast (~> 2.4.0)
|
38
|
-
powerpack (0.1.2)
|
39
|
-
pry (0.11.3)
|
40
|
-
coderay (~> 1.1.0)
|
41
|
-
method_source (~> 0.9.0)
|
42
|
-
pry-byebug (3.6.0)
|
43
|
-
byebug (~> 10.0)
|
44
|
-
pry (~> 0.10)
|
45
|
-
rainbow (3.0.0)
|
46
|
-
rake (10.5.0)
|
47
|
-
rspec (3.7.0)
|
48
|
-
rspec-core (~> 3.7.0)
|
49
|
-
rspec-expectations (~> 3.7.0)
|
50
|
-
rspec-mocks (~> 3.7.0)
|
51
|
-
rspec-core (3.7.1)
|
52
|
-
rspec-support (~> 3.7.0)
|
53
|
-
rspec-expectations (3.7.0)
|
54
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
55
|
-
rspec-support (~> 3.7.0)
|
56
|
-
rspec-mocks (3.7.0)
|
57
|
-
diff-lcs (>= 1.2.0, < 2.0)
|
58
|
-
rspec-support (~> 3.7.0)
|
59
|
-
rspec-support (3.7.1)
|
60
|
-
rubocop (0.57.2)
|
61
|
-
jaro_winkler (~> 1.5.1)
|
62
|
-
parallel (~> 1.10)
|
63
|
-
parser (>= 2.5)
|
64
|
-
powerpack (~> 0.1)
|
65
|
-
rainbow (>= 2.2.2, < 4.0)
|
66
|
-
ruby-progressbar (~> 1.7)
|
67
|
-
unicode-display_width (~> 1.0, >= 1.0.1)
|
68
|
-
rubocop-airbnb (1.5.0)
|
69
|
-
rubocop (= 0.57.2)
|
70
|
-
rubocop-rspec (= 1.27.0)
|
71
|
-
rubocop-rspec (1.27.0)
|
72
|
-
rubocop (>= 0.56.0)
|
73
|
-
ruby-progressbar (1.10.0)
|
74
|
-
thread_safe (0.3.6)
|
75
|
-
tzinfo (1.2.5)
|
76
|
-
thread_safe (~> 0.1)
|
77
|
-
unicode-display_width (1.4.0)
|
78
|
-
|
79
|
-
PLATFORMS
|
80
|
-
ruby
|
81
|
-
|
82
|
-
DEPENDENCIES
|
83
|
-
bundler (~> 1.16)
|
84
|
-
dynamodb-api!
|
85
|
-
pry
|
86
|
-
pry-byebug
|
87
|
-
rake (~> 10.0)
|
88
|
-
rspec (~> 3.0)
|
89
|
-
rubocop-airbnb
|
90
|
-
|
91
|
-
BUNDLED WITH
|
92
|
-
1.16.2
|