-
Notifications
You must be signed in to change notification settings - Fork 159
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
fixed issue that didn't allow a user to update a document when deleting elements in an array attribute #495
fixed issue that didn't allow a user to update a document when deleting elements in an array attribute #495
Conversation
Use `symmetricDifference` instead of `difference`. - `difference` only returns values that are present in the edited attributes, but not in the originally fetched attribute vales. This always ends up returning an empty array. - I used `symmetricDifference` as it returns an intersection of `difference` from both sides and works perfectly for this use case, as the original attributes never change.
@safwanyp is attempting to deploy a commit to the appwrite Team on Vercel. A member of the Team first needs to authorize it. |
Okay I just realized there's another bug. |
Okay so I figured out the issue that prevents updating of a document when editing the elements of an array attribute. In the code it seems like I can think of 2 solutions to this:
I have only tested with the first approach as it doesn't mess with the functionality outside of the required component. |
Hi @safwanyp! Thank you for the PR 😄 I'd say doing a deepClone with JSON.parse should be enough. I'd recommend creating an utility for it in |
- Original code creates a reference copy of `$doc` into `$work` which means that editing values of `$work` changes the values in `$doc`. - Fix this behaviour by creating a deep clone of `$doc` into `$work`, so underlying values aren't cross-referenced.
Hey @TGlide, thanks for the feedback and pointers! I've just pushed a commit that creates a |
deepClone( | ||
Object.keys($doc) | ||
.filter((key) => { | ||
return ![ | ||
'$id', | ||
'$collection', | ||
'$collectionId', | ||
'$databaseId', | ||
'$createdAt', | ||
'$updatedAt' | ||
].includes(key); | ||
}) | ||
.reduce((obj, key) => { | ||
obj[key] = $doc[key]; | ||
return obj; | ||
}, {}) as Models.Document | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this function has become quite verbose.
As an alternative, we can create an initWork function, where we have better control flow.
e.g.
function initWork() {
let prohibitedKeys = ['$id', ...];
const filteredKeys = Object.keys($doc)
// So on and so forth..
}
const work = initWork()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's fair. I'll work on this 👍🏻
Instead of cramming all the steps for correct deep cloning, I split the implementation into 4 distinct steps: - define they keys to exclude - filtering the keys - reduce filtered keys into an object - returning a writable deep clone
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Use
symmetricDifference
instead ofdifference
.difference
only returns values that are present in the edited attributes, but not in the originally fetched attribute vales. This always ends up returning an empty array.symmetricDifference
as it returns an intersection ofdifference
from both sides and works perfectly for this use case, as the original attributes never change.What does this PR do?
This PR closes appwrite/appwrite#5476
Test Plan
difference
function worked, but realized it was being used in other places, which broke things. I used thesymmetricDifference
function as it fits in perfectly for this use case.Related PRs and Issues
Have you read the Contributing Guidelines on issues?
Yes ✅