TanStack / TanStack/db

Adding server-generated properties to transaction in Collection.onInsert etc.

Open
#456 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.9k
Forks
266
Avg merge
1d 4h
Merged PRs (30d)
55

Description

In my onInsert handler, I call a TRPC endpoint which returns the server-generated publicId of the newly created document:

onInsert: async ({ transaction }) => {
  const newDocument = transaction.mutations[0].modified;

  const data = await trpcClient.documents.create.mutate({
    data: newDocument.data,
    multiple: newDocument.data.length > 1,
    templateId: newDocument.templateId,
  });

  // `data.publicId` is available here
};

But when inserting into the collection:

const tx = documentCollection.insert({
  data: documentData,
  multiple: documentData.length > 1,
  templateId: params.templateId,
  publicId: "", // <-- should come from server
  status: "pending",
  createdAt: new Date(),
  updatedAt: new Date(),
  deletedAt: null,
  createdBy: "",
  error: null,
  version: 1,
  key: null,
});

const data = await tx.isPersisted.promise;

// How can I access the `publicId` returned by the server here?

Currently, the transaction doesn’t provide a way to pass back arbitrary data (like publicId) from the TRPC call into the persisted collection record.

Proposed solution:

It would be helpful if the transaction itself exposed a way to attach response metadata from async mutations. For example:
A method like transaction.setResponseData({...})

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Collection.onInsert handler and the transaction.isPersisted.promise flow shown in the issue, then review how the TRPC response is currently handled. Define how transaction.setResponseData could carry server-generated values into the persisted collection record, including when the data becomes available and what completion behavior should mean. Done requires a documented API and coverage for the publicId response case.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.