Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { IR } from '@tanstack/db'
3import { compileSQLite } from '../src/sqlite-compiler'
4
5const val = <T>(value: T) => new IR.Value(value)
6// Helper to create expression nodes
7const ref = (path: Array<string>) => new IR.PropRef(path)
8const func = <T = unknown>(name: string, args: Array<IR.BasicExpression>) =>
9 new IR.Func<T>(name, args)
10
11describe(`SQLite Compiler`, () => {
12 describe(`where clause compilation`, () => {
13 it(`should compile eq operator`, () => {
14 const result = compileSQLite({
15 where: func(`eq`, [ref([`name`]), val(`test`)]),
16 })
17
18 expect(result.where).toBe(`"name" = ?`)
19 expect(result.params).toEqual([`test`])
20 })
21
22 it(`should compile gt operator`, () => {
23 const result = compileSQLite({
24 where: func(`gt`, [ref([`price`]), val(100)]),
25 })
26
27 expect(result.where).toBe(`"price" > ?`)
28 expect(result.params).toEqual([100])
29 })
30
31 it(`should compile gte operator`, () => {
32 const result = compileSQLite({
33 where: func(`gte`, [ref([`price`]), val(100)]),
34 })
35
36 expect(result.where).toBe(`"price" >= ?`)
37 expect(result.params).toEqual([100])
38 })
39
40 it(`should compile lt operator`, () => {
41 const result = compileSQLite({
42 where: func(`lt`, [ref([`price`]), val(100)]),
43 })
44
45 expect(result.where).toBe(`"price" < ?`)
46 expect(result.params).toEqual([100])
47 })
48
49 it(`should compile lte operator`, () => {
50 const result = compileSQLite({
51 where: func(`lte`, [ref([`price`]), val(100)]),
52 })
53
54 expect(result.where).toBe(`"price" <= ?`)
55 expect(result.params).toEqual([100])
56 })
57
58 it(`should compile and operator with two conditions`, () => {
59 const result = compileSQLite({
60 where: func(`and`, [
61 func(`gt`, [ref([`price`]), val(50)]),
62 func(`lt`, [ref([`price`]), val(100)]),
63 ]),
64 })
65
66 expect(result.where).toBe(`("price" > ?) AND ("price" < ?)`)
67 expect(result.params).toEqual([50, 100])
68 })
69
70 it(`should compile and operator with multiple conditions`, () => {
71 const result = compileSQLite({
72 where: func(`and`, [
73 func(`eq`, [ref([`status`]), val(`active`)]),
74 func(`gt`, [ref([`price`]), val(50)]),
75 func(`lt`, [ref([`price`]), val(100)]),
76 ]),
77 })
78
79 expect(result.where).toBe(
80 `("status" = ?) AND ("price" > ?) AND ("price" < ?)`,
81 )
82 expect(result.params).toEqual([`active`, 50, 100])
83 })
84
85 it(`should compile or operator`, () => {
86 const result = compileSQLite({
87 where: func(`or`, [
88 func(`eq`, [ref([`status`]), val(`active`)]),
89 func(`eq`, [ref([`status`]), val(`pending`)]),
90 ]),
91 })
92
93 expect(result.where).toBe(`("status" = ?) OR ("status" = ?)`)
94 expect(result.params).toEqual([`active`, `pending`])
95 })
96
97 it(`should compile isNull operator`, () => {
98 const result = compileSQLite({
99 where: func(`isNull`, [ref([`deleted_at`])]),
100 })
101
102 expect(result.where).toBe(`"deleted_at" IS NULL`)
103 expect(result.params).toEqual([])
104 })
105
106 it(`should compile not(isNull) as IS NOT NULL`, () => {
107 const result = compileSQLite({
108 where: func(`not`, [func(`isNull`, [ref([`deleted_at`])])]),
109 })
110
111 expect(result.where).toBe(`"deleted_at" IS NOT NULL`)
112 expect(result.params).toEqual([])
113 })
114
115 it(`should compile like operator`, () => {
116 const result = compileSQLite({
117 where: func(`like`, [ref([`name`]), val(`%test%`)]),
118 })
119
120 expect(result.where).toBe(`"name" LIKE ?`)
121 expect(result.params).toEqual([`%test%`])
122 })
123
124 it(`should escape quotes in column names`, () => {
125 const result = compileSQLite({
126 where: func(`eq`, [ref([`col"name`]), val(`test`)]),
127 })
128
129 expect(result.where).toBe(`"col""name" = ?`)
130 })
131
132 it(`should throw error for null values in comparison operators`, () => {
133 expect(() =>
134 compileSQLite({
135 where: func(`eq`, [ref([`name`]), val(null)]),
136 }),
137 ).toThrow(`Cannot use null/undefined with 'eq' operator`)
138 })
139
140 it(`should compile ilike operator`, () => {
141 const result = compileSQLite({
142 where: func(`ilike`, [ref([`name`]), val(`%test%`)]),
143 })
144
145 expect(result.where).toBe(`"name" LIKE ? COLLATE NOCASE`)
146 expect(result.params).toEqual([`%test%`])
147 })
148
149 it(`should compile upper function`, () => {
150 const result = compileSQLite({
151 where: func(`eq`, [func(`upper`, [ref([`name`])]), val(`TEST`)]),
152 })
153
154 expect(result.where).toBe(`UPPER("name") = ?`)
155 expect(result.params).toEqual([`TEST`])
156 })
157
158 it(`should compile lower function`, () => {
159 const result = compileSQLite({
160 where: func(`eq`, [func(`lower`, [ref([`name`])]), val(`test`)]),
161 })
162
163 expect(result.where).toBe(`LOWER("name") = ?`)
164 expect(result.params).toEqual([`test`])
165 })
166
167 it(`should compile coalesce function`, () => {
168 const result = compileSQLite({
169 where: func(`eq`, [
170 func(`coalesce`, [ref([`name`]), val(`default`)]),
171 val(`test`),
172 ]),
173 })
174
175 expect(result.where).toBe(`COALESCE("name", ?) = ?`)
176 expect(result.params).toEqual([`default`, `test`])
177 })
178
179 it(`should compile length function`, () => {
180 const result = compileSQLite({
181 where: func(`gt`, [func(`length`, [ref([`name`])]), val(5)]),
182 })
183
184 expect(result.where).toBe(`LENGTH("name") > ?`)
185 expect(result.params).toEqual([5])
186 })
187
188 it(`should compile concat function with multiple args`, () => {
189 const result = compileSQLite({
190 where: func(`eq`, [
191 func(`concat`, [ref([`first_name`]), val(` `), ref([`last_name`])]),
192 val(`John Doe`),
193 ]),
194 })
195
196 expect(result.where).toBe(`CONCAT("first_name", ?, "last_name") = ?`)
197 expect(result.params).toEqual([` `, `John Doe`])
198 })
199
200 it(`should compile add operator`, () => {
201 const result = compileSQLite({
202 where: func(`gt`, [func(`add`, [ref([`price`]), val(10)]), val(100)]),
203 })
204
205 expect(result.where).toBe(`"price" + ? > ?`)
206 expect(result.params).toEqual([10, 100])
207 })
208
209 it(`should throw for length with wrong arg count`, () => {
210 expect(() =>
211 compileSQLite({ where: func(`length`, [ref([`a`]), ref([`b`])]) }),
212 ).toThrow(`length expects 1 argument`)
213 })
214
215 it(`should throw for add with wrong arg count`, () => {
216 expect(() =>
217 compileSQLite({ where: func(`add`, [ref([`price`])]) }),
218 ).toThrow(`add expects 2 arguments`)
219 })
220
221 it(`should throw error for unsupported operators`, () => {
222 expect(() =>
223 compileSQLite({
224 where: func(`unsupported_op`, [ref([`name`]), val(`%test%`)]),
225 }),
226 ).toThrow(`Operator 'unsupported_op' is not supported`)
227 })
228 })
229
230 describe(`orderBy compilation`, () => {
231 it(`should compile simple orderBy`, () => {
232 const result = compileSQLite({
233 orderBy: [
234 {
235 expression: ref([`price`]),
236 compareOptions: { direction: `asc`, nulls: `last` },
237 },
238 ],
239 })
240
241 expect(result.orderBy).toBe(`"price" NULLS LAST`)
242 expect(result.params).toEqual([])
243 })
244
245 it(`should compile orderBy with desc direction`, () => {
246 const result = compileSQLite({
247 orderBy: [
248 {
249 expression: ref([`price`]),
250 compareOptions: { direction: `desc`, nulls: `last` },
251 },
252 ],
253 })
254
255 expect(result.orderBy).toBe(`"price" DESC NULLS LAST`)
256 })
257
258 it(`should compile orderBy with nulls first`, () => {
259 const result = compileSQLite({
260 orderBy: [
261 {
262 expression: ref([`price`]),
263 compareOptions: { direction: `asc`, nulls: `first` },
264 },
265 ],
266 })
267
268 expect(result.orderBy).toBe(`"price" NULLS FIRST`)
269 })
270
271 it(`should compile multiple orderBy clauses`, () => {
272 const result = compileSQLite({
273 orderBy: [
274 {
275 expression: ref([`category`]),
276 compareOptions: { direction: `asc`, nulls: `last` },
277 },
278 {
279 expression: ref([`price`]),
280 compareOptions: { direction: `desc`, nulls: `last` },
281 },
282 ],
283 })
284
285 expect(result.orderBy).toBe(
286 `"category" NULLS LAST, "price" DESC NULLS LAST`,
287 )
288 })
289 })
290
291 describe(`limit`, () => {
292 it(`should pass through limit`, () => {
293 const result = compileSQLite({
294 limit: 50,
295 })
296
297 expect(result.limit).toBe(50)
298 })
299 })
300
301 describe(`combined options`, () => {
302 it(`should compile where, orderBy, and limit together`, () => {
303 const result = compileSQLite({
304 where: func(`gt`, [ref([`price`]), val(100)]),
305 orderBy: [
306 {
307 expression: ref([`price`]),
308 compareOptions: { direction: `desc`, nulls: `last` },
309 },
310 ],
311 limit: 10,
312 })
313
314 expect(result.where).toBe(`"price" > ?`)
315 expect(result.orderBy).toBe(`"price" DESC NULLS LAST`)
316 expect(result.limit).toBe(10)
317 expect(result.params).toEqual([100])
318 })
319
320 it(`should handle empty options`, () => {
321 const result = compileSQLite({})
322
323 expect(result.where).toBeUndefined()
324 expect(result.orderBy).toBeUndefined()
325 expect(result.limit).toBeUndefined()
326 expect(result.params).toEqual([])
327 })
328 })
329})