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.
Files changed (31) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dynamodb_record/associations.rb +33 -33
  3. data/lib/dynamodb_record/finders.rb +0 -1
  4. data/lib/dynamodb_record/query.rb +1 -0
  5. data/lib/dynamodb_record/version.rb +1 -1
  6. data/lib/dynamodb_record.rb +1 -0
  7. data/spec/dynamodb_record/config_spec.rb +28 -0
  8. data/spec/dynamodb_record/document_spec.rb +4 -4
  9. data/spec/dynamodb_record/finders_spec.rb +7 -7
  10. data/spec/dynamodb_record/persistence_spec.rb +12 -12
  11. data/spec/dynamodb_record/query_spec.rb +2 -2
  12. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/_belongs_to/get_object.yml +18 -18
  13. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/_has_many/add_list.yml +108 -57
  14. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/_has_many/create_item.yml +28 -27
  15. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/_has_many/get_collection.yml +19 -18
  16. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/has_and_belongs_to_many.yml +31 -31
  17. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/has_and_belongs_to_many_create.yml +91 -39
  18. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Associations/has_many_through.yml +30 -27
  19. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Document/initializes_from_database.yml +11 -11
  20. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Fields/_find/finds_record.yml +12 -12
  21. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Fields/_find/when_record_doesn_t_exists/returns_empty_object.yml +9 -9
  22. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/_destroy/when_no_range_key/destroys_record.yml +30 -30
  23. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/_destroy/when_there_is_range_key/destroys_record.yml +9 -111
  24. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/does_not_overwrite_existing_record.yml +20 -20
  25. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/saves_record.yml +9 -9
  26. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Persistence/updates_record.yml +31 -31
  27. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Query/_all/find_all_records.yml +12 -12
  28. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Query/querying/_where/returns_records_by_limit.yml +12 -12
  29. data/spec/fixtures/vcr_cassettes/DynamodbRecord_Query/querying/_where/returns_records_where_user_balance_0.yml +12 -12
  30. data/spec/spec_helper.rb +7 -6
  31. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90c0fa26f80217dbc4d6a3f2e3fa0197a485fdcaf4bb845232c276d67c2c678e
4
- data.tar.gz: 61d5b4ca06cfb8906902aee1ae6bafc0c4613f03edfb402447514f9877317143
3
+ metadata.gz: 24d59dadc67c9fe2d0d70b78c5cce8d1af52505f2b49f539ee1cb407be8bff13
4
+ data.tar.gz: c0de287ed037955f41ccfc29d497ca5299d1efff797a180edb819346b5c9a926
5
5
  SHA512:
6
- metadata.gz: ea0d8a245f9f6a41c47d4cf0ef9f5ba056c31712588146b9b14f0c0886f36b487fa691c79383e93bbe34e3c87ceebdd5e8b80bb3b4f1a4ed48812944a9970985
7
- data.tar.gz: 3c734251bd6e00ebc3ab048e702b9ed133296e0acdc09690b6da12b8c9ec7edb3f13d09a28c837d2393dc637359e228c36e334365e1a497f11890145d8db92e9
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)
@@ -16,7 +16,6 @@ module DynamodbRecord
16
16
  key = {}
17
17
  key[hash_key] = id
18
18
  key[self.range_key] = range_key if self.range_key
19
-
20
19
  response = client.get_item(
21
20
  table_name:,
22
21
  key:
@@ -40,6 +40,7 @@ module DynamodbRecord
40
40
  options.merge!(filter_expression:) if filter_expression.present?
41
41
 
42
42
  options.merge!(exclusive_start_key:) if exclusive_start_key
43
+ # p options
43
44
 
44
45
  scan_pager = ScanPager.new(options, self)
45
46
  Collection.new(scan_pager, self)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DynamodbRecord
4
- VERSION = '0.3.1'
4
+ VERSION = '0.3.3'
5
5
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'bundler/setup'
3
4
  require 'securerandom'
4
5
  require 'active_support'
5
6
  require 'active_support/concern'
@@ -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
- # it 'initializes from database', :vcr do
16
- # user = User.find('hguzman10@gmail.com')
17
- # expect(user.new_record).to be_falsy
18
- # end
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
- # user = User.find('hguzman10@gmail.com')
9
- # expect(user.id).to eq('hguzman10@gmail.com')
10
- # expect(user.balance).to eq(0)
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
- # it 'returns empty object' do
15
- # user = User.find('not here')
16
- # expect(user).to be_nil
17
- # end
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: 'hguzman10@gmail.com')
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('hguzman10@gmail.com')
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('hguzman10@gmail.com')
32
+ user = User.find('hguzman50@gmail.com')
33
33
  user.destroy
34
- user = User.find('hguzman10@gmail.com')
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
- 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
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(1)
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
- - 20240512T015903Z
22
+ - 20240514T151341Z
23
23
  X-Amz-Content-Sha256:
24
- - af4fbab2106a31baca5b683427566e6620dff714a4febf67de21d62fe9aff004
24
+ - efb57ec65a1d5400e11f2c1d7ef1abae0923c0d84b3e4a442db0abcb6408f474
25
25
  Authorization:
26
- - AWS4-HMAC-SHA256 Credential=key/20240512/us-east-1/dynamodb/aws4_request,
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=dc0c15194ac1d51a4ad8984e48a0cc3114c900a6b00e2f4dbe13d1a32a53e894
28
+ Signature=ee626915d5626ac0c4f324b1eb64101d695f890fa0ea6af9b35ee366e0ebb764
29
29
  Content-Length:
30
- - '53'
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
- - Sun, 12 May 2024 01:59:03 GMT
39
+ - Tue, 14 May 2024 15:13:41 GMT
40
40
  X-Amzn-Requestid:
41
- - 20483dcf-8a64-4be9-afd8-a5c35c328725
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: Sun, 12 May 2024 01:59:03 GMT
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
- - 20240512T015903Z
73
+ - 20240514T151341Z
74
74
  X-Amz-Content-Sha256:
75
- - 53644a2bbab903704867eb87df0fdfe6dd8cb8ac9e1dae5715033d0f636fd592
75
+ - 3f40080fdb7ac1732232c14b86645a9ffbd3f94cf8ecda6b3c584d105288bb8d
76
76
  Authorization:
77
- - AWS4-HMAC-SHA256 Credential=key/20240512/us-east-1/dynamodb/aws4_request,
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=436e2db7a75c43c788be0473b6806936b97b7398f33009895010fa5a3eaee5c4
79
+ Signature=6efa4f968328b726cd30c758f09068bad22f44524d301b0bbc9d198a873aac06
80
80
  Content-Length:
81
- - '48'
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
- - Sun, 12 May 2024 01:59:03 GMT
90
+ - Tue, 14 May 2024 15:13:41 GMT
91
91
  X-Amzn-Requestid:
92
- - b464ed64-837b-4632-9b14-93a788d62b8d
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: Sun, 12 May 2024 01:59:03 GMT
104
+ recorded_at: Tue, 14 May 2024 15:13:41 GMT
105
105
  recorded_with: VCR 6.2.0