TanStack / TanStack/db

electric-db-collection, sync breaks after change

Open
#399 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.9k
Forks
266
Avg merge
1d 4h
Merged PRs (30d)
55

Description

Hi, I'm having an issue with this collection. The setup is a pretty barebones. In this scenario:

  • There are A and B clients
  • A makes a change, B gets the update
  • A makes another (doesn't matter how many), B gets the update.
  • But if B makes a change after A made a change, then changes are not reflected on the client A.
  • This only happens with tanstack-db. If I use useShape from electric, it works normally.

My collection:

export const collectionsCollection = createCollection(
  electricCollectionOptions<Collection>({
    id: "collections",
    getKey: (row) => row.id,
    shapeOptions: {
      url: `${import.meta.env.VITE_API_URL}/api/sync/collections`,
      fetchClient: (input, init) =>
        fetch(input, {
          credentials: "include",
          ...init,
        }),
    },
    onInsert: async ({ transaction }) => {
      const { txId } = await ApiService.createCollection(
        transaction.mutations[0].modified
      );

      return { txid: txId };
    },
    onDelete: async ({ transaction }) => {
      const { txId } = await ApiService.deleteCollection(
        transaction.mutations[0].modified.id
      );

      return { txid: txId };
    },
    onUpdate: async ({ transaction }) => {
      const { txId } = await ApiService.updateCollection(
        transaction.mutations[0].modified
      );

      return { txid: txId };
    },
  })
);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two-client sequence using the shown createCollection and electricCollectionOptions configuration: make A change, let B receive it, then make B change and check A. Compare this TanStack DB path with the working useShape path; done means B's later change is reflected on client A.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.