Skip to content
Source
Sign inSign up
TanStack/db

[PowerSync]: Replacing optimistic state after insert #900

Send to my agent
OpenNick-Motion(GitHub profile, opens in a new tab) opened this issue 7 comments
  • [ X] I've validated the bug against the latest version of DB packages

Describe the bug When using powersync-db-collection, I wish to create a model with a temporary id, awaiting that mutation, then return the server-confirmed model result from my create method - entailing "swapping" the optimistic model for the confirmed model.

However, this is not possible for two reasons:

  • await tx.isPersisted.promise resolves when the mutation resolves, not when the data has been synced via Powersync.
  • Similar to the problem described in https://github.com/TanStack/db/issues/893 - once the data is synced, there is no mechanism to replace optimistic data. So the temporary model remains alongside the confirmed.

Current Behavior

const tx = todosCollection.insert({
  id: tempId,
  name,
})

await tx.isPersisted.promise
// Problem: Resolved without confirmed data yet in client
// If there were, we could use view-key mapping to swap out optimistic data for real, described here: https://tanstack.com/db/latest/docs/guides/mutations#solution-3-maintain-a-view-key-mapping

Expected behavior There should be a way to await syncing of the confirmed data, and to swap out optimistic data for confirmed.

  1. Yes, I made sure to test on latest after seeing the linked issue.

  2. This is somewhat different AFAIK because it does not involve writeInsert (unless that's happening within the lib that I have not seen).

  3. Oh ok so it's just that powersync isn't awaiting the full sync back of the server write?

  4. And that once it does sync back, there's no swap mechanism (TMK) such as exists for writeInsert

  5. This is a current limitation of the PowerSync TanStackDB collection. We currently offer an offline-first integration where the TanStackDB collection is synced to the local PowerSync SQLite table. Uploads to the server are performed in the background, but are not tied to the TanStackDB mutations. The TanStackDB mutations resolve once the change has been observed and reported by the local SQLite table, not the server. It should be theoretically possible for us to allow mutations to await upload to the server (in some online-first mode) - I believe we should have the information we'd need for that. I believe this could be implemented as a configurable feature.

Sign in to join this conversation.