-
Notifications
You must be signed in to change notification settings - Fork 53
feat(query-db-collection): add manual write methods for direct state updates #303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
🦋 Changeset detectedLatest commit: 650d993 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
- Remove unused Collection import - Fix SyncConfig type parameter issues by removing explicit type annotations - Fix queryClient.setQueryData type issues with proper casting - All build errors resolved, package builds successfully 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
More templates
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
Size Change: 0 B Total Size: 58.1 kB ℹ️ View Unchanged
|
Size Change: 0 B Total Size: 1.05 kB ℹ️ View Unchanged
|
This looks really good to me, I am excited to see this in and put it through the paces a little more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not happy with the code quality of this PR. It seems to overly complicate certain aspects, duplicate code, etc. I left some comments explaining how we can improve the quality of the code.
I have one major comment though about the implementation of the different sync methods (syncInsert
, syncUpdate
, syncUpsert
, syncDelete
, syncBatch
). The syncBatch
function repeats quite some code from the individual syncInsert
/syncUpdate
/... methods. We should be able to refactor the code such that syncBatch
simply does:
BEGIN
batch.foreach(op => dispatch(op))
COMMIT
The dispatch
function just checks the type of the operation and calls syncInsert
/syncUpdate
/syncUpsert
/syncDelete
. Currently, those functions also call BEGIN
and COMMIT
so that's why we don't do that. However this can be solved easily by introducing applyInsert
/applyUpdate
/applyUpsert
/applyDelete
methods which apply the operation (without calling BEGIN and COMMIT). The dispatch
function would then dispatch to those. And the public method would wrap it in BEGIN ... COMMIT
.
Combine that with my suggestion of a single public sync
method and you get quite an elegant implementation:
sync(op) {
BEGIN
dispatch(op)
COMMIT
}
syncBatch(ops) {
BEGIN
ops.foreach(op => dispatch(op))
COMMIT
}
- Renamed syncInsert → writeInsert - Renamed syncUpdate → writeUpdate - Renamed syncDelete → writeDelete - Renamed syncUpsert → writeUpsert - Renamed syncBatch → writeBatch The 'write' prefix better communicates that these methods directly write to collection state, bypassing the normal optimistic update flow. This follows established patterns from Apollo Client (writeQuery, writeFragment) and avoids confusion with synchronous/asynchronous operations. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
@kevin-dp give it another look please! |
Updated changeset description to use the new writeX method names instead of syncX methods. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Description
This PR adds manual write methods to
@tanstack/query-db-collection
to support direct state updates from external sources like WebSockets, server-sent events, or mutation responses.Problem
Currently, @tanstack/query-db-collection has two limitations:
Solution
Added new
write*
methods that bypass the normal optimistic update flow and directly write to collection state:writeInsert(data)
- Write insert directly to collectionwriteUpdate(data)
- Write update directly to collectionwriteDelete(keys)
- Write delete directly to collectionwriteUpsert(data)
- Write upsert directly to collectionwriteBatch(operations)
- Write batch operations directlyThe
write
prefix follows established patterns from Apollo Client (writeQuery
,writeFragment
) and clearly indicates these methods bypass the normal persistence flow.Example Usage
Implementation Details
manual-sync.ts
module for better organizationBreaking Changes
None - this is a new feature that doesn't affect existing APIs.
Testing
Added comprehensive test suite covering:
Closes #294