Skip to content

localOnlyCollectionOptions collection for ephemeral local state - #204

Merged
samwillis merged 4 commits into
mainfrom
cursor/implement-localonly-collection-type-b6eb
Jul 6, 2025
Merged

samwillis merged 4 commits into
mainfrom
cursor/implement-localonly-collection-type-b6eb

Conversation

@samwillis

@samwillis samwillis commented Jun 25, 2025 •

Copy link
Copy Markdown
Collaborator

Stacked on #230 - merge that first!

This just loops back on the sync interface applying all changes immediately.

There is a failing tests, I'm suspicious it is exposing something else going on that we need to look at. (edit: fixed in #230)


A localOnly collection type was implemented in @tanstack/db-collections.

Key changes include:

  • A new file packages/db-collections/src/local-only.ts was created, exporting localOnlyCollectionOptions and related types.
  • The localOnlyCollectionOptions function now configures an in-memory collection with a "loopback" sync.
  • The createLocalOnlySync function captures the collection's internal begin, write, and commit functions.
  • onInsert, onUpdate, and onDelete handlers were introduced to localOnlyCollectionOptions. These handlers wrap user-provided callbacks, first performing a synchronous "transaction confirmation" by writing mutations back through the captured sync interface, then invoking the user's callback. This ensures optimistic changes are immediately reflected.
  • An initialData array was added to LocalOnlyCollectionConfig, allowing the collection to be pre-populated during its initial sync.
  • Type inference for getKey was refined across LocalOnlyCollectionConfig, localOnlyCollectionOptions, and createLocalOnlySync to correctly propagate specific key types (e.g., number instead of string | number).
  • A packages/db-collections/tests/local-only.test-d.ts file was added to verify type compatibility with createCollection.
  • Linting issues (e.g., unused async) were resolved.

The implementation achieves a 95% test success rate, with one remaining edge case for complex mixed sequential operations.

@changeset-bot

changeset-bot Bot commented Jun 25, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 72c3e17

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

This PR includes changesets to release 2 packages
Name Type
@tanstack/db-collections 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 Jun 25, 2025 •

Copy link
Copy Markdown

@tanstack/db-example-react-todo

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

commit: 72c3e17

@github-actions

github-actions Bot commented Jun 25, 2025 •

Copy link
Copy Markdown
Contributor

Size Change: 0 B

Total Size: 29.2 kB

ℹ️ View Unchanged
Filename Size
./packages/db/dist/esm/collection.js 7.87 kB
./packages/db/dist/esm/deferred.js 230 B
./packages/db/dist/esm/errors.js 150 B
./packages/db/dist/esm/index.js 432 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/transactions.js 1.51 kB
./packages/db/dist/esm/utils.js 219 B

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

@github-actions

github-actions Bot commented Jun 25, 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

samwillis and others added 3 commits July 6, 2025 13:53
Implement true loopback sync for local-only collections

Add initial data support for local-only collections

Refactor local-only collection with optimistic updates and custom callbacks

Checkpoint before follow-up message

Update local-only sync to support custom key types

Update local-only collection tests with extended generic types

Refactor local-only sync function signature for improved readability

Add explicit key type parameter to localOnlyCollectionOptions

Checkpoint before follow-up message

Checkpoint before follow-up message

Add localOnly collection type for in-memory collections

Add localOnly collection type for in-memory collections

Refactor local-only collection handlers to call user handlers before confirming transactions

fix changeset
@samwillis
samwillis force-pushed the cursor/implement-localonly-collection-type-b6eb branch from 36bd9c4 to 7b2d15b Compare July 6, 2025 14:03
@samwillis
samwillis changed the base branch from main to samwillis/enforce-transaction-ids July 6, 2025 14:17
@samwillis
samwillis marked this pull request as ready for review July 6, 2025 14:18
@samwillis
samwillis requested a review from KyleAMathews July 6, 2025 14:18

@KyleAMathews KyleAMathews left a comment

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.

Love it! :shipit:

Base automatically changed from samwillis/enforce-transaction-ids to main July 6, 2025 15:30
@samwillis
samwillis merged commit 0912a7c into main Jul 6, 2025
@samwillis
samwillis deleted the cursor/implement-localonly-collection-type-b6eb branch July 6, 2025 15:31
@github-actions github-actions Bot mentioned this pull request Jul 6, 2025
cursor Bot pushed a commit that referenced this pull request Jul 16, 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.

3 participants