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

predicates.suite.ts

973 lines30.7 KB

1/**
2 * Predicates Test Suite
3 *
4 * Tests basic where clause functionality with all comparison operators
5 * across different data types.
6 */
7​
8import { describe, expect, it } from 'vitest'
9import {
10 and,
11 createLiveQueryCollection,
12 eq,
13 gt,
14 gte,
15 ilike,
16 inArray,
17 isNull,
18 like,
19 lower,
20 lt,
21 lte,
22 not,
23 or,
24} from '@tanstack/db'
25import { assertAllItemsMatch, assertCollectionSize } from '../utils/assertions'
26import { waitForQueryData } from '../utils/helpers'
27import type { E2ETestConfig } from '../types'
28​
29export function createPredicatesTestSuite(
30 getConfig: () => Promise<E2ETestConfig>,
31) {
32 describe(`Predicates Suite`, () => {
33 describe(`Equality Operators`, () => {
34 it(`should filter with eq() on string field`, async () => {
35 const config = await getConfig()
36 const usersCollection = config.collections.onDemand.users
37​
38 const query = createLiveQueryCollection((q) =>
39 q
40 .from({ user: usersCollection })
41 .where(({ user }) => eq(user.name, `Alice 0`)),
42 )
43​
44 await query.preload()
45 await waitForQueryData(query, { minSize: 1 })
46​
47 const results = Array.from(query.state.values())
48 expect(results.length).toBeGreaterThan(0)
49 expect(results.every((u) => u.name === `Alice 0`)).toBe(true)
50​
51 await query.cleanup()
52 })
53​
54 it(`should filter with eq() on number field`, async () => {
55 const config = await getConfig()
56 const usersCollection = config.collections.onDemand.users
57​
58 const query = createLiveQueryCollection((q) =>
59 q
60 .from({ user: usersCollection })
61 .where(({ user }) => eq(user.age, 25)),
62 )
63​
64 await query.preload()
65​
66 assertAllItemsMatch(query, (u) => u.age === 25)
67​
68 await query.cleanup()
69 })
70​
71 it(`should filter with eq() on boolean field`, async () => {
72 const config = await getConfig()
73 const usersCollection = config.collections.onDemand.users
74​
75 const query = createLiveQueryCollection((q) =>
76 q
77 .from({ user: usersCollection })
78 .where(({ user }) => eq(user.isActive, true)),
79 )
80​
81 await query.preload()
82 await waitForQueryData(query, { minSize: 1 })
83​
84 const results = Array.from(query.state.values())
85 expect(results.length).toBeGreaterThan(0)
86 assertAllItemsMatch(query, (u) => u.isActive === true)
87​
88 await query.cleanup()
89 })
90​
91 it(`should filter with eq() on UUID field`, async () => {
92 const config = await getConfig()
93 const usersCollection = config.collections.onDemand.users
94​
95 const testUserId = `00000000-0000-4000-8000-000000000000` // User ID for index 0
96​
97 const query = createLiveQueryCollection((q) =>
98 q
99 .from({ user: usersCollection })
100 .where(({ user }) => eq(user.id, testUserId)),
101 )
102​
103 await query.preload()
104 await waitForQueryData(query, { minSize: 1 })
105​
106 assertCollectionSize(query, 1)
107 const result = Array.from(query.state.values())[0]
108 expect(result?.id).toBe(testUserId)
109​
110 await query.cleanup()
111 })
112​
113 it(`should filter with isNull() for null values`, async () => {
114 const config = await getConfig()
115 const usersCollection = config.collections.onDemand.users
116​
117 const query = createLiveQueryCollection((q) =>
118 q
119 .from({ user: usersCollection })
120 .where(({ user }) => isNull(user.email)),
121 )
122​
123 await query.preload()
124 await waitForQueryData(query, { minSize: 1 })
125​
126 const results = Array.from(query.state.values())
127 expect(results.length).toBeGreaterThan(0)
128 assertAllItemsMatch(query, (u) => u.email === null)
129​
130 await query.cleanup()
131 })
132 })
133​
134 describe(`Inequality Operators`, () => {
135 it(`should filter with not(eq()) on string field`, async () => {
136 const config = await getConfig()
137 const usersCollection = config.collections.onDemand.users
138​
139 const query = createLiveQueryCollection((q) =>
140 q
141 .from({ user: usersCollection })
142 .where(({ user }) => not(eq(user.name, `Alice 0`))),
143 )
144​
145 await query.preload()
146 await waitForQueryData(query, { minSize: 1 })
147​
148 const results = Array.from(query.state.values())
149 expect(results.length).toBeGreaterThan(0)
150 assertAllItemsMatch(query, (u) => u.name !== `Alice 0`)
151​
152 await query.cleanup()
153 })
154​
155 it(`should filter with not(isNull()) for non-null values`, async () => {
156 const config = await getConfig()
157 const usersCollection = config.collections.onDemand.users
158​
159 const query = createLiveQueryCollection((q) =>
160 q
161 .from({ user: usersCollection })
162 .where(({ user }) => not(isNull(user.email))),
163 )
164​
165 await query.preload()
166 await waitForQueryData(query, { minSize: 1 })
167​
168 const results = Array.from(query.state.values())
169 expect(results.length).toBeGreaterThan(0)
170 assertAllItemsMatch(query, (u) => u.email !== null)
171​
172 await query.cleanup()
173 })
174 })
175​
176 describe(`Comparison Operators`, () => {
177 it(`should filter with gt() on number field`, async () => {
178 const config = await getConfig()
179 const usersCollection = config.collections.onDemand.users
180​
181 const query = createLiveQueryCollection((q) =>
182 q
183 .from({ user: usersCollection })
184 .where(({ user }) => gt(user.age, 50)),
185 )
186​
187 await query.preload()
188​
189 assertAllItemsMatch(query, (u) => u.age > 50)
190​
191 await query.cleanup()
192 })
193​
194 it(`should filter with gte() on number field`, async () => {
195 const config = await getConfig()
196 const usersCollection = config.collections.onDemand.users
197​
198 const query = createLiveQueryCollection((q) =>
199 q
200 .from({ user: usersCollection })
201 .where(({ user }) => gte(user.age, 50)),
202 )
203​
204 await query.preload()
205​
206 assertAllItemsMatch(query, (u) => u.age >= 50)
207​
208 await query.cleanup()
209 })
210​
211 it(`should filter with lt() on number field`, async () => {
212 const config = await getConfig()
213 const usersCollection = config.collections.onDemand.users
214​
215 const query = createLiveQueryCollection((q) =>
216 q
217 .from({ user: usersCollection })
218 .where(({ user }) => lt(user.age, 30)),
219 )
220​
221 await query.preload()
222​
223 assertAllItemsMatch(query, (u) => u.age < 30)
224​
225 await query.cleanup()
226 })
227​
228 it(`should filter with lte() on number field`, async () => {
229 const config = await getConfig()
230 const usersCollection = config.collections.onDemand.users
231​
232 const query = createLiveQueryCollection((q) =>
233 q
234 .from({ user: usersCollection })
235 .where(({ user }) => lte(user.age, 30)),
236 )
237​
238 await query.preload()
239​
240 assertAllItemsMatch(query, (u) => u.age <= 30)
241​
242 await query.cleanup()
243 })
244​
245 it(`should filter with gt() on viewCount field`, async () => {
246 const config = await getConfig()
247 const postsCollection = config.collections.onDemand.posts
248​
249 const query = createLiveQueryCollection((q) =>
250 q
251 .from({ post: postsCollection })
252 .where(({ post }) => gt(post.viewCount, 100)),
253 )
254​
255 await query.preload()
256​
257 assertAllItemsMatch(query, (p) => p.viewCount > 100)
258​
259 await query.cleanup()
260 })
261​
262 it(`should filter with eq() on BIGINT field using JavaScript BigInt`, async () => {
263 const config = await getConfig()
264 const postsCollection = config.collections.onDemand.posts
265​
266 // Target the first post which has largeViewCount = 9007199254740992n (MAX_SAFE_INTEGER + 1)
267 const targetBigInt = BigInt(`9007199254740992`)
268​
269 const query = createLiveQueryCollection((q) =>
270 q
271 .from({ post: postsCollection })
272 .where(({ post }) => eq(post.largeViewCount, targetBigInt)),
273 )
274​
275 await query.preload()
276 await waitForQueryData(query, { minSize: 1 })
277​
278 const results = Array.from(query.state.values())
279 expect(results.length).toBe(1)
280 // Post 0 has largeViewCount = 9007199254740992n
281 // Database may return as bigint or string depending on driver
282 assertAllItemsMatch(query, (p) => {
283 const value = String(p.largeViewCount)
284 return value === targetBigInt.toString()
285 })
286​
287 await query.cleanup()
288 })
289​
290 it(`should filter with gt() on BIGINT field using JavaScript BigInt`, async () => {
291 const config = await getConfig()
292 const postsCollection = config.collections.onDemand.posts
293​
294 // Filter for posts with largeViewCount > 9007199254740995
295 // This should match posts 4-9 (indices 4,5,6,7,8,9 have values 9007199254740996-9007199254741001)
296 const thresholdBigInt = BigInt(`9007199254740995`)
297​
298 const query = createLiveQueryCollection((q) =>
299 q
300 .from({ post: postsCollection })
301 .where(({ post }) => gt(post.largeViewCount, thresholdBigInt)),
302 )
303​
304 await query.preload()
305 await waitForQueryData(query, { minSize: 1 })
306​
307 const results = Array.from(query.state.values())
308 expect(results.length).toBeGreaterThan(0)
309 // All results should have largeViewCount > threshold
310 assertAllItemsMatch(query, (p) => {
311 const value =
312 typeof p.largeViewCount === `bigint`
313 ? p.largeViewCount
314 : BigInt(p.largeViewCount)
315 return value > thresholdBigInt
316 })
317​
318 await query.cleanup()
319 })
320 })
321​
322 describe(`String Pattern Matching Operators`, () => {
323 it(`should filter with like() operator (case-sensitive)`, async () => {
324 const config = await getConfig()
325 const usersCollection = config.collections.onDemand.users
326​
327 const query = createLiveQueryCollection((q) =>
328 q
329 .from({ user: usersCollection })
330 .where(({ user }) => like(user.name, `Alice%`)),
331 )
332​
333 await query.preload()
334 await waitForQueryData(query, { minSize: 1 })
335​
336 const results = Array.from(query.state.values())
337 expect(results.length).toBeGreaterThan(0)
338 // Should match names starting with "Alice" (case-sensitive)
339 assertAllItemsMatch(query, (u) => u.name.startsWith(`Alice`))
340​
341 await query.cleanup()
342 })
343​
344 it(`should filter with ilike() operator (case-insensitive)`, async () => {
345 const config = await getConfig()
346 const usersCollection = config.collections.onDemand.users
347​
348 const query = createLiveQueryCollection((q) =>
349 q
350 .from({ user: usersCollection })
351 .where(({ user }) => ilike(user.name, `alice%`)),
352 )
353​
354 await query.preload()
355 await waitForQueryData(query, { minSize: 1 })
356​
357 const results = Array.from(query.state.values())
358 expect(results.length).toBeGreaterThan(0)
359 // Should match names starting with "Alice" (case-insensitive)
360 assertAllItemsMatch(query, (u) =>
361 u.name.toLowerCase().startsWith(`alice`),
362 )
363​
364 await query.cleanup()
365 })
366​
367 it(`should filter with like() with wildcard pattern (% at end)`, async () => {
368 const config = await getConfig()
369 const usersCollection = config.collections.onDemand.users
370​
371 const query = createLiveQueryCollection((q) =>
372 q
373 .from({ user: usersCollection })
374 .where(({ user }) => like(user.email, `%@example.com`)),
375 )
376​
377 await query.preload()
378 await waitForQueryData(query, { minSize: 1 })
379​
380 const results = Array.from(query.state.values())
381 expect(results.length).toBeGreaterThan(0)
382 // Should match emails ending with @example.com
383 assertAllItemsMatch(
384 query,
385 (u) => u.email?.endsWith(`@example.com`) ?? false,
386 )
387​
388 await query.cleanup()
389 })
390​
391 it(`should filter with like() with wildcard pattern (% in middle)`, async () => {
392 const config = await getConfig()
393 const usersCollection = config.collections.onDemand.users
394​
395 const query = createLiveQueryCollection((q) =>
396 q
397 .from({ user: usersCollection })
398 .where(({ user }) => like(user.email, `user%0@example.com`)),
399 )
400​
401 await query.preload()
402 await waitForQueryData(query, { minSize: 1 })
403​
404 const results = Array.from(query.state.values())
405 expect(results.length).toBeGreaterThan(0)
406 // Should match emails like user0@example.com, user10@example.com, user20@example.com, etc.
407 assertAllItemsMatch(
408 query,
409 (u) => (u.email?.match(/^user.*0@example\.com$/) ?? null) !== null,
410 )
411​
412 await query.cleanup()
413 })
414​
415 it(`should filter with like() with wildcard pattern matching newline`, async () => {
416 const config = await getConfig()
417 const usersCollection = config.collections.onDemand.users
418​
419 const query = createLiveQueryCollection((q) =>
420 q
421 .from({ user: usersCollection })
422 .where(({ user }) => like(user.name, `Ursula%`)),
423 )
424​
425 await query.preload()
426 await waitForQueryData(query, { minSize: 1 })
427​
428 const results = Array.from(query.state.values())
429 expect(results.length).toBeGreaterThan(0)
430 // should match names starting with "Ursula" even if it contains a newline character
431 assertAllItemsMatch(query, (u) => u.name.startsWith(`Ursula`))
432​
433 await query.cleanup()
434 })
435​
436 it(`should filter with like() with lower() function`, async () => {
437 const config = await getConfig()
438 const usersCollection = config.collections.onDemand.users
439​
440 const query = createLiveQueryCollection((q) =>
441 q
442 .from({ user: usersCollection })
443 .where(({ user }) => like(lower(user.name), `%alice%`)),
444 )
445​
446 await query.preload()
447 await waitForQueryData(query, { minSize: 1 })
448​
449 const results = Array.from(query.state.values())
450 expect(results.length).toBeGreaterThan(0)
451 // Should match names containing "alice" (case-insensitive via lower())
452 assertAllItemsMatch(query, (u) =>
453 u.name.toLowerCase().includes(`alice`),
454 )
455​
456 await query.cleanup()
457 })
458​
459 it(`should filter with ilike() with lower() function`, async () => {
460 const config = await getConfig()
461 const usersCollection = config.collections.onDemand.users
462​
463 const query = createLiveQueryCollection((q) =>
464 q
465 .from({ user: usersCollection })
466 .where(({ user }) => ilike(lower(user.name), `%bob%`)),
467 )
468​
469 await query.preload()
470 await waitForQueryData(query, { minSize: 1 })
471​
472 const results = Array.from(query.state.values())
473 expect(results.length).toBeGreaterThan(0)
474 // Should match names containing "bob" (case-insensitive)
475 assertAllItemsMatch(query, (u) => u.name.toLowerCase().includes(`bob`))
476​
477 await query.cleanup()
478 })
479​
480 it(`should filter with or() combining multiple like() conditions (search pattern)`, async () => {
481 const config = await getConfig()
482 const postsCollection = config.collections.onDemand.posts
483​
484 // This mimics the user's exact query pattern with multiple fields
485 // User's pattern: like(lower(offers.title), `%${searchLower}%`) OR like(lower(offers.human_id), `%${searchLower}%`)
486 const searchTerm = `Introduction`
487 const searchLower = searchTerm.toLowerCase()
488​
489 const query = createLiveQueryCollection((q) =>
490 q
491 .from({ post: postsCollection })
492 .where(({ post }) =>
493 or(
494 like(lower(post.title), `%${searchLower}%`),
495 like(lower(post.content ?? ``), `%${searchLower}%`),
496 ),
497 ),
498 )
499​
500 await query.preload()
501 await waitForQueryData(query, { minSize: 1 })
502​
503 const results = Array.from(query.state.values())
504 expect(results.length).toBeGreaterThan(0)
505 // Should match posts with title or content containing "introduction" (case-insensitive)
506 assertAllItemsMatch(
507 query,
508 (p) =>
509 p.title.toLowerCase().includes(searchLower) ||
510 (p.content?.toLowerCase().includes(searchLower) ?? false),
511 )
512​
513 await query.cleanup()
514 })
515​
516 it(`should filter with like() and orderBy`, async () => {
517 const config = await getConfig()
518 const usersCollection = config.collections.onDemand.users
519​
520 const query = createLiveQueryCollection((q) =>
521 q
522 .from({ user: usersCollection })
523 .where(({ user }) => like(lower(user.name), `%alice%`))
524 .orderBy(({ user }) => user.name, `asc`),
525 )
526​
527 await query.preload()
528 await waitForQueryData(query, { minSize: 1 })
529​
530 const results = Array.from(query.state.values())
531 expect(results.length).toBeGreaterThan(0)
532 assertAllItemsMatch(query, (u) =>
533 u.name.toLowerCase().includes(`alice`),
534 )
535​
536 // Verify ordering
537 const names = results.map((u) => u.name)
538 const sortedNames = [...names].sort((a, b) => a.localeCompare(b))
539 expect(names).toEqual(sortedNames)
540​
541 await query.cleanup()
542 })
543​
544 it(`should filter with like() and limit`, async () => {
545 const config = await getConfig()
546 const usersCollection = config.collections.onDemand.users
547​
548 const query = createLiveQueryCollection((q) =>
549 q
550 .from({ user: usersCollection })
551 .where(({ user }) => like(lower(user.name), `%alice%`))
552 .orderBy(({ user }) => user.name, `asc`) // Required when using LIMIT
553 .limit(5),
554 )
555​
556 await query.preload()
557 await waitForQueryData(query, { minSize: 1 })
558​
559 const results = Array.from(query.state.values())
560 // Should respect limit
561 expect(results.length).toBeLessThanOrEqual(5)
562 assertAllItemsMatch(query, (u) =>
563 u.name.toLowerCase().includes(`alice`),
564 )
565​
566 await query.cleanup()
567 })
568​
569 it(`should handle like() with pattern matching no records`, async () => {
570 const config = await getConfig()
571 const usersCollection = config.collections.onDemand.users
572​
573 const query = createLiveQueryCollection((q) =>
574 q
575 .from({ user: usersCollection })
576 .where(({ user }) => like(user.name, `NonExistent%`)),
577 )
578​
579 await query.preload()
580​
581 assertCollectionSize(query, 0)
582​
583 await query.cleanup()
584 })
585 })
586​
587 describe(`In Operator`, () => {
588 it(`should filter with inArray() on string array`, async () => {
589 const config = await getConfig()
590 const usersCollection = config.collections.onDemand.users
591​
592 const query = createLiveQueryCollection((q) =>
593 q
594 .from({ user: usersCollection })
595 .where(({ user }) =>
596 inArray(user.name, [`Alice 0`, `bob 1`, `Charlie 2`]),
597 ),
598 )
599​
600 await query.preload()
601​
602 const validNames = new Set([`Alice 0`, `bob 1`, `Charlie 2`])
603 assertAllItemsMatch(query, (u) => validNames.has(u.name))
604​
605 await query.cleanup()
606 })
607​
608 it(`should filter with inArray() on number array`, async () => {
609 const config = await getConfig()
610 const usersCollection = config.collections.onDemand.users
611​
612 const query = createLiveQueryCollection((q) =>
613 q
614 .from({ user: usersCollection })
615 .where(({ user }) => inArray(user.age, [25, 30, 35])),
616 )
617​
618 await query.preload()
619​
620 const validAges = new Set([25, 30, 35])
621 assertAllItemsMatch(query, (u) => validAges.has(u.age))
622​
623 await query.cleanup()
624 })
625​
626 it(`should filter with inArray() on UUID array`, async () => {
627 const config = await getConfig()
628 const usersCollection = config.collections.onDemand.users
629​
630 const userIds = [
631 `00000000-0000-4000-8000-000000000000`, // User ID for index 0
632 `00000001-0000-4000-8000-000000000001`, // User ID for index 1
633 `00000002-0000-4000-8000-000000000002`, // User ID for index 2
634 ]
635​
636 const query = createLiveQueryCollection((q) =>
637 q
638 .from({ user: usersCollection })
639 .where(({ user }) => inArray(user.id, userIds)),
640 )
641​
642 await query.preload()
643​
644 const validIds = new Set(userIds)
645 assertAllItemsMatch(query, (u) => validIds.has(u.id))
646​
647 await query.cleanup()
648 })
649​
650 it(`should handle empty inArray()`, async () => {
651 const config = await getConfig()
652 const usersCollection = config.collections.onDemand.users
653​
654 const query = createLiveQueryCollection((q) =>
655 q
656 .from({ user: usersCollection })
657 .where(({ user }) => inArray(user.id, [])),
658 )
659​
660 await query.preload()
661​
662 assertCollectionSize(query, 0)
663​
664 await query.cleanup()
665 })
666​
667 it(`should filter with inArray() on BIGINT array using JavaScript BigInt`, async () => {
668 const config = await getConfig()
669 const postsCollection = config.collections.onDemand.posts
670​
671 // Target posts 0 and 1 which have largeViewCount values:
672 // Post 0: 9007199254740992n, Post 1: 9007199254740993n
673 const targetBigInts = [
674 BigInt(`9007199254740992`),
675 BigInt(`9007199254740993`),
676 ]
677​
678 const query = createLiveQueryCollection((q) =>
679 q
680 .from({ post: postsCollection })
681 .where(({ post }) => inArray(post.largeViewCount, targetBigInts)),
682 )
683​
684 await query.preload()
685 await waitForQueryData(query, { minSize: 2 })
686​
687 const results = Array.from(query.state.values())
688 expect(results.length).toBe(2)
689​
690 // Verify both matching posts are returned
691 const targetStrings = targetBigInts.map((b) => b.toString())
692 assertAllItemsMatch(query, (p) => {
693 const value =
694 typeof p.largeViewCount === `bigint`
695 ? p.largeViewCount.toString()
696 : String(p.largeViewCount)
697 return targetStrings.includes(value)
698 })
699​
700 await query.cleanup()
701 })
702 })
703​
704 describe(`Null Operators`, () => {
705 it(`should filter with isNull() on nullable field`, async () => {
706 const config = await getConfig()
707 const usersCollection = config.collections.onDemand.users
708​
709 const query = createLiveQueryCollection((q) =>
710 q
711 .from({ user: usersCollection })
712 .where(({ user }) => isNull(user.email)),
713 )
714​
715 await query.preload()
716 await waitForQueryData(query, { minSize: 1 })
717​
718 const results = Array.from(query.state.values())
719 expect(results.length).toBeGreaterThan(0)
720 assertAllItemsMatch(query, (u) => u.email === null)
721​
722 await query.cleanup()
723 })
724​
725 it(`should filter with not(isNull()) on nullable field`, async () => {
726 const config = await getConfig()
727 const usersCollection = config.collections.onDemand.users
728​
729 const query = createLiveQueryCollection((q) =>
730 q
731 .from({ user: usersCollection })
732 .where(({ user }) => not(isNull(user.email))),
733 )
734​
735 await query.preload()
736 await waitForQueryData(query, { minSize: 1 })
737​
738 const results = Array.from(query.state.values())
739 expect(results.length).toBeGreaterThan(0)
740 assertAllItemsMatch(query, (u) => u.email !== null)
741​
742 await query.cleanup()
743 })
744​
745 it(`should filter with isNull() on deletedAt (soft delete pattern)`, async () => {
746 const config = await getConfig()
747 const usersCollection = config.collections.onDemand.users
748​
749 const query = createLiveQueryCollection((q) =>
750 q
751 .from({ user: usersCollection })
752 .where(({ user }) => isNull(user.deletedAt)),
753 )
754​
755 await query.preload()
756 await waitForQueryData(query, { minSize: 1 })
757​
758 const results = Array.from(query.state.values())
759 expect(results.length).toBeGreaterThan(0)
760 assertAllItemsMatch(query, (u) => u.deletedAt === null)
761​
762 await query.cleanup()
763 })
764 })
765​
766 describe(`Boolean Logic`, () => {
767 it(`should combine predicates with and()`, async () => {
768 const config = await getConfig()
769 const usersCollection = config.collections.onDemand.users
770​
771 const query = createLiveQueryCollection((q) =>
772 q
773 .from({ user: usersCollection })
774 .where(({ user }) =>
775 and(gt(user.age, 25), eq(user.isActive, true)),
776 ),
777 )
778​
779 await query.preload()
780​
781 assertAllItemsMatch(query, (u) => u.age > 25 && u.isActive === true)
782​
783 await query.cleanup()
784 })
785​
786 it(`should combine predicates with or()`, async () => {
787 const config = await getConfig()
788 const usersCollection = config.collections.onDemand.users
789​
790 const query = createLiveQueryCollection((q) =>
791 q
792 .from({ user: usersCollection })
793 .where(({ user }) => or(eq(user.age, 25), eq(user.age, 30))),
794 )
795​
796 await query.preload()
797​
798 assertAllItemsMatch(query, (u) => u.age === 25 || u.age === 30)
799​
800 await query.cleanup()
801 })
802​
803 it(`should handle complex nested logic`, async () => {
804 const config = await getConfig()
805 const usersCollection = config.collections.onDemand.users
806​
807 const query = createLiveQueryCollection((q) =>
808 q
809 .from({ user: usersCollection })
810 .where(({ user }) =>
811 and(
812 or(eq(user.age, 25), eq(user.age, 30)),
813 eq(user.isActive, true),
814 ),
815 ),
816 )
817​
818 await query.preload()
819​
820 assertAllItemsMatch(
821 query,
822 (u) => (u.age === 25 || u.age === 30) && u.isActive === true,
823 )
824​
825 await query.cleanup()
826 })
827​
828 it(`should handle NOT operator`, async () => {
829 const config = await getConfig()
830 const usersCollection = config.collections.onDemand.users
831​
832 const query = createLiveQueryCollection((q) =>
833 q
834 .from({ user: usersCollection })
835 .where(({ user }) => not(eq(user.isActive, true))),
836 )
837​
838 await query.preload()
839​
840 assertAllItemsMatch(query, (u) => u.isActive !== true)
841​
842 await query.cleanup()
843 })
844 })
845​
846 describe(`Predicate Pushdown Verification`, () => {
847 it(`should only load data matching predicate (no over-fetching)`, async () => {
848 const config = await getConfig()
849 const usersCollection = config.collections.onDemand.users
850​
851 const query = createLiveQueryCollection((q) =>
852 q
853 .from({ user: usersCollection })
854 .where(({ user }) => eq(user.age, 25)),
855 )
856​
857 await query.preload()
858 await waitForQueryData(query, { minSize: 1 })
859​
860 // Verify that the underlying collection didn't load ALL users
861 // In on-demand mode, it should only load age=25 users
862 const results = Array.from(query.state.values())
863 expect(results.length).toBeGreaterThan(0)
864 expect(results.length).toBeLessThan(100) // Shouldn't load all 100 users
865​
866 await query.cleanup()
867 })
868​
869 it(`should not load deleted records when filtering them out`, async () => {
870 const config = await getConfig()
871 const usersCollection = config.collections.onDemand.users
872​
873 const query = createLiveQueryCollection((q) =>
874 q
875 .from({ user: usersCollection })
876 .where(({ user }) => isNull(user.deletedAt)),
877 )
878​
879 await query.preload()
880 await waitForQueryData(query, { minSize: 1 })
881​
882 const results = Array.from(query.state.values())
883 expect(results.length).toBeGreaterThan(0)
884 assertAllItemsMatch(query, (u) => u.deletedAt === null)
885​
886 await query.cleanup()
887 })
888 })
889​
890 describe(`Multiple where() Calls`, () => {
891 it(`should AND multiple where() calls together`, async () => {
892 const config = await getConfig()
893 const usersCollection = config.collections.onDemand.users
894​
895 const query = createLiveQueryCollection((q) =>
896 q
897 .from({ user: usersCollection })
898 .where(({ user }) => gt(user.age, 25))
899 .where(({ user }) => eq(user.isActive, true)),
900 )
901​
902 await query.preload()
903​
904 assertAllItemsMatch(query, (u) => u.age > 25 && u.isActive === true)
905​
906 await query.cleanup()
907 })
908 })
909​
910 describe(`Edge Cases`, () => {
911 it(`should handle query with no where clause on on-demand collection`, async () => {
912 // NOTE: Electric has a bug where empty subset requests don't load data
913 // We work around this by injecting "true = true" when there's no where clause
914 // This is always true so doesn't filter data, just tricks Electric into loading
915​
916 const config = await getConfig()
917 const usersCollection = config.collections.onDemand.users
918​
919 // Query with NO where clause - loads all data
920 const query = createLiveQueryCollection(
921 (q) => q.from({ user: usersCollection }),
922 // No where, no limit, no orderBy
923 )
924​
925 await query.preload()
926 await waitForQueryData(query, { minSize: 50 })
927​
928 // Should load significant data (true = true workaround for Electric)
929 expect(query.size).toBeGreaterThan(0)
930 expect(query.size).toBe(usersCollection.size) // Query shows all collection data
931​
932 await query.cleanup()
933 })
934​
935 it(`should handle predicate matching no records`, async () => {
936 const config = await getConfig()
937 const usersCollection = config.collections.onDemand.users
938​
939 const query = createLiveQueryCollection((q) =>
940 q
941 .from({ user: usersCollection })
942 .where(({ user }) => eq(user.age, 999)),
943 )
944​
945 await query.preload()
946​
947 assertCollectionSize(query, 0)
948​
949 await query.cleanup()
950 })
951​
952 it(`should handle complex AND with no matches`, async () => {
953 const config = await getConfig()
954 const usersCollection = config.collections.onDemand.users
955​
956 const query = createLiveQueryCollection((q) =>
957 q.from({ user: usersCollection }).where(({ user }) =>
958 and(
959 eq(user.age, 25),
960 eq(user.age, 30), // Impossible: age can't be both 25 and 30
961 ),
962 ),
963 )
964​
965 await query.preload()
966​
967 assertCollectionSize(query, 0)
968​
969 await query.cleanup()
970 })
971 })
972 })