How to integrate PowerSync's new built-in attachments (AttachmentQueue) with @tanstack/powersync-db-collection? #1563
Replies: 1 comment 1 reply
-
|
I made a small POC for how I believe TanStackDB could be used in conjunction with PowerSync's new attachment queues.
It should be possible to use a TanStackDB live query to return the
The
I think it should be fine to create a TanStackDB collection for the local only
There hasn't been an example of this before. Perhaps this new POC will fulfil that role soon :) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Context
I'm building an offline-first mobile app on
@tanstack/powersync-db-collectionand want to handle file attachments (photos, PDFs, etc.) the local-first way — file metadata syncs via PowerSync, blobs go to S3 Storage.PowerSync recently shipped a new built-in attachments API (
AttachmentQueuelives inside the core SDK now —@powersync/attachmentsis deprecated). It expects to be wired directly to aPowerSyncDatabaseinstance: you give it a watch query over an attachment-id column on one of your synced tables, and it manages a local-onlyattachmentstable plus an upload/download queue against aRemoteStorageyou implement.Docs: https://docs.powersync.com/client-sdks/advanced/attachments
The question
What's the recommended pattern for using the new
AttachmentQueuetogether with@tanstack/powersync-db-collection, so that I get the TanStack DB benefits (live queries, optimistic mutations, cross-collection joins, reactivity) for the rows that reference attachments — not just for the raw SQLite tables?Specifically:
Where should the
AttachmentQueuewatch happen? TheAttachmentQueueconstructor wants a watch query over the underlyingPowerSyncDatabaseto learn which attachment IDs are "live". If my source of truth for "which rows exist" is now a TanStack DB collection (with optimistic state layered on top of SQLite), watching the raw SQLite table means optimistic inserts of new rows-with-attachments don't trigger an upload until the row is persisted. Is that the intended behavior, or is there a way to drive the queue from a TanStack DB live query instead?Optimistic insert of an attachment-bearing row. When the user adds a row that has a local file (e.g. a capture note with a photo), I want one transaction that (a) inserts the row optimistically into the TanStack collection and (b) enqueues the file for upload. Today I'd do (a) through the collection's
insertand (b) by callingattachmentQueue.saveFile(...)directly on the PowerSync db. Is there a clean way to compose these — e.g. viaPowerSyncTransactor/createTransaction— so they share commit timing and rollback semantics?Reading attachment state from a live query. Attachment records live in the local-only
attachmentstable managed byAttachmentQueue. Is the recommended pattern to define a secondpowerSyncCollectionOptionscollection on top of that local-only table andJOINit in a TanStack DB live query, so a component can do something likenote.photo.localUrireactively? If so, are there any caveats (e.g. the queue mutating the table outside of TanStack DB's mutation pipeline, deserialization of file-state enums)?Is there an idiomatic example anywhere? The shopping list / capture demos I've seen use the collection but not the new attachment helpers. The PowerSync blog post on offline-first file attachments uses the queue against raw PowerSync. A small worked example combining the two would be incredibly useful.
Why ask here vs PowerSync
Either side could own the integration sugar, but the value of combining them — incremental live queries, optimistic UI, joins across the rows table and the attachments table — is squarely TanStack DB's surface area. Hence asking here first.
Versions
@tanstack/db: 0.6.x@tanstack/powersync-db-collection: 0.1.39@powersync/react-native: latest with built-inAttachmentQueue@powersync/op-sqliteBeta Was this translation helpful? Give feedback.
All reactions