Open History to see the last commit
1import type { IR, LoadSubsetOptions } from '@tanstack/db'
2
3/**
4 * Serializes LoadSubsetOptions into a stable, hashable format for query keys.
5 * Includes where, orderBy, limit, and offset for pagination support.
6 * Note: cursor expressions are not serialized as they are backend-specific.
7 * @internal
8 */
9export function serializeLoadSubsetOptions(
10 options: LoadSubsetOptions | undefined,
11): string | undefined {
12 if (!options) {
13 return undefined
14 }
15
16 const result: Record<string, unknown> = {}
17
18 if (options.where) {
19 result.where = serializeExpression(options.where)
20 }
21
22 if (options.orderBy?.length) {
23 result.orderBy = options.orderBy.map((clause) => {
24 const baseOrderBy = {
25 expression: serializeExpression(clause.expression),
26 direction: clause.compareOptions.direction,
27 nulls: clause.compareOptions.nulls,
28 stringSort: clause.compareOptions.stringSort,
29 }
30
31 // Handle locale-specific options when stringSort is 'locale'
32 if (clause.compareOptions.stringSort === `locale`) {
33 return {
34 ...baseOrderBy,
35 locale: clause.compareOptions.locale,
36 localeOptions: clause.compareOptions.localeOptions,
37 }
38 }
39
40 return baseOrderBy
41 })
42 }
43
44 if (options.limit !== undefined) {
45 result.limit = options.limit
46 }
47
48 // Include offset for pagination support
49 if (options.offset !== undefined) {
50 result.offset = options.offset
51 }
52
53 return Object.keys(result).length === 0 ? undefined : JSON.stringify(result)
54}
55
56/**
57 * Recursively serializes an IR expression for stable hashing
58 * @internal
59 */
60function serializeExpression(expr: IR.BasicExpression | undefined): unknown {
61 if (!expr) {
62 return null
63 }
64
65 switch (expr.type) {
66 case `val`:
67 return {
68 type: `val`,
69 value: serializeValue(expr.value),
70 }
71 case `ref`:
72 return {
73 type: `ref`,
74 path: [...expr.path],
75 }
76 case `func`:
77 return {
78 type: `func`,
79 name: expr.name,
80 args: expr.args.map((arg) => serializeExpression(arg)),
81 }
82 default:
83 return null
84 }
85}
86
87/**
88 * Serializes special JavaScript values (undefined, NaN, Infinity, Date)
89 * @internal
90 */
91function serializeValue(value: unknown): unknown {
92 if (value === undefined) {
93 return { __type: `undefined` }
94 }
95
96 if (typeof value === `number`) {
97 if (Number.isNaN(value)) {
98 return { __type: `nan` }
99 }
100 if (value === Number.POSITIVE_INFINITY) {
101 return { __type: `infinity`, sign: 1 }
102 }
103 if (value === Number.NEGATIVE_INFINITY) {
104 return { __type: `infinity`, sign: -1 }
105 }
106 }
107
108 if (
109 value === null ||
110 typeof value === `string` ||
111 typeof value === `number` ||
112 typeof value === `boolean`
113 ) {
114 return value
115 }
116
117 if (value instanceof Date) {
118 return { __type: `date`, value: value.toJSON() }
119 }
120
121 if (Array.isArray(value)) {
122 return value.map((item) => serializeValue(item))
123 }
124
125 if (typeof value === `object`) {
126 return Object.fromEntries(
127 Object.entries(value as Record<string, unknown>).map(([key, val]) => [
128 key,
129 serializeValue(val),
130 ]),
131 )
132 }
133
134 return value
135}