Uh oh!
There was an error while loading. Please reload this page.
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. |
safwanyp
commented
Aug 8, 2023
Okay I just realized there's another bug. |
safwanyp
commented
Aug 9, 2023
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. |
TGlide
commented
Aug 9, 2023
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.
safwanyp
commented
Aug 10, 2023
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.
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.
functioninitWork(){letprohibitedKeys=['$id', ...];constfilteredKeys=Object.keys($doc)// So on and so forth..}constwork=initWork()There was a problem hiding this comment.
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
symmetricDifferenceinstead ofdifference.differenceonly 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.symmetricDifferenceas it returns an intersection ofdifferencefrom both sides and works perfectly for this use case, as the original attributes never change.What does this PR do?
This PR closesappwrite/appwrite#5476
Test Plan
differencefunction worked, but realized it was being used in other places, which broke things. I used thesymmetricDifferencefunction as it fits in perfectly for this use case.Related PRs and Issues
Have you read the Contributing Guidelines on issues?
Yes ✅