Skip to content

fix transaction sorting order and enforce them having an id - #230

Merged
samwillis merged 3 commits into
mainfrom
samwillis/enforce-transaction-ids
Jul 6, 2025
Merged

samwillis merged 3 commits into
mainfrom
samwillis/enforce-transaction-ids

Conversation

@samwillis

Copy link
Copy Markdown
Collaborator

While working on #204 I found two bugs:

  1. direct insert/update/delete mutations where not settings an id on the transaction, this resulted in them being overwritten with further synchronous mutations as they had an id or undefined when being looked up. I have fixed that by ensuring that a transaction id will always fallback to a new uuid if not set.
    @KyleAMathews you used new Transaction() rather than createTransaction() in these mutations, this means they are not added to the global list of transactions. Is this intentional or should then use createTransaction()? If so I will fix that here as it's a very related bug, the id was enforced in createTransaction, but not in the Transaction constructor.

  2. transactions were sorted by createdAt when rebasing over the latest synced data, however if two transactions happened in the same ms they would be ordered arbitrarily. This could result in the optimistic mutations happing in the wrong order - in localOnlyCollectionOptions collection for ephemeral local state #204 this manifested as a very strange situation where half the tests would fail, but if you only ran one of the failing tests it would work... I think this is also highly likely the cause of a flaky test I have seen when running the tests locally but never seen in CI.
    To fix this I have added an additional sequenceNumber to transaction, a monotonic counter in the current session - when we sort we compare by createdAt first, then fall back to sequenceNumber when they are created at the same time. I have left createdAt as this will be useful in future when we have some level of persistence for optimistic mutations.

@samwillis
samwillis requested a review from KyleAMathews July 6, 2025 14:16
@changeset-bot

changeset-bot Bot commented Jul 6, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ef13a14

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@tanstack/db Patch
@tanstack/db-collections Patch
@tanstack/react-db Patch
@tanstack/vue-db Patch
@tanstack/db-example-react-todo Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 6, 2025 •

Copy link
Copy Markdown

@tanstack/db-example-react-todo

npm i https://pkg.pr.new/@tanstack/db@230
npm i https://pkg.pr.new/@tanstack/db-collections@230
npm i https://pkg.pr.new/@tanstack/react-db@230
npm i https://pkg.pr.new/@tanstack/vue-db@230

commit: ef13a14

@github-actions

github-actions Bot commented Jul 6, 2025 •

Copy link
Copy Markdown
Contributor

Size Change: +169 B (+0.58%)

Total Size: 29.2 kB

Filename Size Change
./packages/db/dist/esm/collection.js 7.87 kB -4 B (-0.05%)
./packages/db/dist/esm/index.js 424 B -8 B (-1.85%)
./packages/db/dist/esm/transactions.js 1.51 kB +181 B (+13.6%) ⚠️
ℹ️ View Unchanged
Filename Size
./packages/db/dist/esm/deferred.js 230 B
./packages/db/dist/esm/errors.js 150 B
./packages/db/dist/esm/optimistic-action.js 294 B
./packages/db/dist/esm/proxy.js 3.75 kB
./packages/db/dist/esm/query/compiled-query.js 1.49 kB
./packages/db/dist/esm/query/evaluators.js 1.06 kB
./packages/db/dist/esm/query/extractors.js 870 B
./packages/db/dist/esm/query/functions.js 1.28 kB
./packages/db/dist/esm/query/group-by.js 976 B
./packages/db/dist/esm/query/joins.js 1.14 kB
./packages/db/dist/esm/query/order-by.js 1.42 kB
./packages/db/dist/esm/query/pipeline-compiler.js 878 B
./packages/db/dist/esm/query/query-builder.js 2.14 kB
./packages/db/dist/esm/query/select.js 1.1 kB
./packages/db/dist/esm/query/utils.js 1.13 kB
./packages/db/dist/esm/SortedMap.js 1.24 kB
./packages/db/dist/esm/utils.js 219 B

compressed-size-action::db-package-size

@github-actions

github-actions Bot commented Jul 6, 2025 •

Copy link
Copy Markdown
Contributor

Size Change: 0 B

Total Size: 561 B

ℹ️ View Unchanged
Filename Size
./packages/react-db/dist/esm/index.js 152 B
./packages/react-db/dist/esm/useLiveQuery.js 409 B

compressed-size-action::react-db-package-size

public isPersisted: Deferred<Transaction<T, TOperation>>
public autoCommit: boolean
public createdAt: Date
public sequenceNumber: number

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

On new Transaction — we could just only export the type so people can't use it. It was definitely my mistake using new Transaction.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, I changed it to only export the type, and changed the direct mutations to use createTransaction.

@samwillis
samwillis merged commit f5cf44b into main Jul 6, 2025
@samwillis
samwillis deleted the samwillis/enforce-transaction-ids branch July 6, 2025 15:30
@github-actions github-actions Bot mentioned this pull request Jul 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants