dynamodb-api 0.8.1 → 0.9.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/bin/console +1 -1
- data/docker-compose.yml +1 -1
- data/lib/dynamodb/api/base.rb +24 -9
- data/lib/dynamodb/api/config.rb +2 -0
- data/lib/dynamodb/api/map/operator.rb +15 -12
- data/lib/dynamodb/api/query.rb +30 -17
- data/lib/dynamodb/api/relation/query_methods.rb +2 -2
- data/lib/dynamodb/api/relation/where_clause.rb +74 -14
- data/lib/dynamodb/api/scan.rb +16 -16
- data/lib/dynamodb/api/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 884abb76fafa637862d01d8e3f75b0a49c34653d5aeb24a593f8865aae6d8011
|
4
|
+
data.tar.gz: fba9e304ecffb92392d602982d1f2e4f82c36b6b0dce4c702a378c4280d5c0a4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c12330b1af83f9a73ba0965cc5c8189418825e26f98d3f859e5171666960e1f5d17c616534fb051d17e06e293234e4e1161182c5f3caa769e66e0d1842e7fbf
|
7
|
+
data.tar.gz: 1a2b02ae868c6392dd40863de0c864f69bd2501186b7aef53f0cd407be968f5f5490f2ac8b76919e2acb1bdded7f56b9ff68b664a607fbc0d85126cbb8871d55
|
data/bin/console
CHANGED
data/docker-compose.yml
CHANGED
data/lib/dynamodb/api/base.rb
CHANGED
@@ -12,9 +12,13 @@ module Dynamodb
|
|
12
12
|
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
|
13
13
|
end
|
14
14
|
|
15
|
+
def next
|
16
|
+
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
|
17
|
+
end
|
18
|
+
|
15
19
|
private
|
16
20
|
|
17
|
-
def
|
21
|
+
def build_input
|
18
22
|
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
|
19
23
|
end
|
20
24
|
|
@@ -30,20 +34,31 @@ module Dynamodb
|
|
30
34
|
clause&.name ? clause.name : SelectClause.new.name
|
31
35
|
end
|
32
36
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
# Build filter clause.
|
38
|
+
# `filter_expression`: e.g. "Artist = :a"
|
39
|
+
# `expression_attribute_values`: e.g. { ":a" => "No One You Know" }
|
40
|
+
#
|
41
|
+
# @param input [Aws::DynamoDB::Types::ScanInput]
|
42
|
+
# @return [Aws::DynamoDB::Types::ScanInput]
|
43
|
+
def build_filter_clause(input)
|
44
|
+
return input if filter_clause&.expression.blank?
|
45
|
+
input[:filter_expression] = filter_clause.expression
|
46
|
+
if filter_clause.values.present?
|
47
|
+
if input[:expression_attribute_values].nil?
|
48
|
+
input[:expression_attribute_values] = {}
|
49
|
+
end
|
50
|
+
input[:expression_attribute_values].
|
51
|
+
merge!(filter_clause.values)
|
52
|
+
end
|
53
|
+
input
|
39
54
|
end
|
40
55
|
|
41
|
-
def build_expression_attribute_names(
|
56
|
+
def build_expression_attribute_names(input)
|
42
57
|
if filter_clause&.reserved_words.present?
|
43
58
|
expression_attribute.add(filter_clause.reserved_words)
|
44
59
|
end
|
45
60
|
if expression_attribute.names.present?
|
46
|
-
|
61
|
+
input[:expression_attribute_names] = expression_attribute.names
|
47
62
|
end
|
48
63
|
end
|
49
64
|
end
|
data/lib/dynamodb/api/config.rb
CHANGED
@@ -16,6 +16,8 @@ module Dynamodb
|
|
16
16
|
option :table_name_prefix, default: ''
|
17
17
|
option :index_name_prefix, default: ''
|
18
18
|
|
19
|
+
# @param value [String] the table name
|
20
|
+
# @return [String] the table name
|
19
21
|
def build_table_name(value)
|
20
22
|
return value unless table_name_prefix?
|
21
23
|
"#{table_name_prefix}#{value}"
|
@@ -4,21 +4,24 @@ module Dynamodb
|
|
4
4
|
module Api
|
5
5
|
module Map
|
6
6
|
class Operator # :nodoc:
|
7
|
+
# Replace the Dynamodb Operators
|
8
|
+
# @param k [String] The Dynamodb Operator
|
9
|
+
# @return [String]
|
7
10
|
def self.key(k)
|
8
11
|
k = k.gsub(' ', '')
|
9
12
|
case k
|
10
|
-
when '
|
11
|
-
'
|
12
|
-
when '
|
13
|
-
'
|
14
|
-
when '
|
15
|
-
'
|
16
|
-
when '
|
17
|
-
'
|
18
|
-
when '
|
19
|
-
'
|
20
|
-
when '
|
21
|
-
'
|
13
|
+
when 'EQ'
|
14
|
+
'='
|
15
|
+
when 'NE'
|
16
|
+
'!='
|
17
|
+
when 'LE'
|
18
|
+
'<='
|
19
|
+
when 'LT'
|
20
|
+
'<'
|
21
|
+
when 'GE'
|
22
|
+
'>='
|
23
|
+
when 'GT'
|
24
|
+
'>'
|
22
25
|
else
|
23
26
|
k
|
24
27
|
end
|
data/lib/dynamodb/api/query.rb
CHANGED
@@ -6,16 +6,16 @@ module Dynamodb
|
|
6
6
|
module Api
|
7
7
|
class Query < Base # :nodoc:
|
8
8
|
def all
|
9
|
-
result = Adapter.client.query(
|
9
|
+
result = Adapter.client.query(build_input)
|
10
10
|
@last_evaluated_key = result.last_evaluated_key
|
11
11
|
result
|
12
12
|
end
|
13
13
|
|
14
14
|
def next
|
15
15
|
return nil if @last_evaluated_key.blank?
|
16
|
-
|
17
|
-
|
18
|
-
)
|
16
|
+
input = build_input
|
17
|
+
input[:exclusive_start_key] = @last_evaluated_key
|
18
|
+
result = Adapter.client.query(input)
|
19
19
|
@last_evaluated_key = result.last_evaluated_key
|
20
20
|
return nil if result.count.zero?
|
21
21
|
result
|
@@ -23,21 +23,34 @@ module Dynamodb
|
|
23
23
|
|
24
24
|
private
|
25
25
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
26
|
+
# Build options.
|
27
|
+
#
|
28
|
+
# @return [Aws::DynamoDB::Types::ScanInput]
|
29
|
+
def build_input
|
30
|
+
input = Aws::DynamoDB::Types::QueryInput.new
|
31
|
+
input[:expression_attribute_values] = {}
|
32
|
+
input = base_input(input)
|
33
|
+
input = build_filter_clause(input)
|
34
|
+
input[:scan_index_forward] = order_direct(order_clause)
|
35
|
+
input[:select] = select_name(select_clause)
|
36
|
+
build_expression_attribute_names(input)
|
37
|
+
input[:limit] = limit_clause.number if limit_clause&.number
|
38
|
+
input
|
33
39
|
end
|
34
40
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
+
# Build required options.
|
42
|
+
# `key_condition_expression`: e.g. "Artist = :a"
|
43
|
+
# `expression_attribute_values`: e.g. { ":a" => "No One You Know" }
|
44
|
+
#
|
45
|
+
# @param input [Aws::DynamoDB::Types::ScanInput]
|
46
|
+
# @return [Aws::DynamoDB::Types::ScanInput]
|
47
|
+
def base_input(input)
|
48
|
+
input[:table_name] = from_clause.name
|
49
|
+
input[:index_name] = index_clause.name
|
50
|
+
input[:key_condition_expression] = where_clause.key_condition
|
51
|
+
input[:expression_attribute_values].
|
52
|
+
merge!(where_clause.attribute_values)
|
53
|
+
input
|
41
54
|
end
|
42
55
|
end
|
43
56
|
end
|
@@ -4,24 +4,89 @@ module Dynamodb
|
|
4
4
|
module Api
|
5
5
|
module Relation
|
6
6
|
class WhereClause # :nodoc:
|
7
|
-
attr_reader :
|
7
|
+
attr_reader :key_condition
|
8
|
+
attr_reader :attribute_values
|
8
9
|
|
9
10
|
KEY = 0
|
10
11
|
VALUE = 2
|
11
12
|
OPERATOR = 1
|
13
|
+
FROM = 0
|
14
|
+
TO = 1
|
12
15
|
|
13
|
-
|
14
|
-
|
16
|
+
# @param conditions [Array]
|
17
|
+
def initialize(conditions)
|
18
|
+
built_conditions = build(conditions)
|
19
|
+
@key_condition = built_conditions[:key_conditions]
|
20
|
+
@attribute_values = built_conditions[:attribute_values]
|
15
21
|
end
|
16
22
|
|
17
23
|
private
|
18
24
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
# Build where clause.
|
26
|
+
#
|
27
|
+
# @param conditions [Array]
|
28
|
+
def build(conditions)
|
29
|
+
init = { key_conditions: [], attribute_values: {} }
|
30
|
+
conditions = format_conditions(conditions)
|
31
|
+
built = conditions.each_with_object(init) do |c, h|
|
32
|
+
h[:key_conditions] <<
|
33
|
+
format_key_condition(c[KEY], c[OPERATOR])
|
34
|
+
h[:attribute_values].
|
35
|
+
merge!(format_values(c[KEY], c[OPERATOR], c[VALUE]))
|
36
|
+
end
|
37
|
+
built[:key_conditions] =
|
38
|
+
built[:key_conditions].join(' AND ')
|
39
|
+
built
|
40
|
+
end
|
41
|
+
|
42
|
+
# Format to the `key_condition_expression`.
|
43
|
+
# e.g.
|
44
|
+
# `format_key_condition("Artist", "=")` =>
|
45
|
+
# `"Artist = :Artist"`
|
46
|
+
#
|
47
|
+
# `format_key_condition("Artist", "begins_with")` =>
|
48
|
+
# `"begins_with(Artist, :Artist)"`
|
49
|
+
#
|
50
|
+
# `format_key_condition("Year", "between")` =>
|
51
|
+
# `"Year between :from_Year and :to_Year"`
|
52
|
+
#
|
53
|
+
# @param key [String] a attribute name
|
54
|
+
# @param operator [String] a operator
|
55
|
+
# @return [String] Formatted expression.
|
56
|
+
def format_key_condition(key, operator)
|
57
|
+
case operator.downcase
|
58
|
+
when 'begins_with'
|
59
|
+
"#{operator.downcase}(#{key}, :#{key})"
|
60
|
+
when 'between'
|
61
|
+
"#{key} #{operator.downcase}" \
|
62
|
+
" :from_#{key} AND :to_#{key}"
|
63
|
+
else
|
64
|
+
"#{key} #{Map::Operator.key(operator)} :#{key}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Format to the `expression_attribute_values`
|
69
|
+
# e.g.
|
70
|
+
# `format_values("Artist", "=", "blue")` =>
|
71
|
+
# `{ ":Artist" => "blue" }`
|
72
|
+
#
|
73
|
+
# `format_values("Year", "between", [1999, 2020])` =>
|
74
|
+
# `{ ":from_Year" => 1999, ":to_Year" => 2020 }`
|
75
|
+
#
|
76
|
+
# @param key [String] a attribute name
|
77
|
+
# @param operator [String] a operator
|
78
|
+
# @param values [Object] values
|
79
|
+
# @return [Hash<Object>] Formatted expression.
|
80
|
+
def format_values(key, operator, values)
|
81
|
+
case operator.downcase
|
82
|
+
when 'between'
|
83
|
+
{
|
84
|
+
":from_#{key}" => values[FROM],
|
85
|
+
":to_#{key}" => values[TO],
|
86
|
+
}
|
87
|
+
else
|
88
|
+
{
|
89
|
+
":#{key}" => values.is_a?(Array) ? values[0] : values,
|
25
90
|
}
|
26
91
|
end
|
27
92
|
end
|
@@ -30,11 +95,6 @@ module Dynamodb
|
|
30
95
|
return [conditions] unless conditions[0].is_a?(Array)
|
31
96
|
conditions
|
32
97
|
end
|
33
|
-
|
34
|
-
def format_value(value)
|
35
|
-
return [value] unless value.is_a?(Array)
|
36
|
-
value
|
37
|
-
end
|
38
98
|
end
|
39
99
|
end
|
40
100
|
end
|
data/lib/dynamodb/api/scan.rb
CHANGED
@@ -6,36 +6,36 @@ module Dynamodb
|
|
6
6
|
module Api
|
7
7
|
class Scan < Base # :nodoc:
|
8
8
|
def all
|
9
|
-
result = Adapter.client.scan(
|
9
|
+
result = Adapter.client.scan(build_input)
|
10
10
|
@last_evaluated_key = result.last_evaluated_key
|
11
11
|
result
|
12
12
|
end
|
13
13
|
|
14
14
|
def next
|
15
15
|
return nil if @last_evaluated_key.blank?
|
16
|
-
|
17
|
-
|
18
|
-
)
|
16
|
+
input = build_input
|
17
|
+
input[:exclusive_start_key] = @last_evaluated_key
|
18
|
+
result = Adapter.client.scan(input)
|
19
19
|
@last_evaluated_key = result.last_evaluated_key
|
20
20
|
result
|
21
21
|
end
|
22
22
|
|
23
23
|
private
|
24
24
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
def build_input
|
26
|
+
input = Aws::DynamoDB::Types::ScanInput.new
|
27
|
+
input = base_input(input)
|
28
|
+
input = build_filter_clause(input)
|
29
|
+
input[:index_name] = index_clause.name if index_clause&.name
|
30
|
+
input[:select] = select_name(select_clause)
|
31
|
+
build_expression_attribute_names(input)
|
32
|
+
input[:limit] = limit_clause.number if limit_clause&.number
|
33
|
+
input
|
33
34
|
end
|
34
35
|
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
}
|
36
|
+
def base_input(input)
|
37
|
+
input[:table_name] = from_clause.name
|
38
|
+
input
|
39
39
|
end
|
40
40
|
end
|
41
41
|
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.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- WalkerSumida
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk
|
@@ -193,8 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
193
193
|
- !ruby/object:Gem::Version
|
194
194
|
version: '0'
|
195
195
|
requirements: []
|
196
|
-
|
197
|
-
rubygems_version: 2.7.6
|
196
|
+
rubygems_version: 3.0.3
|
198
197
|
signing_key:
|
199
198
|
specification_version: 4
|
200
199
|
summary: aws dynamodb api
|