dynamodb_record 0.3.1 → 0.3.3
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/lib/dynamodb_record/associations.rb +33 -33
- data/lib/dynamodb_record/finders.rb +0 -1
- data/lib/dynamodb_record/query.rb +1 -0
- data/lib/dynamodb_record/version.rb +1 -1
- data/lib/dynamodb_record.rb +1 -0
- data/spec/dynamodb_record/config_spec.rb +28 -0
- data/spec/dynamodb_record/document_spec.rb +4 -4
- data/spec/dynamodb_record/finders_spec.rb +7 -7
- data/spec/dynamodb_record/persistence_spec.rb +12 -12
- data/spec/dynamodb_record/query_spec.rb +2 -2
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/_belongs_to/get_object.yml +18 -18
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/_has_many/add_list.yml +108 -57
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/_has_many/create_item.yml +28 -27
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/_has_many/get_collection.yml +19 -18
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/has_and_belongs_to_many.yml +31 -31
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/has_and_belongs_to_many_create.yml +91 -39
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/has_many_through.yml +30 -27
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Document/initializes_from_database.yml +11 -11
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Fields/_find/finds_record.yml +12 -12
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Fields/_find/when_record_doesn_t_exists/returns_empty_object.yml +9 -9
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/_destroy/when_no_range_key/destroys_record.yml +30 -30
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/_destroy/when_there_is_range_key/destroys_record.yml +9 -111
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/does_not_overwrite_existing_record.yml +20 -20
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/saves_record.yml +9 -9
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/updates_record.yml +31 -31
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Query/_all/find_all_records.yml +12 -12
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Query/querying/_where/returns_records_by_limit.yml +12 -12
- data/spec/fixtures/vcr_cassettes/DynamodbRecord_Query/querying/_where/returns_records_where_user_balance_0.yml +12 -12
- data/spec/spec_helper.rb +7 -6
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24d59dadc67c9fe2d0d70b78c5cce8d1af52505f2b49f539ee1cb407be8bff13
|
4
|
+
data.tar.gz: c0de287ed037955f41ccfc29d497ca5299d1efff797a180edb819346b5c9a926
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 775f99c88ba0aeab51b216cdb0950afc55ac61aec51703a6d406902b7572122299ad878b681332d48c0d554237d7871d520acd0b6e92b246a6e0c5c63a3f6899
|
7
|
+
data.tar.gz: 14257dc2a185a5fb18085d69ecf4b08e0166294c50c9c1fb3991828d252760a802a6d8e774608cec37f2af7a32b336c921d5c88b83e1cd69c01625537339675a
|
@@ -17,17 +17,17 @@ module DynamodbRecord
|
|
17
17
|
|
18
18
|
# rubocop:disable Naming/PredicateName
|
19
19
|
def has_many(associations)
|
20
|
-
base_model = to_s.downcase
|
20
|
+
base_model = to_s.downcase.split('::').last
|
21
21
|
model = associations.to_s.chop
|
22
|
-
# field = self.class.to_s.downcase
|
23
|
-
table = Config.namespace ? "#{Config.namespace}-#{associations}" : associations
|
24
|
-
|
25
|
-
options = {table_name: table}
|
26
|
-
options.merge!(key_condition_expression: "##{base_model}_id = :#{base_model}_id")
|
27
|
-
options.merge!(expression_attribute_names: {"##{base_model}_id": "#{base_model}_id"})
|
28
|
-
options.merge!(index_name: "#{base_model}_id_index")
|
29
22
|
|
30
23
|
define_method(associations) do
|
24
|
+
table = Config.namespace ? "#{Config.namespace}-#{associations}" : associations
|
25
|
+
|
26
|
+
options = {table_name: table}
|
27
|
+
options.merge!(key_condition_expression: "##{base_model}_id = :#{base_model}_id")
|
28
|
+
options.merge!(expression_attribute_names: {"##{base_model}_id": "#{base_model}_id"})
|
29
|
+
options.merge!(index_name: "#{base_model}_id_index")
|
30
|
+
|
31
31
|
options.merge!(expression_attribute_values: {":#{base_model}_id" => id})
|
32
32
|
klass = Object.const_get(model.capitalize)
|
33
33
|
query = QueryPager.new(options, klass)
|
@@ -36,7 +36,7 @@ module DynamodbRecord
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def has_many_through(associations, table)
|
39
|
-
base_model = to_s.downcase
|
39
|
+
base_model = to_s.downcase.split('::').last
|
40
40
|
relation_model = associations.to_s.chop
|
41
41
|
list = []
|
42
42
|
list << base_model
|
@@ -44,16 +44,16 @@ module DynamodbRecord
|
|
44
44
|
sorted_list = list.sort
|
45
45
|
options = {table_name: table.to_s}
|
46
46
|
|
47
|
-
field = if sorted_list.first == base_model
|
48
|
-
sorted_list.first
|
49
|
-
else
|
50
|
-
sorted_list.last
|
51
|
-
end
|
52
|
-
options.merge!(index_name: "#{field}_id_index")
|
53
|
-
options.merge!(key_condition_expression: "##{field}_id = :#{field}_id")
|
54
|
-
options.merge!(expression_attribute_names: {"##{field}_id": "#{field}_id"})
|
55
|
-
|
56
47
|
define_method(associations) do
|
48
|
+
field = if sorted_list.first == base_model
|
49
|
+
sorted_list.first
|
50
|
+
else
|
51
|
+
sorted_list.last
|
52
|
+
end
|
53
|
+
options.merge!(index_name: "#{field}_id_index")
|
54
|
+
options.merge!(key_condition_expression: "##{field}_id = :#{field}_id")
|
55
|
+
options.merge!(expression_attribute_names: {"##{field}_id": "#{field}_id"})
|
56
|
+
|
57
57
|
options.merge!(expression_attribute_values: {":#{field}_id" => id})
|
58
58
|
# p options
|
59
59
|
|
@@ -69,28 +69,28 @@ module DynamodbRecord
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def has_and_belongs_to_many(associations)
|
72
|
-
base_model = to_s.downcase
|
72
|
+
base_model = to_s.downcase.split('::').last
|
73
73
|
relation_model = associations.to_s.chop
|
74
74
|
list = []
|
75
75
|
list << base_model
|
76
76
|
list << relation_model
|
77
77
|
sorted_list = list.sort
|
78
|
-
options = {}
|
79
|
-
pluralize_name = sorted_list.map(&:pluralize).join('-')
|
80
|
-
|
81
|
-
table = Config.namespace ? "#{Config.namespace}-#{pluralize_name}" : pluralize_name
|
82
|
-
options[:table_name] = table
|
83
|
-
if sorted_list.first == base_model
|
84
|
-
field = sorted_list.first
|
85
|
-
else
|
86
|
-
field = sorted_list.last
|
87
|
-
options.merge!(index_name: "#{table}-index")
|
88
|
-
end
|
89
|
-
|
90
|
-
options.merge!(key_condition_expression: "##{field}_id = :#{field}_id")
|
91
|
-
options.merge!(expression_attribute_names: {"##{field}_id": "#{field}_id"})
|
92
78
|
|
93
79
|
define_method(associations) do
|
80
|
+
options = {}
|
81
|
+
pluralize_name = sorted_list.map(&:pluralize).join('-')
|
82
|
+
table = Config.namespace ? "#{Config.namespace}-#{pluralize_name}" : pluralize_name
|
83
|
+
options[:table_name] = table
|
84
|
+
if sorted_list.first == base_model
|
85
|
+
field = sorted_list.first
|
86
|
+
else
|
87
|
+
field = sorted_list.last
|
88
|
+
options.merge!(index_name: "#{table}-index")
|
89
|
+
end
|
90
|
+
|
91
|
+
options.merge!(key_condition_expression: "##{field}_id = :#{field}_id")
|
92
|
+
options.merge!(expression_attribute_names: {"##{field}_id": "#{field}_id"})
|
93
|
+
|
94
94
|
options.merge!(expression_attribute_values: {":#{field}_id" => id})
|
95
95
|
|
96
96
|
klass = Object.const_get(relation_model.capitalize)
|
data/lib/dynamodb_record.rb
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe DynamodbRecord::Config, :vcr do
|
4
|
+
|
5
|
+
after do
|
6
|
+
DynamodbRecord::Config.set_defaults
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'has default options' do
|
10
|
+
DynamodbRecord::Config.set_defaults
|
11
|
+
expect(DynamodbRecord::Config.region).to eq('us-east-1')
|
12
|
+
expect(DynamodbRecord::Config.namespace).to eq(nil)
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'sets config options' do
|
16
|
+
DynamodbRecord.configure do |config|
|
17
|
+
config.access_key_id = 'key'
|
18
|
+
config.secret_access_key = 'secret'
|
19
|
+
config.region = 'region'
|
20
|
+
config.namespace = 'fleteo-v2'
|
21
|
+
end
|
22
|
+
|
23
|
+
expect(DynamodbRecord::Config.access_key_id).to eq('key')
|
24
|
+
expect(DynamodbRecord::Config.secret_access_key).to eq('secret')
|
25
|
+
expect(DynamodbRecord::Config.region).to eq('region')
|
26
|
+
expect(DynamodbRecord::Config.namespace).to eq('fleteo-v2')
|
27
|
+
end
|
28
|
+
end
|
@@ -12,10 +12,10 @@ RSpec.describe DynamodbRecord::Document do
|
|
12
12
|
expect(person.attributes).to be_empty
|
13
13
|
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
it 'initializes from database', :vcr do
|
16
|
+
user = User.find('hguzman20@gmail.com')
|
17
|
+
expect(user.new_record).to be_falsy
|
18
|
+
end
|
19
19
|
|
20
20
|
it 'raises error on unknown field' do
|
21
21
|
expect { User.new({unknown_field: 'unknown'}) }.to raise_error(NoMethodError)
|
@@ -5,16 +5,16 @@ require 'spec_helper'
|
|
5
5
|
RSpec.describe DynamodbRecord::Fields, :vcr do
|
6
6
|
describe '#find' do
|
7
7
|
it 'finds record' do
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
user = User.find('hguzman10@gmail.com')
|
9
|
+
expect(user.id).to eq('hguzman10@gmail.com')
|
10
|
+
expect(user.balance).to eq(0)
|
11
11
|
end
|
12
12
|
|
13
13
|
context 'when record doesn\'t exists' do
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
it 'returns empty object' do
|
15
|
+
user = User.find('not here')
|
16
|
+
expect(user).to be_nil
|
17
|
+
end
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -4,10 +4,10 @@ require 'spec_helper'
|
|
4
4
|
|
5
5
|
RSpec.describe DynamodbRecord::Persistence, :vcr do
|
6
6
|
it 'saves record' do
|
7
|
-
user = User.new(id: '
|
7
|
+
user = User.new(id: 'hguzman60@gmail.com')
|
8
8
|
user.save
|
9
9
|
expect(user.new_record).to be_falsy
|
10
|
-
expect(user.id).to eq('
|
10
|
+
expect(user.id).to eq('hguzman60@gmail.com')
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'does not overwrite existing record' do
|
@@ -29,20 +29,20 @@ RSpec.describe DynamodbRecord::Persistence, :vcr do
|
|
29
29
|
describe '#destroy' do
|
30
30
|
context 'when no range key' do
|
31
31
|
it 'destroys record' do
|
32
|
-
user = User.find('
|
32
|
+
user = User.find('hguzman50@gmail.com')
|
33
33
|
user.destroy
|
34
|
-
user = User.find('
|
34
|
+
user = User.find('hguzman50@gmail.com')
|
35
35
|
expect(user).to be_nil
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
context 'when there is range key' do
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
end
|
39
|
+
# context 'when there is range key' do
|
40
|
+
# it 'destroys record' do
|
41
|
+
# authorization = Authorization.find!('hguzman10@gmail.com', '1')
|
42
|
+
# authorization.destroy
|
43
|
+
# authorization = Authorization.find('hguzman10@gmail.com', '1')
|
44
|
+
# expect(authorization).to be_nil
|
45
|
+
# end
|
46
|
+
# end
|
47
47
|
end
|
48
48
|
end
|
@@ -6,8 +6,8 @@ RSpec.describe DynamodbRecord::Query, :vcr do
|
|
6
6
|
describe '#all' do
|
7
7
|
it 'find all records' do
|
8
8
|
user = User.all
|
9
|
-
expect(user.count).to eq(
|
10
|
-
expect(user.map(&:id)).to eq(['hguzman20@gmail.com'])
|
9
|
+
expect(user.count).to eq(2)
|
10
|
+
expect(user.map(&:id)).to eq(['hguzman20@gmail.com', 'hguzman30@gmail.com'])
|
11
11
|
end
|
12
12
|
end
|
13
13
|
describe 'querying' do
|
@@ -5,7 +5,7 @@ http_interactions:
|
|
5
5
|
uri: http://localhost:8000/
|
6
6
|
body:
|
7
7
|
encoding: UTF-8
|
8
|
-
string: '{"TableName":"insurances","Key":{"id":{"S":"12345"}}}'
|
8
|
+
string: '{"TableName":"fleteo-v2-insurances","Key":{"id":{"S":"12345"}}}'
|
9
9
|
headers:
|
10
10
|
Accept-Encoding:
|
11
11
|
- ''
|
@@ -19,15 +19,15 @@ http_interactions:
|
|
19
19
|
Host:
|
20
20
|
- localhost:8000
|
21
21
|
X-Amz-Date:
|
22
|
-
-
|
22
|
+
- 20240514T151341Z
|
23
23
|
X-Amz-Content-Sha256:
|
24
|
-
-
|
24
|
+
- efb57ec65a1d5400e11f2c1d7ef1abae0923c0d84b3e4a442db0abcb6408f474
|
25
25
|
Authorization:
|
26
|
-
- AWS4-HMAC-SHA256 Credential=key/
|
26
|
+
- AWS4-HMAC-SHA256 Credential=key/20240514/us-east-1/dynamodb/aws4_request,
|
27
27
|
SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date;x-amz-target,
|
28
|
-
Signature=
|
28
|
+
Signature=ee626915d5626ac0c4f324b1eb64101d695f890fa0ea6af9b35ee366e0ebb764
|
29
29
|
Content-Length:
|
30
|
-
- '
|
30
|
+
- '63'
|
31
31
|
Accept:
|
32
32
|
- "*/*"
|
33
33
|
response:
|
@@ -36,9 +36,9 @@ http_interactions:
|
|
36
36
|
message: OK
|
37
37
|
headers:
|
38
38
|
Date:
|
39
|
-
-
|
39
|
+
- Tue, 14 May 2024 15:13:41 GMT
|
40
40
|
X-Amzn-Requestid:
|
41
|
-
-
|
41
|
+
- 2e572970-2362-4c8e-ab2a-9d498be1cedc
|
42
42
|
Content-Type:
|
43
43
|
- application/x-amz-json-1.0
|
44
44
|
X-Amz-Crc32:
|
@@ -50,13 +50,13 @@ http_interactions:
|
|
50
50
|
body:
|
51
51
|
encoding: UTF-8
|
52
52
|
string: '{"Item":{"name":{"S":"Colcerautos"},"car_id":{"S":"UVX455"},"id":{"S":"12345"}}}'
|
53
|
-
recorded_at:
|
53
|
+
recorded_at: Tue, 14 May 2024 15:13:41 GMT
|
54
54
|
- request:
|
55
55
|
method: post
|
56
56
|
uri: http://localhost:8000/
|
57
57
|
body:
|
58
58
|
encoding: UTF-8
|
59
|
-
string: '{"TableName":"cars","Key":{"id":{"S":"UVX455"}}}'
|
59
|
+
string: '{"TableName":"fleteo-v2-cars","Key":{"id":{"S":"UVX455"}}}'
|
60
60
|
headers:
|
61
61
|
Accept-Encoding:
|
62
62
|
- ''
|
@@ -70,15 +70,15 @@ http_interactions:
|
|
70
70
|
Host:
|
71
71
|
- localhost:8000
|
72
72
|
X-Amz-Date:
|
73
|
-
-
|
73
|
+
- 20240514T151341Z
|
74
74
|
X-Amz-Content-Sha256:
|
75
|
-
-
|
75
|
+
- 3f40080fdb7ac1732232c14b86645a9ffbd3f94cf8ecda6b3c584d105288bb8d
|
76
76
|
Authorization:
|
77
|
-
- AWS4-HMAC-SHA256 Credential=key/
|
77
|
+
- AWS4-HMAC-SHA256 Credential=key/20240514/us-east-1/dynamodb/aws4_request,
|
78
78
|
SignedHeaders=content-type;host;x-amz-content-sha256;x-amz-date;x-amz-target,
|
79
|
-
Signature=
|
79
|
+
Signature=6efa4f968328b726cd30c758f09068bad22f44524d301b0bbc9d198a873aac06
|
80
80
|
Content-Length:
|
81
|
-
- '
|
81
|
+
- '58'
|
82
82
|
Accept:
|
83
83
|
- "*/*"
|
84
84
|
response:
|
@@ -87,9 +87,9 @@ http_interactions:
|
|
87
87
|
message: OK
|
88
88
|
headers:
|
89
89
|
Date:
|
90
|
-
-
|
90
|
+
- Tue, 14 May 2024 15:13:41 GMT
|
91
91
|
X-Amzn-Requestid:
|
92
|
-
-
|
92
|
+
- 25132fc8-9bae-4ed1-86e4-09bb515d7d51
|
93
93
|
Content-Type:
|
94
94
|
- application/x-amz-json-1.0
|
95
95
|
X-Amz-Crc32:
|
@@ -101,5 +101,5 @@ http_interactions:
|
|
101
101
|
body:
|
102
102
|
encoding: UTF-8
|
103
103
|
string: '{"Item":{"marca":{"S":"Chvrolet"},"created_at":{"S":"2024-05-07T14:09:25-05:00"},"id":{"S":"UVX455"},"updated_at":{"S":"2024-05-07T14:09:25-05:00"}}}'
|
104
|
-
recorded_at:
|
104
|
+
recorded_at: Tue, 14 May 2024 15:13:41 GMT
|
105
105
|
recorded_with: VCR 6.2.0
|