Fixed model attribute synchronization for update method#2062
Merged
ricardograca merged 2 commits intobookshelf:masterfrom Mar 22, 2020
Merged
Fixed model attribute synchronization for update method#2062ricardograca merged 2 commits intobookshelf:masterfrom
ricardograca merged 2 commits intobookshelf:masterfrom
Conversation
closes bookshelf#2007 - The attributes object can be modified within `triggerThen` through hooks. Sample scenario described here bookshelf#2007 (comment). - Recalculating 'attributesToSave' object in after-triggerThen step solves the syncronization issue because it is still based on model's `attributes` property (the reference is not lost)
1 task
ricardograca
requested changes
Mar 6, 2020
Member
ricardograca
left a comment
There was a problem hiding this comment.
Can you add a test for the use case this is trying to fix? That should prevent the issue from being reintroduced in the future.
Contributor
Author
|
Hey @ricardograca 👋 let me know if the test is illustrative enough and if anything else is needed from my side fro this change to land in master. |
ricardograca
approved these changes
Mar 22, 2020
Member
ricardograca
left a comment
There was a problem hiding this comment.
Seems to do what it's supposed to. Thanks!
Contributor
Author
|
Thanks @ricardograca I'll get the commit cherry-picked into https://github.com/bookshelf/bookshelf/tree/0.15 in coming days. Thank you very much for all the help! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduction
When model's
attributesare modified during even hooks, e.g.on('save'), changes are not detected and incorrectattributesare persisted in the db.Motivation
As described in the #2007.
Proposed solution
The keep
.attributesavailable for modification and avoid loosing reference to that object duringtriggerThenstage we could refetch attributesToSave through a method instead of keeping an object around.Alternatives considered
Alternatively we could be explicit about not allowing to modify
attributesdirectly by the bookshelf clients. This would mean forcing use ofset(sometimes withunsetflag) method on the model to do any modifications. This approach is rather inflexible and would require clients to modify code.