orbitjs / orbit

Composable data framework for ambitious web applications.
https://orbitjs.com
MIT License
2.33k stars 134 forks source link

Store `__inverseRels__` not cleaned up with transform option `{ useBuffer: true }` #988

Open enspandi opened 1 year ago

enspandi commented 1 year ago

We are using

settings.defaultTransformOptions = { useBuffer: true };

as otherwise the performance of indexeddb bulk operations is very very slow.

Unfortunately, with the latest version, we see that the store for keeping track of rels is not touched at all when removing a record.


Example:

// source = indexeddbsource
source.push([
  source.transformBuilder.removeRecord({ id: '123', type: 'test' })
]);

// Result in indexeddb with `useBuffer: true`
// 1. Record '123' is removed from store `test`
// 2. All relationships in store `__inverseRels__` of record '123' are still there

// Result in indexeddb with `useBuffer: false`
// 1. Record '123' is removed from store `test`
// 2. All relationships in store `__inverseRels__` of record '123' are removed

After some digging, I think the AsyncCacheIntegrityProcessor processor creates the transforms that clean up the __inverseRels__ store with useBuffer: false.

With useBuffer: true, it seems to be the job of SyncCacheIntegrityProcessor, but somehow it fails to return the changeset to remove the inverse rels @

const changes = buffer.stopTrackingChanges();

Versions

"@orbit/indexeddb": "0.17.2",
"@orbit/indexeddb-bucket": "0.17.0",
"@orbit/jsonapi": "0.17.1",
"@orbit/records": "0.17.0",
"@orbit/serializers": "0.17.0",
enspandi commented 1 year ago

@dgeb I think the line here is missing to add the object to the _delta state, similar to what the addInverseRelationshipsSync above does:

this._delta.inverseRelationships[ri] = rels;

https://github.com/orbitjs/orbit/blob/b57cfcbaf560d32f54d7041e5b7b4c3f05f413c3/packages/%40orbit/record-cache/src/simple-record-transform-buffer.ts#L213

I patched the SimpleRecordTransformBuffer for now and it seems to work.