Skip to content

feat: live query where clause as a callback - #152

Merged
samwillis merged 5 commits into
mainfrom
samwillis/where-callback
Jun 9, 2025
Merged

samwillis merged 5 commits into
mainfrom
samwillis/where-callback

Conversation

@samwillis

@samwillis samwillis commented Jun 9, 2025 •

Copy link
Copy Markdown
Collaborator

this enables the use of a callback for a where and having clauses, enabling this type of query:

const { data: todos } = useLiveQuery((query) =>
  query
    .from({ todo: todoCollection })
    .where(({ todo }) => todo.completed && todo.user_id === 'xyz')
)
  • The existing where clauses syntax still work as before
  • This implementation just passes the callback thought to the D2 pipeline filter where the where filtering happens.
  • Type inference fully works in the callback, you get type hints for the based in row context
  • There is a route to inferring which sources a where clause callback uses by preflight testing the callback with a proxy. this will allow a query optimiser to optimise the position of the filtering in the pipeline (such as a filter only touching the main collection running before the join). This is work we will do in future
  • both where and having can be called multiple times, with both the original syntax and new callback, each essentially being ANDed together. This is important for future work where the original syntax (or a variant of it) is needed to query indexes before then the more expressive JS callback for further refining a query.

so say in future there was an index on completed:

const { data: todos } = useLiveQuery((query) =>
  query
    .from({ todo: todoCollection })
    .where('@todo.completed', '=', true) // or a new syntax that has the same effect
    .where(({ todo }) => todo.user_id === 'xyz')
)

@changeset-bot

changeset-bot Bot commented Jun 9, 2025

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 978f8af

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

@samwillis
samwillis requested a review from KyleAMathews June 9, 2025 15:48
@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@152
npm i https://pkg.pr.new/@tanstack/db-collections@152
npm i https://pkg.pr.new/@tanstack/react-db@152
npm i https://pkg.pr.new/@tanstack/vue-db@152

commit: 978f8af

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

nice! It'll be great having the expressivity of JS for where/having clauses.

More perhaps a question for the query syntax rewrite but I'd imagine there's use cases for OR not just AND for separate clauses?

@samwillis
samwillis merged commit 856be72 into main Jun 9, 2025
@samwillis
samwillis deleted the samwillis/where-callback branch June 9, 2025 16:16
@samwillis

Copy link
Copy Markdown
Collaborator Author

I'd imagine there's use cases for OR not just AND for separate clauses?

Yes, I think this would beed to be nested inside a where clause rather than as additional ones. Something to refine as we move forward.

@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