Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

pagination-demand.ts

90 lines3.3 KB

1import type { IR, LoadSubsetOptions } from '@tanstack/db'
2import type { ConvexQueryOrder } from './paginated-options'
3​
4/** Whether a contiguous, complete provider prefix establishes this exact
5 * request. No Convex cursor is decoded and no unseen suffix is consulted. */
6export function paginationDemand<TRow>(
7 request: LoadSubsetOptions,
8 order: ConvexQueryOrder<TRow>,
9): (rows: ReadonlyArray<TRow>, exhausted: boolean) => boolean {
10 const unsupported = (): never => {
11 throw new TypeError(
12 `Convex on-demand paging requires the declared numeric query order and supports only its prefix, boundary tie and continuation requests.`,
13 )
14 }
15 const { limit, offset = 0, where, cursor, orderBy } = request
16 for (const count of [limit, offset]) {
17 if (count !== undefined && (!Number.isSafeInteger(count) || count < 0))
18 throw new RangeError(
19 `Convex subset offset and limit must be nonnegative safe integers.`,
20 )
21 }
22 const leading = orderBy?.[0]
23 if (
24 leading &&
25 (leading.expression.type !== `ref` ||
26 leading.expression.path.length !== 1 ||
27 leading.expression.path[0] !== order.field ||
28 leading.compareOptions.direction !== order.direction)
29 )
30 unsupported()
31 const comparison = (
32 expression: IR.BasicExpression<boolean>,
33 name: string,
34 ): number => {
35 if (
36 expression.type !== `func` ||
37 expression.name !== name ||
38 expression.args.length !== 2
39 )
40 return unsupported()
41 const [field, value] = expression.args
42 if (
43 field?.type !== `ref` ||
44 field.path.length !== 1 ||
45 field.path[0] !== order.field ||
46 value?.type !== `val` ||
47 typeof value.value !== `number` ||
48 !Number.isFinite(value.value)
49 )
50 return unsupported()
51 return value.value
52 }
53 const direction = order.direction === `asc` ? 1 : -1
54 // Main where is a scoped tie (emitted by core separately), never a general
55 // filter translated into a Convex query. Cursor expressions stay separate.
56 const equal = where === undefined ? undefined : comparison(where, `eq`)
57 let after: number | undefined
58 if (cursor) {
59 const boundary = comparison(cursor.whereFrom, direction === 1 ? `gt` : `lt`)
60 if (comparison(cursor.whereCurrent, `eq`) !== boundary) unsupported()
61 // Core permits offset encoding instead of cursor encoding. A provided
62 // absolute offset takes precedence; cursor-only callers keep their scope.
63 if (request.offset === undefined) after = boundary
64 }
65 return (rows, exhausted) => {
66 if (limit === 0) return true
67 let count = 0
68 let boundary: number | undefined
69 let last: number | undefined
70 for (const row of rows) {
71 const value = row[order.field]
72 if (typeof value !== `number` || !Number.isFinite(value))
73 return unsupported()
74 if (last !== undefined && direction * (value - last) < 0)
75 return unsupported()
76 last = value
77 if (equal !== undefined && direction * (value - equal) > 0) return true
78 if (
79 (equal === undefined || value === equal) &&
80 (after === undefined || direction * (value - after) > 0)
81 ) {
82 count++
83 if (limit !== undefined && count === offset + limit) boundary = value
84 }
85 if (boundary !== undefined && direction * (value - boundary) > 0)
86 return true
87 }
88 return exhausted
89 }