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

joins.suite.ts

376 lines12.5 KB

1/**
2 * Joins Test Suite
3 *
4 * Tests multi-collection joins with various syncMode combinations
5 */
6​
7import { describe, expect, it } from 'vitest'
8import { createLiveQueryCollection, eq, gt, isNull } from '@tanstack/db'
9import { waitFor, waitForQueryData } from '../utils/helpers'
10import type { E2ETestConfig } from '../types'
11​
12export function createJoinsTestSuite(getConfig: () => Promise<E2ETestConfig>) {
13 describe(`Joins Suite`, () => {
14 describe(`Two-Collection Joins`, () => {
15 it(`should join Users and Posts`, async () => {
16 const config = await getConfig()
17 const usersCollection = config.collections.onDemand.users
18 const postsCollection = config.collections.onDemand.posts
19​
20 const query = createLiveQueryCollection((q) =>
21 q
22 .from({ user: usersCollection })
23 .join({ post: postsCollection }, ({ user, post }) =>
24 eq(user.id, post.userId),
25 )
26 .select(({ user, post }) => ({
27 id: post.id,
28 userName: user.name,
29 postTitle: post.title,
30 })),
31 )
32​
33 await query.preload()
34 await waitForQueryData(query, { minSize: 1 })
35​
36 const results = Array.from(query.state.values())
37 expect(results.length).toBeGreaterThan(0)
38 expect(results[0]!).toHaveProperty(`userName`)
39 expect(results[0]!).toHaveProperty(`postTitle`)
40​
41 await query.cleanup()
42 })
43​
44 it(`should join with predicates on both collections`, async () => {
45 const config = await getConfig()
46 const usersCollection = config.collections.onDemand.users
47 const postsCollection = config.collections.onDemand.posts
48​
49 const query = createLiveQueryCollection((q) =>
50 q
51 .from({ user: usersCollection })
52 .where(({ user }) => eq(user.isActive, true))
53 .join({ post: postsCollection }, ({ user, post }) =>
54 eq(user.id, post.userId),
55 )
56 .where(({ post }) => gt(post.viewCount, 10))
57 .select(({ user, post }) => ({
58 id: post.id,
59 userName: user.name,
60 postTitle: post.title,
61 viewCount: post.viewCount,
62 })),
63 )
64​
65 await query.preload()
66​
67 const results = Array.from(query.state.values())
68 // Verify predicates applied
69 results.forEach((r) => {
70 expect(r.viewCount).toBeGreaterThan(10)
71 })
72​
73 await query.cleanup()
74 })
75​
76 it(
77 `should join with one eager, one on-demand`,
78 { timeout: 60000 },
79 async () => {
80 const config = await getConfig()
81 const usersEager = config.collections.eager.users
82 const postsOnDemand = config.collections.onDemand.posts
83​
84 const query = createLiveQueryCollection((q) =>
85 q
86 .from({ user: usersEager })
87 .join({ post: postsOnDemand }, ({ user, post }) =>
88 eq(user.id, post.userId),
89 )
90 .select(({ user, post }) => ({
91 id: post.id,
92 userName: user.name,
93 postTitle: post.title,
94 })),
95 )
96​
97 await query.preload()
98 // Joins with eager + on-demand collections may need more time to load data from multiple sources
99 // Use longer timeout for CI environments which can be slower
100 await waitForQueryData(query, { minSize: 1, timeout: 50000 })
101​
102 const results = Array.from(query.state.values())
103 expect(results.length).toBeGreaterThan(0)
104​
105 await query.cleanup()
106 },
107 )
108​
109 it(`should join with ordering across collections`, async () => {
110 const config = await getConfig()
111 const usersCollection = config.collections.onDemand.users
112 const postsCollection = config.collections.onDemand.posts
113​
114 const query = createLiveQueryCollection((q) =>
115 q
116 .from({ user: usersCollection })
117 .join({ post: postsCollection }, ({ user, post }) =>
118 eq(user.id, post.userId),
119 )
120 .orderBy(({ post }) => post.viewCount, `desc`)
121 .select(({ user, post }) => ({
122 id: post.id,
123 userName: user.name,
124 postTitle: post.title,
125 viewCount: post.viewCount,
126 })),
127 )
128​
129 await query.preload()
130​
131 // For joins with ordering, we need to wait for sufficient data in BOTH collections
132 // Wait for the posts collection to load enough data (not just the query results)
133 await waitFor(() => postsCollection.size >= 100, {
134 timeout: 5000,
135 interval: 50,
136 message: `Posts collection did not fully load (got ${postsCollection.size}/100)`,
137 })
138​
139 // Also wait for query to have data
140 await waitForQueryData(query, { minSize: 50, timeout: 5000 })
141​
142 const results = Array.from(query.state.values())
143 expect(results.length).toBeGreaterThan(0)
144​
145 // All results MUST have viewCount field (verifies join completed successfully)
146 expect(results.every((r) => typeof r.viewCount === `number`)).toBe(true)
147​
148 // Verify sorting by viewCount (descending)
149 for (let i = 1; i < results.length; i++) {
150 const prevCount = results[i - 1]!.viewCount
151 const currCount = results[i]!.viewCount
152 expect(prevCount!).toBeGreaterThanOrEqual(currCount!)
153 }
154​
155 await query.cleanup()
156 })
157​
158 it(`should join with pagination`, async () => {
159 const config = await getConfig()
160 const usersCollection = config.collections.onDemand.users
161 const postsCollection = config.collections.onDemand.posts
162​
163 const query = createLiveQueryCollection((q) =>
164 q
165 .from({ user: usersCollection })
166 .join({ post: postsCollection }, ({ user, post }) =>
167 eq(user.id, post.userId),
168 )
169 .orderBy(({ post }) => post.id, `asc`)
170 .limit(10)
171 .offset(5)
172 .select(({ user, post }) => ({
173 id: post.id,
174 userName: user.name,
175 postTitle: post.title,
176 })),
177 )
178​
179 await query.preload()
180​
181 expect(query.size).toBeLessThanOrEqual(10)
182​
183 await query.cleanup()
184 })
185 })
186​
187 describe(`Three-Collection Joins`, () => {
188 it(`should join Users + Posts + Comments`, async () => {
189 const config = await getConfig()
190 const { users, posts, comments } = config.collections.onDemand
191​
192 const query = createLiveQueryCollection((q) =>
193 q
194 .from({ user: users })
195 .join({ post: posts }, ({ user, post }) => eq(user.id, post.userId))
196 .join({ comment: comments }, ({ post, comment }) =>
197 eq(post.id, comment.postId),
198 )
199 .select(({ user, post, comment }) => ({
200 id: comment.id,
201 userName: user.name,
202 postTitle: post.title,
203 commentText: comment.text,
204 })),
205 )
206​
207 await query.preload()
208 await waitForQueryData(query, { minSize: 1 })
209​
210 const results = Array.from(query.state.values())
211 expect(results.length).toBeGreaterThan(0)
212 expect(results[0]!).toHaveProperty(`userName`)
213 expect(results[0]!).toHaveProperty(`postTitle`)
214 expect(results[0]!).toHaveProperty(`commentText`)
215​
216 await query.cleanup()
217 })
218​
219 it(`should handle predicates on all three collections`, async () => {
220 const config = await getConfig()
221 const { users, posts, comments } = config.collections.onDemand
222​
223 const query = createLiveQueryCollection((q) =>
224 q
225 .from({ user: users })
226 .where(({ user }) => eq(user.isActive, true))
227 .join({ post: posts }, ({ user, post }) => eq(user.id, post.userId))
228 .where(({ post }) => isNull(post.deletedAt))
229 .join({ comment: comments }, ({ post, comment }) =>
230 eq(post.id, comment.postId),
231 )
232 .where(({ comment }) => isNull(comment.deletedAt))
233 .select(({ user, post, comment }) => ({
234 id: comment.id,
235 userName: user.name,
236 postTitle: post.title,
237 commentText: comment.text,
238 })),
239 )
240​
241 await query.preload()
242 await waitForQueryData(query, { minSize: 1 })
243​
244 const results = Array.from(query.state.values())
245 // All results should match all predicates and return some rows
246 expect(results.length).toBeGreaterThan(0)
247​
248 await query.cleanup()
249 })
250​
251 it(
252 `should handle mixed syncModes in 3-way join`,
253 { timeout: 60000 },
254 async () => {
255 const config = await getConfig()
256 const usersEager = config.collections.eager.users
257 const postsOnDemand = config.collections.onDemand.posts
258 const commentsOnDemand = config.collections.onDemand.comments
259​
260 const query = createLiveQueryCollection((q) =>
261 q
262 .from({ user: usersEager })
263 .join({ post: postsOnDemand }, ({ user, post }) =>
264 eq(user.id, post.userId),
265 )
266 .join({ comment: commentsOnDemand }, ({ post, comment }) =>
267 eq(post.id, comment.postId),
268 )
269 .select(({ user, post, comment }) => ({
270 id: comment.id,
271 userName: user.name,
272 postTitle: post.title,
273 commentText: comment.text,
274 })),
275 )
276​
277 await query.preload()
278 // 3-way joins with mixed eager + on-demand collections need more time
279 // Use longer timeout for CI environments which can be slower
280 await waitForQueryData(query, { minSize: 1, timeout: 50000 })
281​
282 const results = Array.from(query.state.values())
283 expect(results.length).toBeGreaterThan(0)
284​
285 await query.cleanup()
286 },
287 )
288 })
289​
290 describe(`Predicate Pushdown in Joins`, () => {
291 it(`should push predicates to correct collections`, async () => {
292 const config = await getConfig()
293 const { users, posts } = config.collections.onDemand
294​
295 const query = createLiveQueryCollection((q) =>
296 q
297 .from({ user: users })
298 .join({ post: posts }, ({ user, post }) => eq(user.id, post.userId))
299 .where(({ post }) => gt(post.viewCount, 50))
300 .select(({ user, post }) => ({
301 id: post.id,
302 userName: user.name,
303 postTitle: post.title,
304 viewCount: post.viewCount,
305 })),
306 )
307​
308 await query.preload()
309​
310 const results = Array.from(query.state.values())
311 // Verify predicate applied
312 results.forEach((r) => {
313 expect(r.viewCount).toBeGreaterThan(50)
314 })
315​
316 await query.cleanup()
317 })
318​
319 it(`should not over-fetch in joined collections`, async () => {
320 const config = await getConfig()
321 const { users, posts } = config.collections.onDemand
322​
323 const query = createLiveQueryCollection((q) =>
324 q
325 .from({ user: users })
326 .where(({ user }) => gt(user.age, 30))
327 .join({ post: posts }, ({ user, post }) => eq(user.id, post.userId))
328 .select(({ user, post }) => ({
329 id: post.id,
330 userName: user.name,
331 userAge: user.age,
332 postTitle: post.title,
333 })),
334 )
335​
336 await query.preload()
337​
338 const results = Array.from(query.state.values())
339 // All users should be > 30
340 results.forEach((r) => {
341 expect(r.userAge).toBeGreaterThan(30)
342 })
343​
344 await query.cleanup()
345 })
346 })
347​
348 describe(`Left Joins`, () => {
349 it(`should handle left joins correctly`, async () => {
350 const config = await getConfig()
351 const { users, posts } = config.collections.onDemand
352​
353 const query = createLiveQueryCollection((q) =>
354 q
355 .from({ user: users })
356 .leftJoin({ post: posts }, ({ user, post }) =>
357 eq(user.id, post.userId),
358 )
359 .select(({ user, post }) => ({
360 id: user.id,
361 userName: user.name,
362 postTitle: post.title, // May be null for users without posts
363 })),
364 )
365​
366 await query.preload()
367 await waitForQueryData(query, { minSize: 1 })
368​
369 const results = Array.from(query.state.values())
370 expect(results.length).toBeGreaterThan(0)
371​
372 await query.cleanup()
373 })
374 })
375 })