Given I am using Hanami Model version 0.6.1, I would like the repository update only the changed attributes of an entity.
For example:
user_instance1 = UserRepository.find(1)
user_instance1.name = 'John'
user_instance2 = UserRepository.find(1)
user_instance2.email = '[email protected]'
UserRepository.update(user_instance1)
#expected: UPDATE USER SET NAME = 'John' WHERE ID = 1
UserRepository.update(user_instance2)
#expected: UPDATE USER SET EMAIL = '[email protected]' WHERE ID = 1
But what it happens is that the second command overrides all fields, including those which were not changed.
I know I can use the Hanami::Entity::DirtyTracking
to get all changed attributes, but I do not know how to update an entity partially with these attributes.
Is there a way to do this?
hanami entity is an immutable data structure. That's why you can't change data with setters:
Instead, you can create a new one entity, for example:
And also, you can use
#update
with old entity object: