Skip to content

Commit 091f214

Browse files
committed
update docs
1 parent ce91599 commit 091f214

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

docs/collections/query-collection.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ All direct write methods are available on `collection.utils`:
521521

522522
## QueryFn and Predicate Push-Down
523523

524-
When using `syncMode: 'on-demand'`, the collection automatically pushes down query predicates (where clauses, orderBy, and limit) to your `queryFn`. This allows you to fetch only the data needed for each specific query, rather than fetching the entire dataset.
524+
When using `syncMode: 'on-demand'`, the collection automatically pushes down query predicates (where clauses, orderBy, limit, and offset) to your `queryFn`. This allows you to fetch only the data needed for each specific query, rather than fetching the entire dataset.
525525

526526
### How LoadSubsetOptions Are Passed
527527

@@ -530,9 +530,13 @@ LoadSubsetOptions are passed to your `queryFn` via the query context's `meta` pr
530530
```typescript
531531
queryFn: async (ctx) => {
532532
// Extract LoadSubsetOptions from the context
533-
const { limit, where, orderBy } = ctx.meta.loadSubsetOptions
533+
const { limit, offset, where, orderBy } = ctx.meta.loadSubsetOptions
534534

535535
// Use these to fetch only the data you need
536+
// - where: filter expression (AST)
537+
// - orderBy: sort expression (AST)
538+
// - limit: maximum number of rows
539+
// - offset: number of rows to skip (for pagination)
536540
// ...
537541
}
538542
```
@@ -572,7 +576,7 @@ const productsCollection = createCollection(
572576
syncMode: 'on-demand', // Enable predicate push-down
573577

574578
queryFn: async (ctx) => {
575-
const { limit, where, orderBy } = ctx.meta.loadSubsetOptions
579+
const { limit, offset, where, orderBy } = ctx.meta.loadSubsetOptions
576580

577581
// Parse the expressions into simple format
578582
const parsed = parseLoadSubsetOptions({ where, orderBy, limit })
@@ -605,6 +609,11 @@ const productsCollection = createCollection(
605609
params.set('limit', String(parsed.limit))
606610
}
607611

612+
// Add offset for pagination
613+
if (offset) {
614+
params.set('offset', String(offset))
615+
}
616+
608617
const response = await fetch(`/api/products?${params}`)
609618
return response.json()
610619
},
@@ -629,6 +638,7 @@ const affordableElectronics = createLiveQueryCollection({
629638

630639
// This triggers a queryFn call with:
631640
// GET /api/products?category=electronics&price_lt=100&sort=price:asc&limit=10
641+
// When paginating, offset is included: &offset=20
632642
```
633643

634644
### Custom Handlers for Complex APIs
@@ -731,10 +741,11 @@ queryFn: async (ctx) => {
731741
Convenience function that parses all LoadSubsetOptions at once. Good for simple use cases.
732742

733743
```typescript
734-
const { filters, sorts, limit } = parseLoadSubsetOptions(ctx.meta?.loadSubsetOptions)
744+
const { filters, sorts, limit, offset } = parseLoadSubsetOptions(ctx.meta?.loadSubsetOptions)
735745
// filters: [{ field: ['category'], operator: 'eq', value: 'electronics' }]
736746
// sorts: [{ field: ['price'], direction: 'asc', nulls: 'last' }]
737747
// limit: 10
748+
// offset: 20 (for pagination)
738749
```
739750

740751
#### `parseWhereExpression(expr, options)`

0 commit comments

Comments
 (0)