Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TransactionWrite update() with a block #846

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ckhsponge
Copy link
Contributor

WIP

def update(model, &block)
action = Dynamoid::TransactionWrite::Update.new(model, raise_error: false, &block)
register_action action
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new transactional method #update behaves a bit differently than the non-transactional one. Non-transactional updates a model and its fields with new values. It's easy enough because UpdateItem may be configured to return all attributes of an updated item. TransactWriteItems doesn't support this AFAIK. And manually implement logic of remove/add operations to update attributes in memory also doesn't appeal to me. So accepting a model as a parameter seems incorrect to me.

Instead I would propose to accept a primary key as a parameter. There is no such non-transactional method but I will add it later to complement existing #update method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct. Because of that should we just change update_fields to optionally allow a block or do you think adding an entirely new method to TransactionWrite would be better?

Copy link
Member

@andrykonchin andrykonchin Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer adding new method update in the TransactionWrite module (like it was already done here).

I overlooked that semantic of existing .update method is find && update_attributes. And indeed .update_fields looks very close to the #update method.

So go ahead with adding a block parameter to the existing transactional method .update_fields.

else
remove(field_or_values)
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to have a separate class to collect user actions (I mean to set/add/delete), similar to ItemUpdater (https://github.com/Dynamoid/dynamoid/blob/master/lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb)

@@ -1106,6 +1106,7 @@ model
* `#destroy`/`#destroy!` - remove an model
* `#upsert` - add a new model or update an existing one, no callbacks
* `#update_fields` - update a model without its instantiation
* `#update` - update a model using a block that supports set, add, and delete operations on fields
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* `#update` - update a model using a block that supports set, add, and delete operations on fields
* `#update` - update a model using a block that supports set, add, remove, and delete operations on fields

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants