Class: Dynamoid::TransactionWrite::UpdateUpsert
- Defined in:
- lib/dynamoid/transaction_write/update_upsert.rb
Constant Summary
Constants inherited from Action
Instance Attribute Summary
Attributes inherited from Action
#additions, #attributes, #deletions, #model, #options, #removals
Instance Method Summary collapse
-
#initialize(model_or_model_class, attributes = {}, options = {}, &block) ⇒ UpdateUpsert
constructor
A new instance of UpdateUpsert.
-
#to_h ⇒ Object
shared by Update and Upsert.
Methods inherited from Action
#add, #add_timestamps, #changes_applied, #delete, #find_from_attributes, #hash_key, #model_class, #raise_validation_error?, #range_key, #run_callbacks, #sanitize_item, #set, #skip_callbacks?, #skip_validation?, #touch_model_timestamps, #valid?, #write_attributes_to_model
Constructor Details
#initialize(model_or_model_class, attributes = {}, options = {}, &block) ⇒ UpdateUpsert
Returns a new instance of UpdateUpsert.
8 9 10 11 12 |
# File 'lib/dynamoid/transaction_write/update_upsert.rb', line 8 def initialize(model_or_model_class, attributes = {}, = {}, &block) super(model_or_model_class, attributes, , &block) write_attributes_to_model end |
Instance Method Details
#to_h ⇒ Object
shared by Update and Upsert
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dynamoid/transaction_write/update_upsert.rb', line 15 def to_h if model # model.hash_key = SecureRandom.uuid if model.hash_key.blank? (skip_created_at: true) changes = model.changes.map { |k, v| [k.to_sym, v[1]] }.to_h # hash of dirty attributes else changes = attributes.clone || {} # changes[model_class.hash_key] = SecureRandom.uuid changes = (changes, skip_created_at: true) end changes.delete(model_class.hash_key) # can't update id! changes.delete(model_class.range_key) if model_class.range_key? item = Dynamoid::Dumping.dump_attributes(changes, model_class.attributes) # set 'key' that is used to look up record for updating key = { model_class.hash_key => hash_key } key[model_class.range_key] = range_key if model_class.range_key? # e.g. "SET #updated_at = :updated_at ADD record_count :i" item_keys = item.keys update_expression = "SET #{item_keys.each_with_index.map { |_k, i| "#_n#{i} = :_s#{i}" }.join(', ')}" # e.g. {":updated_at" => 1645453.234, ":i" => 1} expression_attribute_values = item_keys.each_with_index.map { |k, i| [":_s#{i}", item[k]] }.to_h expression_attribute_names = {} update_expression = set_additions(expression_attribute_values, update_expression) update_expression = set_deletions(expression_attribute_values, update_expression) expression_attribute_names, update_expression = set_removals(expression_attribute_names, update_expression) # only alias names for fields in models, other values such as for ADD do not have them # e.g. {"#updated_at" => "updated_at"} # attribute_keys_in_model = item_keys.intersection(model_class.attributes.keys) # expression_attribute_names = attribute_keys_in_model.map{|k| ["##{k}","#{k}"]}.to_h expression_attribute_names.merge!(item_keys.each_with_index.map { |k, i| ["#_n#{i}", k.to_s] }.to_h) condition_expression = "attribute_exists(#{model_class.hash_key})" # fail if record is missing condition_expression += " and attribute_exists(#{model_class.range_key})" if model_class.range_key? # needed? result = { update: { key: key, table_name: model_class.table_name, update_expression: update_expression, expression_attribute_values: expression_attribute_values } } result[:update][:expression_attribute_names] = expression_attribute_names if expression_attribute_names.present? result[:update][:condition_expression] = condition_expression unless [:skip_existence_check] result end |