dynamoid 3.10.0 → 3.12.1

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 (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -1
  3. data/README.md +268 -8
  4. data/dynamoid.gemspec +4 -4
  5. data/lib/dynamoid/adapter.rb +1 -1
  6. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/filter_expression_convertor.rb +53 -18
  7. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb +5 -4
  8. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/projection_expression_convertor.rb +9 -7
  9. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/query.rb +1 -1
  10. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/scan.rb +1 -1
  11. data/lib/dynamoid/adapter_plugin/aws_sdk_v3/transact.rb +31 -0
  12. data/lib/dynamoid/adapter_plugin/aws_sdk_v3.rb +17 -5
  13. data/lib/dynamoid/components.rb +1 -0
  14. data/lib/dynamoid/config.rb +3 -0
  15. data/lib/dynamoid/criteria/chain.rb +74 -21
  16. data/lib/dynamoid/criteria/where_conditions.rb +13 -6
  17. data/lib/dynamoid/dirty.rb +97 -11
  18. data/lib/dynamoid/dumping.rb +39 -17
  19. data/lib/dynamoid/errors.rb +30 -3
  20. data/lib/dynamoid/fields.rb +13 -3
  21. data/lib/dynamoid/finders.rb +44 -23
  22. data/lib/dynamoid/loadable.rb +1 -0
  23. data/lib/dynamoid/persistence/inc.rb +35 -19
  24. data/lib/dynamoid/persistence/item_updater_with_casting_and_dumping.rb +36 -0
  25. data/lib/dynamoid/persistence/item_updater_with_dumping.rb +33 -0
  26. data/lib/dynamoid/persistence/save.rb +29 -14
  27. data/lib/dynamoid/persistence/update_fields.rb +23 -8
  28. data/lib/dynamoid/persistence/update_validations.rb +3 -3
  29. data/lib/dynamoid/persistence/upsert.rb +22 -8
  30. data/lib/dynamoid/persistence.rb +184 -28
  31. data/lib/dynamoid/transaction_read/find.rb +137 -0
  32. data/lib/dynamoid/transaction_read.rb +146 -0
  33. data/lib/dynamoid/transaction_write/base.rb +47 -0
  34. data/lib/dynamoid/transaction_write/create.rb +49 -0
  35. data/lib/dynamoid/transaction_write/delete_with_instance.rb +65 -0
  36. data/lib/dynamoid/transaction_write/delete_with_primary_key.rb +64 -0
  37. data/lib/dynamoid/transaction_write/destroy.rb +84 -0
  38. data/lib/dynamoid/transaction_write/item_updater.rb +55 -0
  39. data/lib/dynamoid/transaction_write/save.rb +169 -0
  40. data/lib/dynamoid/transaction_write/update_attributes.rb +46 -0
  41. data/lib/dynamoid/transaction_write/update_fields.rb +239 -0
  42. data/lib/dynamoid/transaction_write/upsert.rb +106 -0
  43. data/lib/dynamoid/transaction_write.rb +673 -0
  44. data/lib/dynamoid/type_casting.rb +3 -1
  45. data/lib/dynamoid/undumping.rb +13 -2
  46. data/lib/dynamoid/validations.rb +8 -5
  47. data/lib/dynamoid/version.rb +1 -1
  48. data/lib/dynamoid.rb +8 -0
  49. metadata +21 -5
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dynamoid
4
+ class TransactionWrite
5
+ class Base
6
+ # Callback called at "initialization" or "registration" an action
7
+ # before changes are persisted. It's a proper place to validate
8
+ # a model or run callbacks
9
+ def on_registration
10
+ raise 'Not implemented'
11
+ end
12
+
13
+ # Callback called after changes are persisted.
14
+ # It's a proper place to mark changes in a model as applied.
15
+ def on_commit
16
+ raise 'Not implemented'
17
+ end
18
+
19
+ # Callback called when a transaction is rolled back.
20
+ # It's a proper place to undo changes made in after_... callbacks.
21
+ def on_rollback
22
+ raise 'Not implemented'
23
+ end
24
+
25
+ # Whether some callback aborted or canceled an action
26
+ def aborted?
27
+ raise 'Not implemented'
28
+ end
29
+
30
+ # Whether there are changes to persist, e.g. updating a model with no
31
+ # attribute changed is skipped.
32
+ def skipped?
33
+ raise 'Not implemented'
34
+ end
35
+
36
+ # Value returned to a user as an action result
37
+ def observable_by_user_result
38
+ raise 'Not implemented'
39
+ end
40
+
41
+ # Coresponding part of a final request body
42
+ def action_request
43
+ raise 'Not implemented'
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Dynamoid
6
+ class TransactionWrite
7
+ class Create < Base
8
+ def initialize(model_class, attributes = {}, **options, &block)
9
+ super()
10
+
11
+ @model = model_class.new(attributes)
12
+
13
+ if block
14
+ yield(@model)
15
+ end
16
+
17
+ @save_action = Save.new(@model, **options)
18
+ end
19
+
20
+ def on_registration
21
+ @save_action.on_registration
22
+ end
23
+
24
+ def on_commit
25
+ @save_action.on_commit
26
+ end
27
+
28
+ def on_rollback
29
+ @save_action.on_rollback
30
+ end
31
+
32
+ def aborted?
33
+ @save_action.aborted?
34
+ end
35
+
36
+ def skipped?
37
+ false
38
+ end
39
+
40
+ def observable_by_user_result
41
+ @model
42
+ end
43
+
44
+ def action_request
45
+ @save_action.action_request
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Dynamoid
6
+ class TransactionWrite
7
+ class DeleteWithInstance < Base
8
+ def initialize(model)
9
+ super()
10
+
11
+ @model = model
12
+ @model_class = model.class
13
+ end
14
+
15
+ def on_registration
16
+ validate_model!
17
+ end
18
+
19
+ def on_commit
20
+ @model.destroyed = true
21
+ end
22
+
23
+ def on_rollback; end
24
+
25
+ def aborted?
26
+ false
27
+ end
28
+
29
+ def skipped?
30
+ false
31
+ end
32
+
33
+ def observable_by_user_result
34
+ @model
35
+ end
36
+
37
+ def action_request
38
+ key = { @model_class.hash_key => dump_attribute(@model_class.hash_key, @model.hash_key) }
39
+
40
+ if @model_class.range_key?
41
+ key[@model_class.range_key] = dump_attribute(@model_class.range_key, @model.range_value)
42
+ end
43
+
44
+ {
45
+ delete: {
46
+ key: key,
47
+ table_name: @model_class.table_name
48
+ }
49
+ }
50
+ end
51
+
52
+ private
53
+
54
+ def validate_model!
55
+ raise Dynamoid::Errors::MissingHashKey if @model.hash_key.nil?
56
+ raise Dynamoid::Errors::MissingRangeKey if @model_class.range_key? && @model.range_value.nil?
57
+ end
58
+
59
+ def dump_attribute(name, value)
60
+ options = @model_class.attributes[name]
61
+ Dumping.dump_field(value, options)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Dynamoid
6
+ class TransactionWrite
7
+ class DeleteWithPrimaryKey < Base
8
+ def initialize(model_class, hash_key, range_key)
9
+ super()
10
+
11
+ @model_class = model_class
12
+ @hash_key = hash_key
13
+ @range_key = range_key
14
+ end
15
+
16
+ def on_registration
17
+ validate_primary_key!
18
+ end
19
+
20
+ def on_commit; end
21
+
22
+ def on_rollback; end
23
+
24
+ def aborted?
25
+ false
26
+ end
27
+
28
+ def skipped?
29
+ false
30
+ end
31
+
32
+ def observable_by_user_result
33
+ nil
34
+ end
35
+
36
+ def action_request
37
+ key = { @model_class.hash_key => dump_attribute(@model_class.hash_key, @hash_key) }
38
+
39
+ if @model_class.range_key?
40
+ key[@model_class.range_key] = dump_attribute(@model_class.range_key, @range_key)
41
+ end
42
+
43
+ {
44
+ delete: {
45
+ key: key,
46
+ table_name: @model_class.table_name
47
+ }
48
+ }
49
+ end
50
+
51
+ private
52
+
53
+ def validate_primary_key!
54
+ raise Dynamoid::Errors::MissingHashKey if @hash_key.nil?
55
+ raise Dynamoid::Errors::MissingRangeKey if @model_class.range_key? && @range_key.nil?
56
+ end
57
+
58
+ def dump_attribute(name, value)
59
+ options = @model_class.attributes[name]
60
+ Dumping.dump_field(value, options)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Dynamoid
6
+ class TransactionWrite
7
+ class Destroy < Base
8
+ def initialize(model, **options)
9
+ super()
10
+
11
+ @model = model
12
+ @options = options
13
+ @model_class = model.class
14
+ @aborted = false
15
+ end
16
+
17
+ def on_registration
18
+ validate_model!
19
+
20
+ @aborted = true
21
+ @model.run_callbacks(:destroy) do
22
+ @aborted = false
23
+ true
24
+ end
25
+
26
+ if @aborted && @options[:raise_error]
27
+ raise Dynamoid::Errors::RecordNotDestroyed, @model
28
+ end
29
+ end
30
+
31
+ def on_commit
32
+ return if @aborted
33
+
34
+ @model.destroyed = true
35
+ @model.run_callbacks(:commit)
36
+ end
37
+
38
+ def on_rollback
39
+ @model.run_callbacks(:rollback)
40
+ end
41
+
42
+ def aborted?
43
+ @aborted
44
+ end
45
+
46
+ def skipped?
47
+ false
48
+ end
49
+
50
+ def observable_by_user_result
51
+ return false if @aborted
52
+
53
+ @model
54
+ end
55
+
56
+ def action_request
57
+ key = { @model_class.hash_key => dump_attribute(@model_class.hash_key, @model.hash_key) }
58
+
59
+ if @model_class.range_key?
60
+ key[@model_class.range_key] = dump_attribute(@model_class.range_key, @model.range_value)
61
+ end
62
+
63
+ {
64
+ delete: {
65
+ key: key,
66
+ table_name: @model_class.table_name
67
+ }
68
+ }
69
+ end
70
+
71
+ private
72
+
73
+ def validate_model!
74
+ raise Dynamoid::Errors::MissingHashKey if @model.hash_key.nil?
75
+ raise Dynamoid::Errors::MissingRangeKey if @model_class.range_key? && @model.range_value.nil?
76
+ end
77
+
78
+ def dump_attribute(name, value)
79
+ options = @model_class.attributes[name]
80
+ Dumping.dump_field(value, options)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dynamoid
4
+ class TransactionWrite
5
+ class ItemUpdater
6
+ attr_reader :attributes_to_set, :attributes_to_add, :attributes_to_delete, :attributes_to_remove
7
+
8
+ def initialize(model_class)
9
+ @model_class = model_class
10
+
11
+ @attributes_to_set = {}
12
+ @attributes_to_add = {}
13
+ @attributes_to_delete = {}
14
+ @attributes_to_remove = []
15
+ end
16
+
17
+ def empty?
18
+ [@attributes_to_set, @attributes_to_add, @attributes_to_delete, @attributes_to_remove].all?(&:empty?)
19
+ end
20
+
21
+ def set(attributes)
22
+ validate_attribute_names!(attributes.keys)
23
+ @attributes_to_set.merge!(attributes)
24
+ end
25
+
26
+ # adds to array of fields for use in REMOVE update expression
27
+ def remove(*names)
28
+ validate_attribute_names!(names)
29
+ @attributes_to_remove += names
30
+ end
31
+
32
+ # increments a number or adds to a set, starts at 0 or [] if it doesn't yet exist
33
+ def add(attributes)
34
+ validate_attribute_names!(attributes.keys)
35
+ @attributes_to_add.merge!(attributes)
36
+ end
37
+
38
+ # deletes a value or values from a set
39
+ def delete(attributes)
40
+ validate_attribute_names!(attributes.keys)
41
+ @attributes_to_delete.merge!(attributes)
42
+ end
43
+
44
+ private
45
+
46
+ def validate_attribute_names!(names)
47
+ names.each do |name|
48
+ unless @model_class.attributes[name]
49
+ raise Dynamoid::Errors::UnknownAttribute.new(@model_class, name)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Dynamoid
6
+ class TransactionWrite
7
+ class Save < Base
8
+ def initialize(model, **options)
9
+ super()
10
+
11
+ @model = model
12
+ @model_class = model.class
13
+ @options = options
14
+
15
+ @aborted = false
16
+ @was_new_record = model.new_record?
17
+ @valid = nil
18
+ end
19
+
20
+ def on_registration
21
+ validate_model!
22
+
23
+ if @options[:validate] != false && !(@valid = @model.valid?)
24
+ if @options[:raise_error]
25
+ raise Dynamoid::Errors::DocumentNotValid, @model
26
+ else
27
+ @aborted = true
28
+ return
29
+ end
30
+ end
31
+
32
+ @aborted = true
33
+ callback_name = @was_new_record ? :create : :update
34
+
35
+ @model.run_callbacks(:save) do
36
+ @model.run_callbacks(callback_name) do
37
+ @model.run_callbacks(:validate) do
38
+ @aborted = false
39
+ true
40
+ end
41
+ end
42
+ end
43
+
44
+ if @aborted && @options[:raise_error]
45
+ raise Dynamoid::Errors::RecordNotSaved, @model
46
+ end
47
+
48
+ if @was_new_record && @model.hash_key.nil?
49
+ @model.hash_key = SecureRandom.uuid
50
+ end
51
+ end
52
+
53
+ def on_commit
54
+ return if @aborted
55
+
56
+ @model.changes_applied
57
+
58
+ if @was_new_record
59
+ @model.new_record = false
60
+ end
61
+
62
+ @model.run_callbacks(:commit)
63
+ end
64
+
65
+ def on_rollback
66
+ @model.run_callbacks(:rollback)
67
+ end
68
+
69
+ def aborted?
70
+ @aborted
71
+ end
72
+
73
+ def skipped?
74
+ @model.persisted? && !@model.changed?
75
+ end
76
+
77
+ def observable_by_user_result
78
+ !@aborted
79
+ end
80
+
81
+ def action_request
82
+ if @was_new_record
83
+ action_request_to_create
84
+ else
85
+ action_request_to_update
86
+ end
87
+ end
88
+
89
+ private
90
+
91
+ def validate_model!
92
+ raise Dynamoid::Errors::MissingHashKey if !@was_new_record && @model.hash_key.nil?
93
+ raise Dynamoid::Errors::MissingRangeKey if @model_class.range_key? && @model.range_value.nil?
94
+ end
95
+
96
+ def action_request_to_create
97
+ touch_model_timestamps(skip_created_at: false)
98
+
99
+ attributes_dumped = Dynamoid::Dumping.dump_attributes(@model.attributes, @model_class.attributes)
100
+
101
+ # require primary key not to exist yet
102
+ condition = "attribute_not_exists(#{@model_class.hash_key})"
103
+ if @model_class.range_key?
104
+ condition += " AND attribute_not_exists(#{@model_class.range_key})"
105
+ end
106
+
107
+ {
108
+ put: {
109
+ item: attributes_dumped,
110
+ table_name: @model_class.table_name,
111
+ condition_expression: condition
112
+ }
113
+ }
114
+ end
115
+
116
+ def action_request_to_update
117
+ touch_model_timestamps(skip_created_at: true)
118
+
119
+ # changed attributes to persist
120
+ changes = @model.attributes.slice(*@model.changed.map(&:to_sym))
121
+ changes_dumped = Dynamoid::Dumping.dump_attributes(changes, @model_class.attributes)
122
+
123
+ # primary key to look up an item to update
124
+ key = { @model_class.hash_key => dump_attribute(@model_class.hash_key, @model.hash_key) }
125
+ key[@model_class.range_key] = dump_attribute(@model_class.range_key, @model.range_value) if @model_class.range_key?
126
+
127
+ # Build UpdateExpression and keep names and values placeholders mapping
128
+ # in ExpressionAttributeNames and ExpressionAttributeValues.
129
+ update_expression_statements = []
130
+ expression_attribute_names = {}
131
+ expression_attribute_values = {}
132
+
133
+ changes_dumped.each_with_index do |(name, value), i|
134
+ name_placeholder = "#_n#{i}"
135
+ value_placeholder = ":_s#{i}"
136
+
137
+ update_expression_statements << "#{name_placeholder} = #{value_placeholder}"
138
+ expression_attribute_names[name_placeholder] = name
139
+ expression_attribute_values[value_placeholder] = value
140
+ end
141
+
142
+ update_expression = "SET #{update_expression_statements.join(', ')}"
143
+
144
+ {
145
+ update: {
146
+ key: key,
147
+ table_name: @model_class.table_name,
148
+ update_expression: update_expression,
149
+ expression_attribute_names: expression_attribute_names,
150
+ expression_attribute_values: expression_attribute_values
151
+ }
152
+ }
153
+ end
154
+
155
+ def touch_model_timestamps(skip_created_at:)
156
+ return unless @model_class.timestamps_enabled?
157
+
158
+ timestamp = DateTime.now.in_time_zone(Time.zone)
159
+ @model.updated_at = timestamp unless @options[:touch] == false && !@was_new_record
160
+ @model.created_at ||= timestamp unless skip_created_at
161
+ end
162
+
163
+ def dump_attribute(name, value)
164
+ options = @model_class.attributes[name]
165
+ Dumping.dump_field(value, options)
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+ require 'dynamoid/persistence/update_validations'
5
+
6
+ module Dynamoid
7
+ class TransactionWrite
8
+ class UpdateAttributes < Base
9
+ def initialize(model, attributes, **options)
10
+ super()
11
+
12
+ @model = model
13
+ @model.assign_attributes(attributes)
14
+ @save_action = Save.new(model, **options)
15
+ end
16
+
17
+ def on_registration
18
+ @save_action.on_registration
19
+ end
20
+
21
+ def on_commit
22
+ @save_action.on_commit
23
+ end
24
+
25
+ def on_rollback
26
+ @save_action.on_rollback
27
+ end
28
+
29
+ def aborted?
30
+ @save_action.aborted?
31
+ end
32
+
33
+ def skipped?
34
+ @save_action.skipped?
35
+ end
36
+
37
+ def observable_by_user_result
38
+ @save_action.observable_by_user_result
39
+ end
40
+
41
+ def action_request
42
+ @save_action.action_request
43
+ end
44
+ end
45
+ end
46
+ end