Skip to content

feat: new query syntax #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 92 commits into from
Jul 7, 2025
Merged

feat: new query syntax #185

merged 92 commits into from
Jul 7, 2025

Conversation

samwillis
Copy link
Collaborator

@samwillis samwillis commented Jun 18, 2025

Implements the new query syntax from #170

todo:

  • new intermedia representation
  • query builder that outputs the intermedia representation
  • compiler that compiles the ir to a D2 pipeline
  • test - and it works...

features:

  • from collection
  • from subquery
  • where
  • join collection
  • join subquery
  • groupBy
  • having
  • orderBy (with limit+offset)
  • return type of a query fully derived from query
  • React useLiveQuery
  • Vue useLiveQuery

The main entry point is a new createLiveQueryCollection function:

createLiveQueryCollection((q) =>
  q.from({ user: usersCollection }).select(({ user }) => ({
    id: user.id,
    name: user.name,
    age: user.age,
    email: user.email,
    active: user.active,
  }))
)

this wraps a new liveQueryCollectionOptions and createCollection call - the former of which can be used like the other xxxCollectionOptions function we have:

createCollection(liveQueryCollectionOptions({
  id: 'name-for-collection`,
  query: (q) =>
    q.from({ user: usersCollection }).select(({ user }) => ({
      id: user.id,
      name: user.name,
      age: user.age,
      email: user.email,
      active: user.active,
    })),
  // any other options
}))

createLiveQueryCollection can be called with either a query builder function, or a config object that takes the query param as a query builder function.

Copy link

changeset-bot bot commented Jun 18, 2025

⚠️ No Changeset found

Latest commit: 8b3086a

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

Copy link
Contributor

github-actions bot commented Jun 18, 2025

Size Change: +1.11 kB (+3.8%)

Total Size: 30.3 kB

Filename Size Change
./packages/db/dist/esm/collection.js 7.86 kB -13 B (-0.17%)
./packages/db/dist/esm/index.js 563 B +139 B (+32.78%) 🚨
./packages/db/dist/esm/query/compiled-query.js 0 B -1.49 kB (removed) 🏆
./packages/db/dist/esm/query/evaluators.js 0 B -1.06 kB (removed) 🏆
./packages/db/dist/esm/query/extractors.js 0 B -870 B (removed) 🏆
./packages/db/dist/esm/query/functions.js 0 B -1.28 kB (removed) 🏆
./packages/db/dist/esm/query/group-by.js 0 B -976 B (removed) 🏆
./packages/db/dist/esm/query/joins.js 0 B -1.14 kB (removed) 🏆
./packages/db/dist/esm/query/order-by.js 0 B -1.42 kB (removed) 🏆
./packages/db/dist/esm/query/pipeline-compiler.js 0 B -878 B (removed) 🏆
./packages/db/dist/esm/query/query-builder.js 0 B -2.14 kB (removed) 🏆
./packages/db/dist/esm/query/select.js 0 B -1.1 kB (removed) 🏆
./packages/db/dist/esm/query/utils.js 0 B -1.13 kB (removed) 🏆
./packages/db/dist/esm/utils.js 0 B -219 B (removed) 🏆
./packages/db/dist/esm/query/builder/functions.js 531 B +531 B (new file) 🆕
./packages/db/dist/esm/query/builder/index.js 3.4 kB +3.4 kB (new file) 🆕
./packages/db/dist/esm/query/builder/ref-proxy.js 851 B +851 B (new file) 🆕
./packages/db/dist/esm/query/compiler/evaluators.js 1.34 kB +1.34 kB (new file) 🆕
./packages/db/dist/esm/query/compiler/group-by.js 2.08 kB +2.08 kB (new file) 🆕
./packages/db/dist/esm/query/compiler/index.js 1.42 kB +1.42 kB (new file) 🆕
./packages/db/dist/esm/query/compiler/joins.js 1.15 kB +1.15 kB (new file) 🆕
./packages/db/dist/esm/query/compiler/order-by.js 933 B +933 B (new file) 🆕
./packages/db/dist/esm/query/compiler/select.js 657 B +657 B (new file) 🆕
./packages/db/dist/esm/query/ir.js 312 B +312 B (new file) 🆕
./packages/db/dist/esm/query/live-query-collection.js 2.02 kB +2.02 kB (new file) 🆕
ℹ️ View Unchanged
Filename Size
./packages/db/dist/esm/deferred.js 230 B
./packages/db/dist/esm/errors.js 150 B
./packages/db/dist/esm/optimistic-action.js 294 B
./packages/db/dist/esm/proxy.js 3.75 kB
./packages/db/dist/esm/SortedMap.js 1.24 kB
./packages/db/dist/esm/transactions.js 1.51 kB

compressed-size-action::db-package-size

Copy link
Contributor

github-actions bot commented Jun 18, 2025

Size Change: 0 B

Total Size: 561 B

ℹ️ View Unchanged
Filename Size
./packages/react-db/dist/esm/index.js 152 B
./packages/react-db/dist/esm/useLiveQuery.js 409 B

compressed-size-action::react-db-package-size

Copy link

pkg-pr-new bot commented Jun 18, 2025

@tanstack/db-example-react-todo

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

commit: 8b3086a

@samwillis
Copy link
Collaborator Author

@samwillis samwillis mentioned this pull request Jun 30, 2025
@samwillis
Copy link
Collaborator Author

I've renamed isIn to inArray

@samwillis
Copy link
Collaborator Author

I had a similar problem a while ago, this might be helpful

Thanks @DawidWraga, I had a good go at trying to use this pattern (the & Simplify<{}>), but sadly haven't managed to make it minimise the types. Your Simplify is almost identical to a Prettify I already had in a few places - it's a great trick.

Copy link
Contributor

@kevin-dp kevin-dp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What a tremendous job!
I love how the queries look now compared to how they were before. Awesome! 🔥

@samwillis samwillis merged commit e1e37c0 into main Jul 7, 2025
4 checks passed
@samwillis samwillis deleted the query2 branch July 7, 2025 13:46
@github-project-automation github-project-automation bot moved this from In Progress to Done in 0.1.0 release Jul 7, 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
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants