dynamodb-api 0.5.0 → 0.6.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/CHANGELOG.md +6 -0
- data/README.md +69 -32
- data/bin/console +13 -0
- data/lib/dynamodb/api.rb +14 -0
- data/lib/dynamodb/api/delete/item.rb +14 -0
- data/lib/dynamodb/api/update/item.rb +48 -0
- data/lib/dynamodb/api/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 34f7a13fc8d01e4e3fd6126b892e5bd71484bb19ef77a838a5a02307be3e4530
|
4
|
+
data.tar.gz: f1beedbb696099688379ebc381144b601273d0614ddff89fa5466450f4fe56d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 589539eca0a48f767afeefecb2eed852f8095c8082bca32d973aa3608327eeb95b6a31c58c1d438a2ff96bac044096960d604799123bd63fc2eec5d6609f3b42
|
7
|
+
data.tar.gz: c157d7759fd9d9328f305bebb5ab7800f8d55b083e2686dac1ea07863507dff4e9402ee6bd2936ad17b64d11f94203881f211b4c1dbbdef8327af81094c15fbd
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## [0.6.0] - 2018-11-03
|
8
|
+
### Added
|
9
|
+
- [#25](https://github.com/walkersumida/dynamodb-api/pull/25) `update` method
|
10
|
+
- [#24](https://github.com/walkersumida/dynamodb-api/pull/24) `delete` method
|
11
|
+
- [#23](https://github.com/walkersumida/dynamodb-api/pull/23) `query` method
|
12
|
+
|
7
13
|
## [0.5.0] - 2018-09-30
|
8
14
|
### Added
|
9
15
|
- [#21](https://github.com/walkersumida/dynamodb-api/pull/21) Automatically add Expression Attribute Names
|
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) | 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 |
|
53
|
+
| id | maker_id(Partition key) | model | release_date(Sort key) | status |
|
54
|
+
|:---|:---|:---|:---|:---|
|
55
|
+
|1 |1 |Accord |0.19760508e8 |0 |
|
56
|
+
|2 |2 |CROWN |0.19550101e8 |0 |
|
57
|
+
|3 |3 |Model S |0.20120601e8 |0 |
|
58
|
+
|4 |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
|
@@ -63,63 +63,63 @@ https://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html#query-instan
|
|
63
63
|
#### Partition(Hash) key
|
64
64
|
|
65
65
|
```ruby
|
66
|
-
query = Dynamodb::Api
|
66
|
+
query = Dynamodb::Api.query
|
67
67
|
query.from('cars').index('index_maker_id_release_date').
|
68
68
|
where(['maker_id', '=', 1])
|
69
69
|
items = query.all.items
|
70
70
|
```
|
71
71
|
|
72
|
-
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
73
|
-
|
74
|
-
|1 |S2000 |0.19980101e8 |1 |
|
75
|
-
|1 |Accord |0.19760508e8 |0 |
|
72
|
+
| id | maker_id(Partition key) | model | release_date(Sort key) | status |
|
73
|
+
|:---|:---|:---|:---|:---|
|
74
|
+
|4 |1 |S2000 |0.19980101e8 |1 |
|
75
|
+
|1 |1 |Accord |0.19760508e8 |0 |
|
76
76
|
|
77
77
|
#### Partition key and Sort(Range) key
|
78
78
|
|
79
79
|
```ruby
|
80
|
-
query = Dynamodb::Api
|
80
|
+
query = Dynamodb::Api.query
|
81
81
|
query.from('cars').index('index_maker_id_release_date').
|
82
82
|
where([['maker_id', '=', 1], ['release_date', '>=', 19_980_101]])
|
83
83
|
items = query.all.items
|
84
84
|
```
|
85
85
|
|
86
|
-
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
87
|
-
|
88
|
-
|1 |S2000 |0.19980101e8 |1 |
|
86
|
+
| id | maker_id(Partition key) | model | release_date(Sort key) | status |
|
87
|
+
|:---|:---|:---|:---|:---|
|
88
|
+
|4 |1 |S2000 |0.19980101e8 |1 |
|
89
89
|
|
90
90
|
#### Sorting
|
91
91
|
|
92
92
|
```ruby
|
93
|
-
query = Dynamodb::Api
|
93
|
+
query = Dynamodb::Api.query
|
94
94
|
query.from('cars').index('index_maker_id_release_date').
|
95
95
|
where(['maker_id', '=', 1]).
|
96
96
|
order('asc') # default: 'desc'
|
97
97
|
items = query.all.items
|
98
98
|
```
|
99
99
|
|
100
|
-
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
101
|
-
|
102
|
-
|1 |Accord |0.19760508e8 |0 |
|
103
|
-
|1 |S2000 |0.19980101e8 |1 |
|
100
|
+
| id | maker_id(Partition key) | model | release_date(Sort key) | status |
|
101
|
+
|:---|:---|:---|:---|:---|
|
102
|
+
|1 |1 |Accord |0.19760508e8 |0 |
|
103
|
+
|4 |1 |S2000 |0.19980101e8 |1 |
|
104
104
|
|
105
105
|
#### Filter
|
106
106
|
|
107
107
|
```ruby
|
108
|
-
query = Dynamodb::Api
|
108
|
+
query = Dynamodb::Api.query
|
109
109
|
query.from('cars').index('index_maker_id_release_date').
|
110
110
|
where(['maker_id', '=', 1]).
|
111
111
|
filter('model = :model', ':model': 'S2000')
|
112
112
|
items = query.all.items
|
113
113
|
```
|
114
114
|
|
115
|
-
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
116
|
-
|
117
|
-
|1 |S2000 |0.19980101e8 |1 |
|
115
|
+
| id | maker_id(Partition key) | model | release_date(Sort key) | status |
|
116
|
+
|:---|:---|:---|:---|:---|
|
117
|
+
|4 |1 |S2000 |0.19980101e8 |1 |
|
118
118
|
|
119
119
|
#### Limit
|
120
120
|
|
121
121
|
```ruby
|
122
|
-
query = Dynamodb::Api
|
122
|
+
query = Dynamodb::Api.query
|
123
123
|
query.from('cars').index('index_maker_id_release_date').
|
124
124
|
where(['maker_id', '=', 1]).
|
125
125
|
order('asc'). # default: 'desc'
|
@@ -127,9 +127,9 @@ query.from('cars').index('index_maker_id_release_date').
|
|
127
127
|
items = query.all.items
|
128
128
|
```
|
129
129
|
|
130
|
-
| maker_id(Partition key) | model | release_date(Sort key) | status |
|
131
|
-
|
132
|
-
|1 |Accord |0.19760508e8 |0 |
|
130
|
+
| id | maker_id(Partition key) | model | release_date(Sort key) | status |
|
131
|
+
|:---|:---|:---|:---|:---|
|
132
|
+
|1 |1 |Accord |0.19760508e8 |0 |
|
133
133
|
|
134
134
|
#### Expression Attribute Names
|
135
135
|
|
@@ -138,22 +138,59 @@ In `dynamodb-api` , you can omit the special processing by putting `#` at the be
|
|
138
138
|
Refer to the list of reserved words from [here](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ReservedWords.html).
|
139
139
|
|
140
140
|
```ruby
|
141
|
-
query = Dynamodb::Api
|
141
|
+
query = Dynamodb::Api.query
|
142
142
|
query.from('cars').index('index_maker_id_release_date').
|
143
143
|
where(['maker_id', '=', 1]).
|
144
144
|
filter('#status = :status', ':status': 1)
|
145
145
|
items = query.all.items
|
146
146
|
```
|
147
147
|
|
148
|
-
| maker_id | model | release_date | status |
|
149
|
-
|
150
|
-
|1 |S2000 |0.19980101e8 |1 |
|
148
|
+
| id | maker_id(Partition key) | model | release_date(Sort key) | status |
|
149
|
+
|:---|:---|:---|:---|:---|
|
150
|
+
|4 |1 |S2000 |0.19980101e8 |1 |
|
151
151
|
|
152
152
|
If you don't add `#` to a reserved word, the following error will occur:
|
153
153
|
|
154
154
|
Aws::DynamoDB::Errors::ValidationException:
|
155
155
|
Invalid FilterExpression: Attribute name is a reserved keyword; reserved keyword: [reserved word]
|
156
156
|
|
157
|
+
### Insert
|
158
|
+
|
159
|
+
```ruby
|
160
|
+
item = { id: '5', maker_id: 1, maker: 'Honda', model: 'NSX', release_date: 19900914 }
|
161
|
+
Dynamodb::Api.insert('cars', item)
|
162
|
+
```
|
163
|
+
|
164
|
+
### Update
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
key = { id: '5' }
|
168
|
+
value = { new_col: 'new' }
|
169
|
+
Dynamodb::Api.update('cars', key, value)
|
170
|
+
```
|
171
|
+
|
172
|
+
### Delete
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
key = { id: '5' }
|
176
|
+
Dynamodb::Api.delete('cars', key)
|
177
|
+
```
|
178
|
+
|
179
|
+
### Other API operations
|
180
|
+
|
181
|
+
`client` returns the `<Aws::DynamoDB::Client>` .
|
182
|
+
So, you can use all [API operations](https://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Client.html).
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
client = Dynamodb::Api::Adapter.client # <Aws::DynamoDB::Client>
|
186
|
+
|
187
|
+
# e.g.
|
188
|
+
client.create_backup(
|
189
|
+
table_name: 'TableName', # required
|
190
|
+
backup_name: 'BackupName', # required
|
191
|
+
)
|
192
|
+
```
|
193
|
+
|
157
194
|
## Development
|
158
195
|
|
159
196
|
- Run `docker-compose up` to run the dynamodb_local.
|
data/bin/console
CHANGED
@@ -11,4 +11,17 @@ require "dynamodb/api"
|
|
11
11
|
# Pry.start
|
12
12
|
|
13
13
|
require "irb"
|
14
|
+
|
15
|
+
ENV['ACCESS_KEY'] ||= 'abcd'
|
16
|
+
ENV['SECRET_KEY'] ||= '1234'
|
17
|
+
|
18
|
+
Aws.config.update(
|
19
|
+
region: 'ap-northeast-1',
|
20
|
+
credentials: Aws::Credentials.new(ENV['ACCESS_KEY'], ENV['SECRET_KEY'])
|
21
|
+
)
|
22
|
+
|
23
|
+
Dynamodb::Api.config do |config|
|
24
|
+
config.endpoint = 'http://0.0.0.0:8000'
|
25
|
+
end
|
26
|
+
|
14
27
|
IRB.start(__FILE__)
|
data/lib/dynamodb/api.rb
CHANGED
@@ -18,7 +18,9 @@ require 'dynamodb/api/relation/expression_attribute_names'
|
|
18
18
|
require 'dynamodb/api/relation/limit_clause'
|
19
19
|
require 'dynamodb/api/put/item'
|
20
20
|
require 'dynamodb/api/delete/tables'
|
21
|
+
require 'dynamodb/api/delete/item'
|
21
22
|
require 'dynamodb/api/map/operator'
|
23
|
+
require 'dynamodb/api/update/item'
|
22
24
|
|
23
25
|
module Dynamodb
|
24
26
|
module Api # :nodoc:
|
@@ -36,9 +38,21 @@ module Dynamodb
|
|
36
38
|
Delete::Tables.delete_tables
|
37
39
|
end
|
38
40
|
|
41
|
+
def query
|
42
|
+
Query.new
|
43
|
+
end
|
44
|
+
|
39
45
|
def insert(table_name, value)
|
40
46
|
# TODO: BatchWriteItem
|
41
47
|
Put::Item.put_item(value, table_name)
|
42
48
|
end
|
49
|
+
|
50
|
+
def update(table_name, key, value)
|
51
|
+
Update::Item.new.update_item(key, value, table_name)
|
52
|
+
end
|
53
|
+
|
54
|
+
def delete(table_name, key)
|
55
|
+
Delete::Item.delete_item(key, table_name)
|
56
|
+
end
|
43
57
|
end
|
44
58
|
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dynamodb
|
4
|
+
module Api
|
5
|
+
module Delete
|
6
|
+
class Item # :nodoc:
|
7
|
+
def self.delete_item(key, table_name)
|
8
|
+
client = Adapter.client
|
9
|
+
client.delete_item(key: key, table_name: table_name)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Dynamodb
|
4
|
+
module Api
|
5
|
+
module Update
|
6
|
+
class Item # :nodoc:
|
7
|
+
def update_item(key, cols, table_name)
|
8
|
+
@key = key
|
9
|
+
@cols = cols
|
10
|
+
@table_name = table_name
|
11
|
+
|
12
|
+
client = Adapter.client
|
13
|
+
client.update_item(build_update_clause)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def build_update_expression
|
19
|
+
str = @cols.each_with_object([]) do |(k, _v), ary|
|
20
|
+
ary << "##{k} = :#{k}"
|
21
|
+
end.join(', ')
|
22
|
+
"SET #{str}"
|
23
|
+
end
|
24
|
+
|
25
|
+
def build_expression_attribute_names
|
26
|
+
@cols.each_with_object({}) do |(k, _v), h|
|
27
|
+
h["##{k}"] = k
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def build_expression_attribute_values
|
32
|
+
@cols.each_with_object({}) do |(k, v), h|
|
33
|
+
h[":#{k}"] = v
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def build_update_clause
|
38
|
+
{
|
39
|
+
key: @key, table_name: @table_name,
|
40
|
+
update_expression: build_update_expression,
|
41
|
+
expression_attribute_names: build_expression_attribute_names,
|
42
|
+
expression_attribute_values: build_expression_attribute_values,
|
43
|
+
}
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/dynamodb/api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamodb-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.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-
|
11
|
+
date: 2018-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk
|
@@ -152,6 +152,7 @@ files:
|
|
152
152
|
- lib/dynamodb/api/adapter.rb
|
153
153
|
- lib/dynamodb/api/config.rb
|
154
154
|
- lib/dynamodb/api/config/options.rb
|
155
|
+
- lib/dynamodb/api/delete/item.rb
|
155
156
|
- lib/dynamodb/api/delete/tables.rb
|
156
157
|
- lib/dynamodb/api/map/operator.rb
|
157
158
|
- lib/dynamodb/api/put/item.rb
|
@@ -166,6 +167,7 @@ files:
|
|
166
167
|
- lib/dynamodb/api/relation/query_methods.rb
|
167
168
|
- lib/dynamodb/api/relation/select_clause.rb
|
168
169
|
- lib/dynamodb/api/relation/where_clause.rb
|
170
|
+
- lib/dynamodb/api/update/item.rb
|
169
171
|
- lib/dynamodb/api/version.rb
|
170
172
|
homepage: https://github.com/walkersumida/dynamodb-api
|
171
173
|
licenses:
|