Skip to content

[POC] TanStackDB Attachments#983

Draft
stevensJourney wants to merge 1 commit into
mainfrom
poc-tanstackdb-attachments
Draft

[POC] TanStackDB Attachments#983
stevensJourney wants to merge 1 commit into
mainfrom
poc-tanstackdb-attachments

Conversation

@stevensJourney
Copy link
Copy Markdown
Collaborator

@stevensJourney stevensJourney commented May 29, 2026

This is a small POC for using our SDK Attachment helpers with TanStackDB collections.

The local only attachments table is kept in sync with a corresponding TanStackDB collection. This allows queries which join TanStackDB collections to the attachments state: e.g.

    q
      .from({ lists: listsCollection })
      .leftJoin({ todos: todosCollection }, ({ lists, todos }) => eq(lists.id, todos.list_id))
      .groupBy(({ lists }) => [lists.id, lists.name])
      .select(({ lists, todos }) => ({
      .leftJoin({ attachment: attachmentsCollection }, ({ lists, attachment }) => eq(lists.photo_id, attachment.id))
      .groupBy(({ lists, attachment }) => [lists.id, lists.name, attachment.local_uri])
      .select(({ lists, todos, attachment }) => ({
        id: lists.id,
        name: lists.name,
        attachment_local_uri: attachment?.local_uri,
        total_tasks: count(todos?.id),
        completed_tasks: sum(todos?.completed as number)
      }))

The standard AttachmentQueue class is extended to support a saveFile method which uses TanStackDB transactions. The mutation of creating a new attachment and associating relational data is done in a single (optimistic) TanStackDB transaction. A TanStackDB updateHook is provided which allows setting of said relationships.

In this demo, the watched attachment items - which represent the wholistic state of present attachment records, are fetched from a TanStackDB watched query.

  // Define which attachments exist in your data model
  watchAttachments: async (onUpdate, abortSignal) => {
    const livePhotoIds = createCollection(
      liveQueryCollectionOptions({
        query: (q) =>
          q
            .from({ document: listsCollection })
            .where(({ document }) => not(isNull(document.photo_id)))
            .select(({ document }) => ({
              photo_id: document.photo_id
            }))
      })
    );
//...
}

AI usage disclaimer: I tried using Codex for this, but it was doing a terrible job - so I did it myself. Codex did fix a few typos though.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 29, 2026

⚠️ No Changeset found

Latest commit: 58c15a2

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

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.

1 participant