-
Notifications
You must be signed in to change notification settings - Fork 166
Query once API implementation #1211
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
base: main
Are you sure you want to change the base?
Conversation
Implements the queryOnce function as described in the RFC. This provides
a lightweight wrapper around createLiveQueryCollection that:
- Creates a live query collection with gcTime: 0
- Preloads the data
- Extracts results as an array (or single item for findOne)
- Automatically cleans up the collection
The queryOnce function:
- Accepts either a query function or config object
- Supports all query builder operations (where, select, join, orderBy, etc.)
- Properly handles findOne() queries returning single results
- Ensures cleanup happens even on error via try/finally
Use cases:
- AI/LLM context building
- Data export
- Background processing
- Testing
API:
```typescript
// Simple query
const users = await queryOnce((q) =>
q.from({ user: usersCollection })
.where(({ user }) => eq(user.active, true))
)
// Single result with findOne
const user = await queryOnce((q) =>
q.from({ user: usersCollection })
.where(({ user }) => eq(user.id, 1))
.findOne()
)
// Config object form
const orders = await queryOnce({
query: (q) => q.from({ order: ordersCollection }).limit(100)
})
```
|
Cursor Agent can help with this pull request. Just |
🦋 Changeset detectedLatest commit: c2ed7ad The changes in this PR will be included in the next version bump. This PR includes changesets to release 14 packages
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 |
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: +385 B (+0.42%) Total Size: 92.4 kB
ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 3.7 kB ℹ️ View Unchanged
|
Co-authored-by: sam.willis <sam.willis@gmail.com>
| const first = collection.values().next().value as | ||
| | InferResultType<TContext> | ||
| | undefined | ||
| return (first ?? undefined) as InferResultType<TContext> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of first ?? undefined here? Could first be null and hence we explicitly turn it into undefined ?
🎯 Changes
Implements the
queryOnceAPI, enabling one-shot execution of queries. This function creates a temporary live query collection, preloads data, extracts results, and automatically cleans up the collection.This is useful for scenarios where a persistent live query is not needed, such as:
It provides two overloads: a simple query function and a config object, supporting all standard query builder features including filtering, projection, joins, ordering, pagination, and
findOne().✅ Checklist
pnpm test:pr.🚀 Release Impact