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

BaseQueryBuilder.md

1,064 lines26.3 KB

1---
2id: BaseQueryBuilder
3title: BaseQueryBuilder
4---
5​
6Defined in: [packages/db/src/query/builder/index.ts:143](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L143)
7​
8## Type Parameters
9​
10### TContext
11​
12`TContext` *extends* [`Context`](../interfaces/Context.md) = [`Context`](../interfaces/Context.md)
13​
14## Constructors
15​
16### Constructor
17​
18```ts
19new BaseQueryBuilder<TContext>(query, owned): BaseQueryBuilder<TContext>;
20```
21​
22Defined in: [packages/db/src/query/builder/index.ts:146](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L146)
23​
24#### Parameters
25​
26##### query
27​
28`Partial`\<[`QueryIR`](../@tanstack/namespaces/IR/interfaces/QueryIR.md)\> = `{}`
29​
30##### owned
31​
32`boolean` = `false`
33​
34**`Internal`**
35​
36Whether the builder may keep `query` without copying it.
37​
38#### Returns
39​
40`BaseQueryBuilder`\<`TContext`\>
41​
42## Accessors
43​
44### fn
45​
46#### Get Signature
47​
48```ts
49get fn(): object;
50```
51​
52Defined in: [packages/db/src/query/builder/index.ts:965](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L965)
53​
54Functional variants of the query builder
55These are imperative function that are called for ery row.
56Warning: that these cannot be optimized by the query compiler, and may prevent
57some type of optimizations being possible.
58​
59##### Example
60​
61```ts
62q.fn.select((row) => ({
63 name: row.user.name.toUpperCase(),
64 age: row.user.age + 1,
65}))
66```
67​
68##### Returns
69​
70###### having()
71​
72```ts
73having(callback): QueryBuilder<TContext>;
74```
75​
76Filter grouped rows using a function that operates on each aggregated row
77Warning: This cannot be optimized by the query compiler
78​
79###### Parameters
80​
81###### callback
82​
83(`row`) => `any`
84​
85A function that receives an aggregated row (with $selected when select() was called) and returns a boolean
86​
87###### Returns
88​
89[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
90​
91A QueryBuilder with functional having filter applied
92​
93###### Example
94​
95```ts
96// Functional having (not optimized)
97query
98 .from({ posts: postsCollection })
99 .groupBy(({posts}) => posts.userId)
100 .select(({posts}) => ({ userId: posts.userId, count: count(posts.id) }))
101 .fn.having(({ $selected }) => $selected.count > 5)
102```
103​
104###### select()
105​
106```ts
107select<TFuncSelectResult>(callback): FnSelectQueryResult<TContext, TFuncSelectResult>;
108```
109​
110Select fields using a function that operates on each row
111Warning: This cannot be optimized by the query compiler
112​
113###### Type Parameters
114​
115###### TFuncSelectResult
116​
117`TFuncSelectResult`
118​
119###### Parameters
120​
121###### callback
122​
123(`row`) => `TFuncSelectResult`
124​
125A function that receives a row and returns the selected value
126​
127###### Returns
128​
129`FnSelectQueryResult`\<`TContext`, `TFuncSelectResult`\>
130​
131A QueryBuilder with functional selection applied
132​
133###### Example
134​
135```ts
136// Functional select (not optimized)
137query
138 .from({ users: usersCollection })
139 .fn.select(row => ({
140 name: row.users.name.toUpperCase(),
141 age: row.users.age + 1,
142 }))
143```
144​
145Child query builders, query expressions, and helpers such as eq(),
146toArray(), and materialize() cannot be returned from fn.select(). Use
147them as fields in select() so the compiler can add them to the query
148graph.
149​
150Compiled Collection-valued includes cannot be inputs to fn.select(),
151including nested descendants. Use toArray() or materialize() in the
152upstream select(), or do parent-only functional work before adding
153live Collection includes with select().
154​
155###### where()
156​
157```ts
158where(callback): QueryBuilder<TContext>;
159```
160​
161Filter rows using a function that operates on each row
162Warning: This cannot be optimized by the query compiler
163​
164###### Parameters
165​
166###### callback
167​
168(`row`) => `any`
169​
170A function that receives a row and returns a boolean
171​
172###### Returns
173​
174[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
175​
176A QueryBuilder with functional filtering applied
177​
178###### Example
179​
180```ts
181// Functional where (not optimized)
182query
183 .from({ users: usersCollection })
184 .fn.where(row => row.users.name.startsWith('A'))
185```
186​
187## Methods
188​
189### \_bindCollectionSources()
190​
191```ts
192_bindCollectionSources(resolveCollection): BaseQueryBuilder<TContext>;
193```
194​
195Defined in: [packages/db/src/query/builder/index.ts:1070](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L1070)
196​
197Bind this query for one consumer. Later descriptor clauses wait for another consumer.
198​
199#### Parameters
200​
201##### resolveCollection
202​
203`CollectionResolver` | `undefined`
204​
205#### Returns
206​
207`BaseQueryBuilder`\<`TContext`\>
208​
209***
210​
211### \_getQuery()
212​
213```ts
214_getQuery(): QueryIR;
215```
216​
217Defined in: [packages/db/src/query/builder/index.ts:1062](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L1062)
218​
219#### Returns
220​
221[`QueryIR`](../@tanstack/namespaces/IR/interfaces/QueryIR.md)
222​
223***
224​
225### distinct()
226​
227```ts
228distinct(): QueryBuilder<TContext>;
229```
230​
231Defined in: [packages/db/src/query/builder/index.ts:882](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L882)
232​
233Specify that the query should return distinct rows.
234Deduplicates rows based on the selected columns.
235​
236#### Returns
237​
238[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
239​
240A QueryBuilder with distinct enabled
241​
242#### Example
243​
244```ts
245// Get countries our users are from
246query
247 .from({ users: usersCollection })
248 .select(({users}) => ({ country: users.country }))
249 .distinct()
250```
251​
252***
253​
254### findOne()
255​
256```ts
257findOne(): QueryBuilder<TContext & SingleResult>;
258```
259​
260Defined in: [packages/db/src/query/builder/index.ts:902](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L902)
261​
262Specify that the query should return a single result
263​
264#### Returns
265​
266[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext` & [`SingleResult`](../type-aliases/SingleResult.md)\>
267​
268A QueryBuilder that returns the first result
269​
270#### Example
271​
272```ts
273// Get the user matching the query
274query
275 .from({ users: usersCollection })
276 .where(({users}) => eq(users.id, 1))
277 .findOne()
278```
279​
280***
281​
282### from()
283​
284```ts
285from<TSource>(source): QueryBuilder<ContextFromSource<TSource>>;
286```
287​
288Defined in: [packages/db/src/query/builder/index.ts:253](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L253)
289​
290Specify the source table or subquery for the query
291​
292#### Type Parameters
293​
294##### TSource
295​
296`TSource` *extends* [`Source`](../type-aliases/Source.md)
297​
298#### Parameters
299​
300##### source
301​
302[`SingleSource`](../type-aliases/SingleSource.md)\<`TSource`\>
303​
304An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
305​
306#### Returns
307​
308[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`ContextFromSource`](../type-aliases/ContextFromSource.md)\<`TSource`\>\>
309​
310A QueryBuilder with the specified source
311​
312#### Example
313​
314```ts
315// Query from a collection
316query.from({ users: usersCollection })
317​
318// Query from a subquery
319const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
320query.from({ activeUsers })
321```
322​
323***
324​
325### fullJoin()
326​
327```ts
328fullJoin<TSource>(source, onCallback): QueryBuilder<MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, "full">>;
329```
330​
331Defined in: [packages/db/src/query/builder/index.ts:473](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L473)
332​
333Perform a FULL JOIN with another table or subquery
334​
335#### Type Parameters
336​
337##### TSource
338​
339`TSource` *extends* [`Source`](../type-aliases/Source.md)
340​
341#### Parameters
342​
343##### source
344​
345`TSource`
346​
347An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
348​
349##### onCallback
350​
351[`JoinOnCallback`](../type-aliases/JoinOnCallback.md)\<[`MergeContextForJoinCallback`](../type-aliases/MergeContextForJoinCallback.md)\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends CollectionOptionsIdentity\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends QueryBuilder\<TContext\> ? ResultValue\<TContext\> : never \}\[K\] \}\>\>
352​
353A function that receives table references and returns the join condition
354​
355#### Returns
356​
357[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`MergeContextWithJoinType`](../type-aliases/MergeContextWithJoinType.md)\<`TContext`, [`SchemaFromSource`](../type-aliases/SchemaFromSource.md)\<`TSource`\>, `"full"`\>\>
358​
359A QueryBuilder with the full joined table available
360​
361#### Example
362​
363```ts
364// Full join users with posts
365query
366 .from({ users: usersCollection })
367 .fullJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
368```
369​
370***
371​
372### groupBy()
373​
374```ts
375groupBy(keys): QueryBuilder<TContext>;
376```
377​
378Defined in: [packages/db/src/query/builder/index.ts:797](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L797)
379​
380Group rows by one or more columns for aggregation
381​
382#### Parameters
383​
384##### keys
385​
386A function that receives table references and returns the
387 field(s) to group by. A query with no aggregate wraps it in `groupKeys()`;
388 aggregates such as `count()` bring the grouping code themselves.
389​
390[`GroupByCallback`](../type-aliases/GroupByCallback.md)\<`TContext`\> | [`GroupKeys`](../interfaces/GroupKeys.md)\<`TContext`\>
391​
392#### Returns
393​
394[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
395​
396A QueryBuilder with grouping applied (enables aggregate functions in SELECT and HAVING)
397​
398#### Example
399​
400```ts
401// Group by a single column
402query
403 .from({ posts: postsCollection })
404 .groupBy(({posts}) => posts.userId)
405 .select(({posts, count}) => ({
406 userId: posts.userId,
407 postCount: count()
408 }))
409​
410// Group by multiple columns
411query
412 .from({ sales: salesCollection })
413 .groupBy(({sales}) => [sales.region, sales.category])
414 .select(({sales, sum}) => ({
415 region: sales.region,
416 category: sales.category,
417 totalSales: sum(sales.amount)
418 }))
419​
420// Group without an aggregate
421query
422 .from({ orders: ordersCollection })
423 .groupBy(groupKeys(({orders}) => orders.status))
424 .select(({orders}) => ({ status: orders.status }))
425```
426​
427***
428​
429### having()
430​
431```ts
432having(callback): QueryBuilder<TContext>;
433```
434​
435Defined in: [packages/db/src/query/builder/index.ts:570](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L570)
436​
437Filter grouped rows based on aggregate conditions
438​
439#### Parameters
440​
441##### callback
442​
443[`WhereCallback`](../type-aliases/WhereCallback.md)\<`TContext`\>
444​
445A function that receives table references and returns an expression
446​
447#### Returns
448​
449[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
450​
451A QueryBuilder with the having condition applied
452​
453#### Example
454​
455```ts
456// Filter groups by count
457query
458 .from({ posts: postsCollection })
459 .groupBy(({posts}) => posts.userId)
460 .having(({posts}) => gt(count(posts.id), 5))
461​
462// Filter by average
463query
464 .from({ orders: ordersCollection })
465 .groupBy(({orders}) => orders.customerId)
466 .having(({orders}) => gt(avg(orders.total), 100))
467​
468// Multiple having calls are ANDed together
469query
470 .from({ orders: ordersCollection })
471 .groupBy(({orders}) => orders.customerId)
472 .having(({orders}) => gt(count(orders.id), 5))
473 .having(({orders}) => gt(avg(orders.total), 100))
474```
475​
476***
477​
478### innerJoin()
479​
480```ts
481innerJoin<TSource>(source, onCallback): QueryBuilder<MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, "inner">>;
482```
483​
484Defined in: [packages/db/src/query/builder/index.ts:447](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L447)
485​
486Perform an INNER JOIN with another table or subquery
487​
488#### Type Parameters
489​
490##### TSource
491​
492`TSource` *extends* [`Source`](../type-aliases/Source.md)
493​
494#### Parameters
495​
496##### source
497​
498`TSource`
499​
500An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
501​
502##### onCallback
503​
504[`JoinOnCallback`](../type-aliases/JoinOnCallback.md)\<[`MergeContextForJoinCallback`](../type-aliases/MergeContextForJoinCallback.md)\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends CollectionOptionsIdentity\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends QueryBuilder\<TContext\> ? ResultValue\<TContext\> : never \}\[K\] \}\>\>
505​
506A function that receives table references and returns the join condition
507​
508#### Returns
509​
510[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`MergeContextWithJoinType`](../type-aliases/MergeContextWithJoinType.md)\<`TContext`, [`SchemaFromSource`](../type-aliases/SchemaFromSource.md)\<`TSource`\>, `"inner"`\>\>
511​
512A QueryBuilder with the inner joined table available
513​
514#### Example
515​
516```ts
517// Inner join users with posts
518query
519 .from({ users: usersCollection })
520 .innerJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
521```
522​
523***
524​
525### join()
526​
527```ts
528join<TSource, TJoinType>(
529 source,
530 onCallback,
531type): QueryBuilder<MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, TJoinType>>;
532```
533​
534Defined in: [packages/db/src/query/builder/index.ts:342](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L342)
535​
536Join another table or subquery to the current query
537​
538#### Type Parameters
539​
540##### TSource
541​
542`TSource` *extends* [`Source`](../type-aliases/Source.md)
543​
544##### TJoinType
545​
546`TJoinType` *extends* `"inner"` \| `"left"` \| `"right"` \| `"full"` = `"left"`
547​
548#### Parameters
549​
550##### source
551​
552`TSource`
553​
554An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
555​
556##### onCallback
557​
558[`JoinOnCallback`](../type-aliases/JoinOnCallback.md)\<[`MergeContextForJoinCallback`](../type-aliases/MergeContextForJoinCallback.md)\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends CollectionOptionsIdentity\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends QueryBuilder\<TContext\> ? ResultValue\<TContext\> : never \}\[K\] \}\>\>
559​
560A function that receives table references and returns the join condition
561​
562##### type
563​
564`TJoinType` = `...`
565​
566The type of join: 'inner', 'left', 'right', or 'full' (defaults to 'left')
567​
568#### Returns
569​
570[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`MergeContextWithJoinType`](../type-aliases/MergeContextWithJoinType.md)\<`TContext`, [`SchemaFromSource`](../type-aliases/SchemaFromSource.md)\<`TSource`\>, `TJoinType`\>\>
571​
572A QueryBuilder with the joined table available
573​
574#### Example
575​
576```ts
577// Left join users with posts
578query
579 .from({ users: usersCollection })
580 .join({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
581​
582// Inner join with explicit type
583query
584 .from({ u: usersCollection })
585 .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId), 'inner')
586```
587​
588// Join with a subquery
589const activeUsers = query.from({ u: usersCollection }).where(({u}) => u.active)
590query
591 .from({ activeUsers })
592 .join({ p: postsCollection }, ({u, p}) => eq(u.id, p.userId))
593​
594***
595​
596### leftJoin()
597​
598```ts
599leftJoin<TSource>(source, onCallback): QueryBuilder<MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, "left">>;
600```
601​
602Defined in: [packages/db/src/query/builder/index.ts:395](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L395)
603​
604Perform a LEFT JOIN with another table or subquery
605​
606#### Type Parameters
607​
608##### TSource
609​
610`TSource` *extends* [`Source`](../type-aliases/Source.md)
611​
612#### Parameters
613​
614##### source
615​
616`TSource`
617​
618An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
619​
620##### onCallback
621​
622[`JoinOnCallback`](../type-aliases/JoinOnCallback.md)\<[`MergeContextForJoinCallback`](../type-aliases/MergeContextForJoinCallback.md)\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends CollectionOptionsIdentity\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends QueryBuilder\<TContext\> ? ResultValue\<TContext\> : never \}\[K\] \}\>\>
623​
624A function that receives table references and returns the join condition
625​
626#### Returns
627​
628[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`MergeContextWithJoinType`](../type-aliases/MergeContextWithJoinType.md)\<`TContext`, [`SchemaFromSource`](../type-aliases/SchemaFromSource.md)\<`TSource`\>, `"left"`\>\>
629​
630A QueryBuilder with the left joined table available
631​
632#### Example
633​
634```ts
635// Left join users with posts
636query
637 .from({ users: usersCollection })
638 .leftJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
639```
640​
641***
642​
643### limit()
644​
645```ts
646limit(count): QueryBuilder<TContext>;
647```
648​
649Defined in: [packages/db/src/query/builder/index.ts:837](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L837)
650​
651Limit the number of rows returned by the query
652`orderBy` is required for `limit`
653​
654#### Parameters
655​
656##### count
657​
658`number`
659​
660Maximum number of rows to return
661​
662#### Returns
663​
664[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
665​
666A QueryBuilder with the limit applied
667​
668#### Example
669​
670```ts
671// Get top 5 posts by likes
672query
673 .from({ posts: postsCollection })
674 .orderBy(({posts}) => posts.likes, 'desc')
675 .limit(5)
676```
677​
678***
679​
680### offset()
681​
682```ts
683offset(count): QueryBuilder<TContext>;
684```
685​
686Defined in: [packages/db/src/query/builder/index.ts:861](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L861)
687​
688Skip a number of rows before returning results
689`orderBy` is required for `offset`
690​
691#### Parameters
692​
693##### count
694​
695`number`
696​
697Number of rows to skip
698​
699#### Returns
700​
701[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
702​
703A QueryBuilder with the offset applied
704​
705#### Example
706​
707```ts
708// Get second page of results
709query
710 .from({ posts: postsCollection })
711 .orderBy(({posts}) => posts.createdAt, 'desc')
712 .offset(page * pageSize)
713 .limit(pageSize)
714```
715​
716***
717​
718### orderBy()
719​
720```ts
721orderBy(callback, options): QueryBuilder<TContext>;
722```
723​
724Defined in: [packages/db/src/query/builder/index.ts:704](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L704)
725​
726Sort the query results by one or more columns
727​
728#### Parameters
729​
730##### callback
731​
732[`OrderByCallback`](../type-aliases/OrderByCallback.md)\<`TContext`\>
733​
734A function that receives table references and returns the field to sort by
735​
736##### options
737​
738[`OrderByDirection`](../@tanstack/namespaces/IR/type-aliases/OrderByDirection.md) | `OrderByOptions`
739​
740#### Returns
741​
742[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
743​
744A QueryBuilder with the ordering applied
745​
746#### Example
747​
748```ts
749// Sort by a single column
750query
751 .from({ users: usersCollection })
752 .orderBy(({users}) => users.name)
753​
754// Sort descending
755query
756 .from({ users: usersCollection })
757 .orderBy(({users}) => users.createdAt, 'desc')
758​
759// Multiple sorts (chain orderBy calls)
760query
761 .from({ users: usersCollection })
762 .orderBy(({users}) => users.lastName)
763 .orderBy(({users}) => users.firstName)
764```
765​
766***
767​
768### rightJoin()
769​
770```ts
771rightJoin<TSource>(source, onCallback): QueryBuilder<MergeContextWithJoinType<TContext, SchemaFromSource<TSource>, "right">>;
772```
773​
774Defined in: [packages/db/src/query/builder/index.ts:421](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L421)
775​
776Perform a RIGHT JOIN with another table or subquery
777​
778#### Type Parameters
779​
780##### TSource
781​
782`TSource` *extends* [`Source`](../type-aliases/Source.md)
783​
784#### Parameters
785​
786##### source
787​
788`TSource`
789​
790An object with a single key-value pair where the key is the table alias and the value is a Collection or subquery
791​
792##### onCallback
793​
794[`JoinOnCallback`](../type-aliases/JoinOnCallback.md)\<[`MergeContextForJoinCallback`](../type-aliases/MergeContextForJoinCallback.md)\<`TContext`, \{ \[K in string \| number \| symbol\]: \{ \[K in string \| number \| symbol\]: TSource\[K\] extends CollectionImpl\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends CollectionOptionsIdentity\<any, any, any, any, any\> ? InferCollectionType\<any\[any\]\> : TSource\[K\] extends QueryBuilder\<TContext\> ? ResultValue\<TContext\> : never \}\[K\] \}\>\>
795​
796A function that receives table references and returns the join condition
797​
798#### Returns
799​
800[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`MergeContextWithJoinType`](../type-aliases/MergeContextWithJoinType.md)\<`TContext`, [`SchemaFromSource`](../type-aliases/SchemaFromSource.md)\<`TSource`\>, `"right"`\>\>
801​
802A QueryBuilder with the right joined table available
803​
804#### Example
805​
806```ts
807// Right join users with posts
808query
809 .from({ users: usersCollection })
810 .rightJoin({ posts: postsCollection }, ({users, posts}) => eq(users.id, posts.userId))
811```
812​
813***
814​
815### select()
816​
817#### Call Signature
818​
819```ts
820select<TSelectObject>(callback): QueryBuilder<WithResult<TContext, ResultTypeFromSelect<TSelectObject>>>;
821```
822​
823Defined in: [packages/db/src/query/builder/index.ts:636](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L636)
824​
825Select specific columns or computed values from the query
826​
827##### Type Parameters
828​
829###### TSelectObject
830​
831`TSelectObject` *extends* `SelectShape`
832​
833##### Parameters
834​
835###### callback
836​
837(`refs`) => `NonScalarSelectObject`\<`TSelectObject`\>
838​
839A function that receives table references and returns an object with selected fields or expressions
840​
841##### Returns
842​
843[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`WithResult`](../type-aliases/WithResult.md)\<`TContext`, [`ResultTypeFromSelect`](../type-aliases/ResultTypeFromSelect.md)\<`TSelectObject`\>\>\>
844​
845A QueryBuilder that returns only the selected fields
846​
847##### Example
848​
849```ts
850// Select specific columns
851query
852 .from({ users: usersCollection })
853 .select(({users}) => ({
854 name: users.name,
855 email: users.email
856 }))
857​
858// Select with computed values
859query
860 .from({ users: usersCollection })
861 .select(({users}) => ({
862 fullName: concat(users.firstName, ' ', users.lastName),
863 ageInMonths: mul(users.age, 12)
864 }))
865​
866// Select with aggregates (requires GROUP BY)
867query
868 .from({ posts: postsCollection })
869 .groupBy(({posts}) => posts.userId)
870 .select(({posts, count}) => ({
871 userId: posts.userId,
872 postCount: count(posts.id)
873 }))
874```
875​
876#### Call Signature
877​
878```ts
879select<TSelectValue>(callback): QueryBuilder<WithResult<TContext, ResultTypeFromSelectValue<TSelectValue>>>;
880```
881​
882Defined in: [packages/db/src/query/builder/index.ts:641](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L641)
883​
884Select specific columns or computed values from the query
885​
886##### Type Parameters
887​
888###### TSelectValue
889​
890`TSelectValue` *extends* `ScalarSelectValue`
891​
892##### Parameters
893​
894###### callback
895​
896(`refs`) => `WithoutBareSubquery`\<`TSelectValue`\>
897​
898A function that receives table references and returns an object with selected fields or expressions
899​
900##### Returns
901​
902[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`WithResult`](../type-aliases/WithResult.md)\<`TContext`, `ResultTypeFromSelectValue`\<`TSelectValue`\>\>\>
903​
904A QueryBuilder that returns only the selected fields
905​
906##### Example
907​
908```ts
909// Select specific columns
910query
911 .from({ users: usersCollection })
912 .select(({users}) => ({
913 name: users.name,
914 email: users.email
915 }))
916​
917// Select with computed values
918query
919 .from({ users: usersCollection })
920 .select(({users}) => ({
921 fullName: concat(users.firstName, ' ', users.lastName),
922 ageInMonths: mul(users.age, 12)
923 }))
924​
925// Select with aggregates (requires GROUP BY)
926query
927 .from({ posts: postsCollection })
928 .groupBy(({posts}) => posts.userId)
929 .select(({posts, count}) => ({
930 userId: posts.userId,
931 postCount: count(posts.id)
932 }))
933```
934​
935***
936​
937### unionAll()
938​
939#### Call Signature
940​
941```ts
942unionAll<TSource>(source): QueryBuilder<ContextFromUnionSource<TSource>>;
943```
944​
945Defined in: [packages/db/src/query/builder/index.ts:279](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L279)
946​
947Union multiple independent source streams in one query.
948​
949##### Type Parameters
950​
951###### TSource
952​
953`TSource` *extends* [`Source`](../type-aliases/Source.md)
954​
955##### Parameters
956​
957###### source
958​
959`TSource`
960​
961An object with one or more aliases mapped to collections or subqueries
962​
963##### Returns
964​
965[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`ContextFromUnionSource`](../type-aliases/ContextFromUnionSource.md)\<`TSource`\>\>
966​
967A QueryBuilder with the unioned sources available
968​
969##### Example
970​
971```ts
972query
973 .unionAll({ message: messagesCollection, toolCall: toolCallsCollection })
974 .orderBy(({ message, toolCall }) =>
975 coalesce(message.timestamp, toolCall.timestamp)
976 )
977```
978​
979#### Call Signature
980​
981```ts
982unionAll<TBranches>(...branches): QueryBuilder<ContextFromUnionBranches<TBranches>>;
983```
984​
985Defined in: [packages/db/src/query/builder/index.ts:282](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L282)
986​
987Union multiple independent source streams in one query.
988​
989##### Type Parameters
990​
991###### TBranches
992​
993`TBranches` *extends* readonly \[[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`any`\>, [`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`any`\>\]
994​
995##### Parameters
996​
997###### branches
998​
999...`TBranches`
1000​
1001##### Returns
1002​
1003[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<[`ContextFromUnionBranches`](../type-aliases/ContextFromUnionBranches.md)\<`TBranches`\>\>
1004​
1005A QueryBuilder with the unioned sources available
1006​
1007##### Example
1008​
1009```ts
1010query
1011 .unionAll({ message: messagesCollection, toolCall: toolCallsCollection })
1012 .orderBy(({ message, toolCall }) =>
1013 coalesce(message.timestamp, toolCall.timestamp)
1015```
1016​
1017***
1018​
1019### where()
1020​
1021```ts
1022where(callback): QueryBuilder<TContext>;
1023```
1024​
1025Defined in: [packages/db/src/query/builder/index.ts:512](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/index.ts#L512)
1026​
1027Filter rows based on a condition
1028​
1029#### Parameters
1030​
1031##### callback
1032​
1033[`WhereCallback`](../type-aliases/WhereCallback.md)\<`TContext`\>
1034​
1035A function that receives table references and returns an expression
1036​
1037#### Returns
1038​
1039[`QueryBuilder`](../type-aliases/QueryBuilder.md)\<`TContext`\>
1040​
1041A QueryBuilder with the where condition applied
1042​
1043#### Example
1044​
1045```ts
1046// Simple condition
1047query
1048 .from({ users: usersCollection })
1049 .where(({users}) => gt(users.age, 18))
1050​
1051// Multiple conditions
1052query
1053 .from({ users: usersCollection })
1054 .where(({users}) => and(
1055 gt(users.age, 18),
1056 eq(users.active, true)
1057 ))
1058​
1059// Multiple where calls are ANDed together
1060query
1061 .from({ users: usersCollection })
1062 .where(({users}) => gt(users.age, 18))
1063 .where(({users}) => eq(users.active, true))
1064```