data icon indicating copy to clipboard operation
data copied to clipboard

Mutated data using setAttr or saving the record gives an error

Open Jopie01 opened this issue 2 years ago • 8 comments

I'm working now on getting mutating data working. When I directly change the record itself (which is an Object) everything goes well. But when I try to save the changes by this.store.saveRecord(record, options) it fails with

Error: Assertion Failed: Expected Cache to have a resource entry for the identifier @lid:party.address-47 but none was found

Also mutating the record using the this.cache.setAttr(identifier, key, value) fails with the same error.

The thing is that I only want the changes to be send to the backend. But I first need to get somehow the above working. Big question is how to create that resource entry.

Jopie01 avatar Apr 11 '23 18:04 Jopie01

Ok, the cache does everything with the complete identifier. But in the example of the instantiateRecord hook from the store package the identifier is basically thrown away. Only the type and id are kept.

I now added the complete identifier to my record, used that and now it works. I also discovered that I have to use the setAttr to signal the cache that I changed something so I can get a list of changes. To be honest, I haven't tested it with relationships yet.

Jopie01 avatar Apr 11 '23 18:04 Jopie01

The question is if it is right to use setAttr for relationships? It works, my record got updated but the 'existingin thechangedAttrarray isnull` even if there is a related record.

Jopie01 avatar Apr 11 '23 20:04 Jopie01

The question is if it is right to use setAttr for relationships?

No it isn't. I used the clientDidCreate in the json-api-cache package and in particular the switch statement as an example. https://github.com/emberjs/data/blob/d0e3b78f8122cfbb6288c2ff0e80691b84cabb1c/packages/json-api/src/-private/cache.ts#L625

I'm now mutating the record with the new identifier of the related record.

Jopie01 avatar Apr 12 '23 10:04 Jopie01

you'll want to send an operation to the cache to update the graph :)

runspired avatar Apr 12 '23 16:04 runspired

you'll want to send an operation to the cache to update the graph

That's what I'm doing now indeed. Using replaceRelatedRecords or addToRelatedRecords as op will notify the cache. However I had some struggles with this. The new Ember-Data is a very delicate system. You must follow the rules strictly otherwise you will end up in trouble.

So no manually adding or changing something in the record (which is a TrackedObject), but use the different methods of the cache to do that. You can manually change the record which work perfectly in the UI, but then the cache gets confused.

BTW, I'm missing the changedRelationships to get a list of changed relations of a record / resource / ....

Jopie01 avatar Apr 12 '23 18:04 Jopie01

So no manually adding or changing something in the record (which is a TrackedObject)

right, the example was a simple read-only. For something more complex you'll generally want to create a Proxy that handles making reads tracked and translates writes to cache operations

runspired avatar Apr 12 '23 23:04 runspired

@Jopie01

BTW, I'm missing the changedRelationships to get a list of changed relations of a record / resource /

This will be in 5.3 :) #8824

runspired avatar Sep 04 '23 08:09 runspired

Thanks for the hard work! Will look into it soon.

Jopie01 avatar Sep 04 '23 12:09 Jopie01