Skip to content

feat: select callback - #154

Merged
samwillis merged 2 commits into
mainfrom
samwillis/select-callback
Jun 9, 2025
Merged

samwillis merged 2 commits into
mainfrom
samwillis/select-callback

Conversation

@samwillis

Copy link
Copy Markdown
Collaborator

this add the option for select to take a callback function for generating the final row objects. It reseves an object as the context of the row, namespaced with each input collection - essentially what a multiple input (i.e. query with joins) query would return.

useLiveQuery((query) =>
  query
      .from({ collection })
      .select(({ collection }) => ({
        displayName: `${collection.name} (Age: ${collection.age})`,
        status: collection.isActive ? `Active` : `Inactive`,
        ageGroup:
          collection.age < 30
            ? `Young`
            : collection.age < 40
              ? `Middle`
              : `Senior`,
        emailDomain: collection.email.split(`@`)[1],
      })
)

Internally it is also possible to provide both the original references and new callbacks - we should leverage this undocumented until we have a new syntax replacing the original:

it(`combines callback with traditional selects`, () => {
const callback = ({ employees }: any) => ({
computed: employees.salary * 1.1,
})
const query = queryBuilder<TestSchema>()
.from(`employees`)
.select(`@id`, `@name`, callback, { department_name: `@employees.name` })

@changeset-bot

changeset-bot Bot commented Jun 9, 2025 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9d84a5d

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

This PR includes changesets to release 5 packages
Name Type
@tanstack/db Patch
@tanstack/db-collections Patch
@tanstack/react-db Patch
@tanstack/vue-db 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 9, 2025 •

Copy link
Copy Markdown

@tanstack/db-example-react-todo

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

commit: 9d84a5d

@samwillis
samwillis force-pushed the samwillis/select-callback branch from 06c224c to 9d84a5d Compare June 9, 2025 16:30
@samwillis
samwillis requested a review from KyleAMathews June 9, 2025 16:31

@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.

🔥

@samwillis
samwillis merged commit 0455e27 into main Jun 9, 2025
@samwillis
samwillis deleted the samwillis/select-callback branch June 9, 2025 16:44
@github-actions github-actions Bot mentioned this pull request Jun 9, 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.

2 participants