Skip to content

Commit f31370f

Browse files
committed
Add new store_attribute_with_nil_value config option
1 parent 8648087 commit f31370f

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

lib/dynamoid/adapter_plugin/aws_sdk_v3.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,8 +639,12 @@ def self.attribute_value_list(operator, value)
639639
end
640640

641641
def sanitize_item(attributes)
642+
config_value = Dynamoid.config.store_attribute_with_nil_value
643+
store_attribute_with_nil_value = config_value.nil? ? false : !!config_value
644+
642645
attributes.reject do |_, v|
643-
v.nil? || ((v.is_a?(Set) || v.is_a?(String)) && v.empty?)
646+
((v.is_a?(Set) || v.is_a?(String)) && v.empty?) ||
647+
(!store_attribute_with_nil_value && v.nil?)
644648
end.transform_values do |v|
645649
v.is_a?(Hash) ? v.stringify_keys : v
646650
end

lib/dynamoid/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ module Config
3131
option :sync_retry_max_times, default: 60 # a bit over 2 minutes
3232
option :sync_retry_wait_seconds, default: 2
3333
option :convert_big_decimal, default: false
34+
option :store_attribute_with_nil_value, default: false # keep or ignore attribute with nil value at saving
3435
option :models_dir, default: './app/models' # perhaps you keep your dynamoid models in a different directory?
3536
option :application_timezone, default: :utc # available values - :utc, :local, time zone name like "Hawaii"
3637
option :dynamodb_timezone, default: :utc # available values - :utc, :local, time zone name like "Hawaii"

spec/dynamoid/persistence_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,43 @@ def reload_address
695695
end
696696
end
697697
end
698+
699+
describe '`store_attribute_with_nil_value` config option' do
700+
let(:klass) do
701+
new_class do
702+
field :age, :integer
703+
end
704+
end
705+
706+
context 'true', config: { store_attribute_with_nil_value: true } do
707+
it 'keeps document attribute with nil' do
708+
obj = klass.new(age: nil)
709+
obj.save
710+
711+
expect(raw_attributes(obj)).to include(age: nil)
712+
end
713+
end
714+
715+
context 'false', config: { store_attribute_with_nil_value: false } do
716+
it 'does not keep document attribute with nil' do
717+
obj = klass.new(age: nil)
718+
obj.save
719+
720+
# doesn't contain :age key
721+
expect(raw_attributes(obj).keys).to contain_exactly(:id, :created_at, :updated_at)
722+
end
723+
end
724+
725+
context 'by default', config: { store_attribute_with_nil_value: nil } do
726+
it 'does not keep document attribute with nil' do
727+
obj = klass.new(age: nil)
728+
obj.save
729+
730+
# doesn't contain :age key
731+
expect(raw_attributes(obj).keys).to contain_exactly(:id, :created_at, :updated_at)
732+
end
733+
end
734+
end
698735
end
699736

700737
describe '#update_attribute' do

0 commit comments

Comments
 (0)