Open History to see the last commit
1{
2 "author": {
3 "id": "MDQ6VXNlcjgyNTQyNA==",
4 "is_bot": false,
5 "login": "whollacsek",
6 "name": "William Hollacsek"
7 },
8 "body": "## In short\n\nAn on-demand Electric collection with persistence works on the first app launch, but breaks on every launch after that. The second launch fails with:\n\n```\nSnapshot requests are not supported in full mode\n```\n\nAfter that error the collection stays in `loading`, and every live query on it fails until the local data is deleted.\n\nIt happens when the shape's rows have **tags**, which is the case when the shape's `where` has a subquery.\n\n## Versions\n\n- `@tanstack/electric-db-collection` 0.5.4. The same code is in 0.5.5 and on `main`.\n- `@electric-sql/client` (the version 0.5.4 depends on)\n- Persistence: `persistedCollectionOptions` + `createExpoSQLitePersistence` (React Native / Expo)\n\n## Setup\n\n```ts\nelectricCollectionOptions({\n syncMode: \"on-demand\",\n shapeOptions: {\n // the proxy adds a where with a subquery, for example:\n // room_id IN (SELECT ... FROM permission_cache WHERE ...)\n },\n // ...\n})\n```\n\nwrapped in `persistedCollectionOptions(...)`.\n\n## Steps\n\n1. Launch the app. A live query loads a subset. Rows arrive with tags. The library saves a resume state with `requiresTagState: true`.\n2. Close the app, then open it again (a new JS process).\n\n## What goes wrong, step by step\n\nLine numbers are from `dist/esm/electric.js` in 0.5.4.\n\n1. On the new launch, the tag state from the first launch is gone (it was in memory), so `retainsTagState` is `false`. The saved resume state says `requiresTagState: true`, so `needsFullSnapshot` becomes `true` (~line 828).\n2. Because of that, the stream is created in **full** mode: `log` is not `'changes_only'`, and the offset is `-1` (~line 872).\n3. The live query then calls `loadSubset`, and `loadSubset` still calls `stream.requestSnapshot(...)` (~line 248).\n4. The Electric client does not allow snapshots in full mode, so it throws `Snapshot requests are not supported in full mode`.\n5. The throw comes before the stream starts, so the stream never starts and the collection never becomes ready.\n\nSo the library chooses full mode, then asks for something that full mode refuses.\n\n## Expected\n\nWhen the stream is in full mode, `loadSubset` should not request a snapshot. It should wait until the full sync is ready, because the full log will contain the rows anyway.\n\n## Our workaround\n\nWe ship a small patch (about 35 lines) in `loadSubset`. When `syncMode === 'on-demand' && needsFullSnapshot`, it does not call `requestSnapshot`. It waits for the first `markReady` instead (it rejects if the initial sync calls `markError`, and resolves if the collection is aborted).\n\nWith the patch, the second launch works. One cost is left: every launch downloads the full shape again, because the tag state is not saved. Saving the tag state with the resume state may be the better fix. You know the design better than we do.\n\nWe are glad to open a PR with the patch, or share our test.\n\n## Test\n\nWe have a `node:test` test with two \"launches\". It uses the real `electricCollectionOptions` + `persistedCollectionOptions` + `createExpoSQLitePersistence` over `node:sqlite`, and a small fake Electric server that tags rows. It fails on 0.5.4 and passes with the patch. We can post it here.\n\n## Related\n\n#811 has the same error text, in progressive mode.\n",
9 "comments": [],
10 "number": 2056,
11 "state": "OPEN",
12 "title": "electric-db-collection: on-demand + persistence fails after relaunch with \"Snapshot requests are not supported in full mode\"",
13 "url": "https://github.com/TanStack/db/issues/2056"
14}