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

caseWhen.md

1,319 lines20.6 KB

1---
2id: caseWhen
3title: caseWhen
4---
5​
6## Call Signature
7​
8```ts
9function caseWhen<C1, V1>(condition1, value1): CaseWhenResult<[V1], false>;
10```
11​
12Defined in: [packages/db/src/query/builder/functions.ts:407](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L407)
13​
14Returns the value for the first matching condition, similar to SQL
15`CASE WHEN`.
16​
17Arguments are evaluated as condition/value pairs followed by an optional
18default value. Scalar branch values return a query expression and can be used
19in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
20`having`, and equality join operands. If no scalar branch matches and no
21default is provided, the result is `null`.
22​
23When a branch value is a projection object, `caseWhen` becomes a select-only
24projection value. Projection branches can include nested fields, ref spreads,
25and includes. If no projection branch matches and no default is provided, the
26result is `undefined`.
27​
28### Type Parameters
29​
30#### C1
31​
32`C1` *extends* `ExpressionLike`
33​
34#### V1
35​
36`V1` *extends* `CaseWhenValue`
37​
38### Parameters
39​
40#### condition1
41​
42`C1`
43​
44#### value1
45​
46`V1`
47​
48### Returns
49​
50`CaseWhenResult`\<\[`V1`\], `false`\>
51​
52### Examples
53​
54```ts
55caseWhen(gt(user.age, 18), `adult`, `minor`)
56```
57​
58```ts
59caseWhen(
60 gt(user.age, 65),
61 `senior`,
62 gt(user.age, 18),
63 `adult`,
64 `minor`,
66```
67​
68```ts
69caseWhen(gt(user.age, 18), {
70 ...user,
71 posts: include(
72 q
73 .from({ post: postsCollection })
74 .where(({ post }) => eq(post.userId, user.id)),
75 ),
76})
77```
78​
79## Call Signature
80​
81```ts
82function caseWhen<C1, V1, D>(
83 condition1,
84 value1,
85defaultValue): CaseWhenResult<[V1, D], true>;
86```
87​
88Defined in: [packages/db/src/query/builder/functions.ts:411](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L411)
89​
90Returns the value for the first matching condition, similar to SQL
91`CASE WHEN`.
92​
93Arguments are evaluated as condition/value pairs followed by an optional
94default value. Scalar branch values return a query expression and can be used
95in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
96`having`, and equality join operands. If no scalar branch matches and no
97default is provided, the result is `null`.
98​
99When a branch value is a projection object, `caseWhen` becomes a select-only
100projection value. Projection branches can include nested fields, ref spreads,
101and includes. If no projection branch matches and no default is provided, the
102result is `undefined`.
103​
104### Type Parameters
105​
106#### C1
107​
108`C1` *extends* `ExpressionLike`
109​
110#### V1
111​
112`V1` *extends* `CaseWhenValue`
113​
114#### D
115​
116`D` *extends* `CaseWhenValue`
117​
118### Parameters
119​
120#### condition1
121​
122`C1`
123​
124#### value1
125​
126`V1`
127​
128#### defaultValue
129​
130`D`
131​
132### Returns
133​
134`CaseWhenResult`\<\[`V1`, `D`\], `true`\>
135​
136### Examples
137​
138```ts
139caseWhen(gt(user.age, 18), `adult`, `minor`)
140```
141​
142```ts
143caseWhen(
144 gt(user.age, 65),
145 `senior`,
146 gt(user.age, 18),
147 `adult`,
148 `minor`,
150```
151​
152```ts
153caseWhen(gt(user.age, 18), {
154 ...user,
155 posts: include(
156 q
157 .from({ post: postsCollection })
158 .where(({ post }) => eq(post.userId, user.id)),
159 ),
160})
161```
162​
163## Call Signature
164​
165```ts
166function caseWhen<C1, V1, C2, V2>(
167 condition1,
168 value1,
169 condition2,
170value2): CaseWhenResult<[V1, V2], false>;
171```
172​
173Defined in: [packages/db/src/query/builder/functions.ts:416](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L416)
174​
175Returns the value for the first matching condition, similar to SQL
176`CASE WHEN`.
177​
178Arguments are evaluated as condition/value pairs followed by an optional
179default value. Scalar branch values return a query expression and can be used
180in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
181`having`, and equality join operands. If no scalar branch matches and no
182default is provided, the result is `null`.
183​
184When a branch value is a projection object, `caseWhen` becomes a select-only
185projection value. Projection branches can include nested fields, ref spreads,
186and includes. If no projection branch matches and no default is provided, the
187result is `undefined`.
188​
189### Type Parameters
190​
191#### C1
192​
193`C1` *extends* `ExpressionLike`
194​
195#### V1
196​
197`V1` *extends* `CaseWhenValue`
198​
199#### C2
200​
201`C2` *extends* `ExpressionLike`
202​
203#### V2
204​
205`V2` *extends* `CaseWhenValue`
206​
207### Parameters
208​
209#### condition1
210​
211`C1`
212​
213#### value1
214​
215`V1`
216​
217#### condition2
218​
219`C2`
220​
221#### value2
222​
223`V2`
224​
225### Returns
226​
227`CaseWhenResult`\<\[`V1`, `V2`\], `false`\>
228​
229### Examples
230​
231```ts
232caseWhen(gt(user.age, 18), `adult`, `minor`)
233```
234​
235```ts
236caseWhen(
237 gt(user.age, 65),
238 `senior`,
239 gt(user.age, 18),
240 `adult`,
241 `minor`,
243```
244​
245```ts
246caseWhen(gt(user.age, 18), {
247 ...user,
248 posts: include(
249 q
250 .from({ post: postsCollection })
251 .where(({ post }) => eq(post.userId, user.id)),
252 ),
253})
254```
255​
256## Call Signature
257​
258```ts
259function caseWhen<C1, V1, C2, V2, D>(
260 condition1,
261 value1,
262 condition2,
263 value2,
264defaultValue): CaseWhenResult<[V1, V2, D], true>;
265```
266​
267Defined in: [packages/db/src/query/builder/functions.ts:427](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L427)
268​
269Returns the value for the first matching condition, similar to SQL
270`CASE WHEN`.
271​
272Arguments are evaluated as condition/value pairs followed by an optional
273default value. Scalar branch values return a query expression and can be used
274in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
275`having`, and equality join operands. If no scalar branch matches and no
276default is provided, the result is `null`.
277​
278When a branch value is a projection object, `caseWhen` becomes a select-only
279projection value. Projection branches can include nested fields, ref spreads,
280and includes. If no projection branch matches and no default is provided, the
281result is `undefined`.
282​
283### Type Parameters
284​
285#### C1
286​
287`C1` *extends* `ExpressionLike`
288​
289#### V1
290​
291`V1` *extends* `CaseWhenValue`
292​
293#### C2
294​
295`C2` *extends* `ExpressionLike`
296​
297#### V2
298​
299`V2` *extends* `CaseWhenValue`
300​
301#### D
302​
303`D` *extends* `CaseWhenValue`
304​
305### Parameters
306​
307#### condition1
308​
309`C1`
310​
311#### value1
312​
313`V1`
314​
315#### condition2
316​
317`C2`
318​
319#### value2
320​
321`V2`
322​
323#### defaultValue
324​
325`D`
326​
327### Returns
328​
329`CaseWhenResult`\<\[`V1`, `V2`, `D`\], `true`\>
330​
331### Examples
332​
333```ts
334caseWhen(gt(user.age, 18), `adult`, `minor`)
335```
336​
337```ts
338caseWhen(
339 gt(user.age, 65),
340 `senior`,
341 gt(user.age, 18),
342 `adult`,
343 `minor`,
345```
346​
347```ts
348caseWhen(gt(user.age, 18), {
349 ...user,
350 posts: include(
351 q
352 .from({ post: postsCollection })
353 .where(({ post }) => eq(post.userId, user.id)),
354 ),
355})
356```
357​
358## Call Signature
359​
360```ts
361function caseWhen<C1, V1, C2, V2, C3, V3>(
362 condition1,
363 value1,
364 condition2,
365 value2,
366 condition3,
367value3): CaseWhenResult<[V1, V2, V3], false>;
368```
369​
370Defined in: [packages/db/src/query/builder/functions.ts:440](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L440)
371​
372Returns the value for the first matching condition, similar to SQL
373`CASE WHEN`.
374​
375Arguments are evaluated as condition/value pairs followed by an optional
376default value. Scalar branch values return a query expression and can be used
377in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
378`having`, and equality join operands. If no scalar branch matches and no
379default is provided, the result is `null`.
380​
381When a branch value is a projection object, `caseWhen` becomes a select-only
382projection value. Projection branches can include nested fields, ref spreads,
383and includes. If no projection branch matches and no default is provided, the
384result is `undefined`.
385​
386### Type Parameters
387​
388#### C1
389​
390`C1` *extends* `ExpressionLike`
391​
392#### V1
393​
394`V1` *extends* `CaseWhenValue`
395​
396#### C2
397​
398`C2` *extends* `ExpressionLike`
399​
400#### V2
401​
402`V2` *extends* `CaseWhenValue`
403​
404#### C3
405​
406`C3` *extends* `ExpressionLike`
407​
408#### V3
409​
410`V3` *extends* `CaseWhenValue`
411​
412### Parameters
413​
414#### condition1
415​
416`C1`
417​
418#### value1
419​
420`V1`
421​
422#### condition2
423​
424`C2`
425​
426#### value2
427​
428`V2`
429​
430#### condition3
431​
432`C3`
433​
434#### value3
435​
436`V3`
437​
438### Returns
439​
440`CaseWhenResult`\<\[`V1`, `V2`, `V3`\], `false`\>
441​
442### Examples
443​
444```ts
445caseWhen(gt(user.age, 18), `adult`, `minor`)
446```
447​
448```ts
449caseWhen(
450 gt(user.age, 65),
451 `senior`,
452 gt(user.age, 18),
453 `adult`,
454 `minor`,
456```
457​
458```ts
459caseWhen(gt(user.age, 18), {
460 ...user,
461 posts: include(
462 q
463 .from({ post: postsCollection })
464 .where(({ post }) => eq(post.userId, user.id)),
465 ),
466})
467```
468​
469## Call Signature
470​
471```ts
472function caseWhen<C1, V1, C2, V2, C3, V3, D>(
473 condition1,
474 value1,
475 condition2,
476 value2,
477 condition3,
478 value3,
479defaultValue): CaseWhenResult<[V1, V2, V3, D], true>;
480```
481​
482Defined in: [packages/db/src/query/builder/functions.ts:455](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L455)
483​
484Returns the value for the first matching condition, similar to SQL
485`CASE WHEN`.
486​
487Arguments are evaluated as condition/value pairs followed by an optional
488default value. Scalar branch values return a query expression and can be used
489in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
490`having`, and equality join operands. If no scalar branch matches and no
491default is provided, the result is `null`.
492​
493When a branch value is a projection object, `caseWhen` becomes a select-only
494projection value. Projection branches can include nested fields, ref spreads,
495and includes. If no projection branch matches and no default is provided, the
496result is `undefined`.
497​
498### Type Parameters
499​
500#### C1
501​
502`C1` *extends* `ExpressionLike`
503​
504#### V1
505​
506`V1` *extends* `CaseWhenValue`
507​
508#### C2
509​
510`C2` *extends* `ExpressionLike`
511​
512#### V2
513​
514`V2` *extends* `CaseWhenValue`
515​
516#### C3
517​
518`C3` *extends* `ExpressionLike`
519​
520#### V3
521​
522`V3` *extends* `CaseWhenValue`
523​
524#### D
525​
526`D` *extends* `CaseWhenValue`
527​
528### Parameters
529​
530#### condition1
531​
532`C1`
533​
534#### value1
535​
536`V1`
537​
538#### condition2
539​
540`C2`
541​
542#### value2
543​
544`V2`
545​
546#### condition3
547​
548`C3`
549​
550#### value3
551​
552`V3`
553​
554#### defaultValue
555​
556`D`
557​
558### Returns
559​
560`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `D`\], `true`\>
561​
562### Examples
563​
564```ts
565caseWhen(gt(user.age, 18), `adult`, `minor`)
566```
567​
568```ts
569caseWhen(
570 gt(user.age, 65),
571 `senior`,
572 gt(user.age, 18),
573 `adult`,
574 `minor`,
576```
577​
578```ts
579caseWhen(gt(user.age, 18), {
580 ...user,
581 posts: include(
582 q
583 .from({ post: postsCollection })
584 .where(({ post }) => eq(post.userId, user.id)),
585 ),
586})
587```
588​
589## Call Signature
590​
591```ts
592function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4>(
593 condition1,
594 value1,
595 condition2,
596 value2,
597 condition3,
598 value3,
599 condition4,
600value4): CaseWhenResult<[V1, V2, V3, V4], false>;
601```
602​
603Defined in: [packages/db/src/query/builder/functions.ts:472](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L472)
604​
605Returns the value for the first matching condition, similar to SQL
606`CASE WHEN`.
607​
608Arguments are evaluated as condition/value pairs followed by an optional
609default value. Scalar branch values return a query expression and can be used
610in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
611`having`, and equality join operands. If no scalar branch matches and no
612default is provided, the result is `null`.
613​
614When a branch value is a projection object, `caseWhen` becomes a select-only
615projection value. Projection branches can include nested fields, ref spreads,
616and includes. If no projection branch matches and no default is provided, the
617result is `undefined`.
618​
619### Type Parameters
620​
621#### C1
622​
623`C1` *extends* `ExpressionLike`
624​
625#### V1
626​
627`V1` *extends* `CaseWhenValue`
628​
629#### C2
630​
631`C2` *extends* `ExpressionLike`
632​
633#### V2
634​
635`V2` *extends* `CaseWhenValue`
636​
637#### C3
638​
639`C3` *extends* `ExpressionLike`
640​
641#### V3
642​
643`V3` *extends* `CaseWhenValue`
644​
645#### C4
646​
647`C4` *extends* `ExpressionLike`
648​
649#### V4
650​
651`V4` *extends* `CaseWhenValue`
652​
653### Parameters
654​
655#### condition1
656​
657`C1`
658​
659#### value1
660​
661`V1`
662​
663#### condition2
664​
665`C2`
666​
667#### value2
668​
669`V2`
670​
671#### condition3
672​
673`C3`
674​
675#### value3
676​
677`V3`
678​
679#### condition4
680​
681`C4`
682​
683#### value4
684​
685`V4`
686​
687### Returns
688​
689`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`\], `false`\>
690​
691### Examples
692​
693```ts
694caseWhen(gt(user.age, 18), `adult`, `minor`)
695```
696​
697```ts
698caseWhen(
699 gt(user.age, 65),
700 `senior`,
701 gt(user.age, 18),
702 `adult`,
703 `minor`,
705```
706​
707```ts
708caseWhen(gt(user.age, 18), {
709 ...user,
710 posts: include(
711 q
712 .from({ post: postsCollection })
713 .where(({ post }) => eq(post.userId, user.id)),
714 ),
715})
716```
717​
718## Call Signature
719​
720```ts
721function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, D>(
722 condition1,
723 value1,
724 condition2,
725 value2,
726 condition3,
727 value3,
728 condition4,
729 value4,
730defaultValue): CaseWhenResult<[V1, V2, V3, V4, D], true>;
731```
732​
733Defined in: [packages/db/src/query/builder/functions.ts:491](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L491)
734​
735Returns the value for the first matching condition, similar to SQL
736`CASE WHEN`.
737​
738Arguments are evaluated as condition/value pairs followed by an optional
739default value. Scalar branch values return a query expression and can be used
740in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
741`having`, and equality join operands. If no scalar branch matches and no
742default is provided, the result is `null`.
743​
744When a branch value is a projection object, `caseWhen` becomes a select-only
745projection value. Projection branches can include nested fields, ref spreads,
746and includes. If no projection branch matches and no default is provided, the
747result is `undefined`.
748​
749### Type Parameters
750​
751#### C1
752​
753`C1` *extends* `ExpressionLike`
754​
755#### V1
756​
757`V1` *extends* `CaseWhenValue`
758​
759#### C2
760​
761`C2` *extends* `ExpressionLike`
762​
763#### V2
764​
765`V2` *extends* `CaseWhenValue`
766​
767#### C3
768​
769`C3` *extends* `ExpressionLike`
770​
771#### V3
772​
773`V3` *extends* `CaseWhenValue`
774​
775#### C4
776​
777`C4` *extends* `ExpressionLike`
778​
779#### V4
780​
781`V4` *extends* `CaseWhenValue`
782​
783#### D
784​
785`D` *extends* `CaseWhenValue`
786​
787### Parameters
788​
789#### condition1
790​
791`C1`
792​
793#### value1
794​
795`V1`
796​
797#### condition2
798​
799`C2`
800​
801#### value2
802​
803`V2`
804​
805#### condition3
806​
807`C3`
808​
809#### value3
810​
811`V3`
812​
813#### condition4
814​
815`C4`
816​
817#### value4
818​
819`V4`
820​
821#### defaultValue
822​
823`D`
824​
825### Returns
826​
827`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`, `D`\], `true`\>
828​
829### Examples
830​
831```ts
832caseWhen(gt(user.age, 18), `adult`, `minor`)
833```
834​
835```ts
836caseWhen(
837 gt(user.age, 65),
838 `senior`,
839 gt(user.age, 18),
840 `adult`,
841 `minor`,
843```
844​
845```ts
846caseWhen(gt(user.age, 18), {
847 ...user,
848 posts: include(
849 q
850 .from({ post: postsCollection })
851 .where(({ post }) => eq(post.userId, user.id)),
852 ),
853})
854```
855​
856## Call Signature
857​
858```ts
859function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, C5, V5>(
860 condition1,
861 value1,
862 condition2,
863 value2,
864 condition3,
865 value3,
866 condition4,
867 value4,
868 condition5,
869value5): CaseWhenResult<[V1, V2, V3, V4, V5], false>;
870```
871​
872Defined in: [packages/db/src/query/builder/functions.ts:512](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L512)
873​
874Returns the value for the first matching condition, similar to SQL
875`CASE WHEN`.
876​
877Arguments are evaluated as condition/value pairs followed by an optional
878default value. Scalar branch values return a query expression and can be used
879in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
880`having`, and equality join operands. If no scalar branch matches and no
881default is provided, the result is `null`.
882​
883When a branch value is a projection object, `caseWhen` becomes a select-only
884projection value. Projection branches can include nested fields, ref spreads,
885and includes. If no projection branch matches and no default is provided, the
886result is `undefined`.
887​
888### Type Parameters
889​
890#### C1
891​
892`C1` *extends* `ExpressionLike`
893​
894#### V1
895​
896`V1` *extends* `CaseWhenValue`
897​
898#### C2
899​
900`C2` *extends* `ExpressionLike`
901​
902#### V2
903​
904`V2` *extends* `CaseWhenValue`
905​
906#### C3
907​
908`C3` *extends* `ExpressionLike`
909​
910#### V3
911​
912`V3` *extends* `CaseWhenValue`
913​
914#### C4
915​
916`C4` *extends* `ExpressionLike`
917​
918#### V4
919​
920`V4` *extends* `CaseWhenValue`
921​
922#### C5
923​
924`C5` *extends* `ExpressionLike`
925​
926#### V5
927​
928`V5` *extends* `CaseWhenValue`
929​
930### Parameters
931​
932#### condition1
933​
934`C1`
935​
936#### value1
937​
938`V1`
939​
940#### condition2
941​
942`C2`
943​
944#### value2
945​
946`V2`
947​
948#### condition3
949​
950`C3`
951​
952#### value3
953​
954`V3`
955​
956#### condition4
957​
958`C4`
959​
960#### value4
961​
962`V4`
963​
964#### condition5
965​
966`C5`
967​
968#### value5
969​
970`V5`
971​
972### Returns
973​
974`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`, `V5`\], `false`\>
975​
976### Examples
977​
978```ts
979caseWhen(gt(user.age, 18), `adult`, `minor`)
980```
981​
982```ts
983caseWhen(
984 gt(user.age, 65),
985 `senior`,
986 gt(user.age, 18),
987 `adult`,
988 `minor`,
990```
991​
992```ts
993caseWhen(gt(user.age, 18), {
994 ...user,
995 posts: include(
996 q
997 .from({ post: postsCollection })
998 .where(({ post }) => eq(post.userId, user.id)),
999 ),
1001```
1002​
1003## Call Signature
1004​
1005```ts
1006function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, C5, V5, D>(
1007 condition1,
1008 value1,
1009 condition2,
1010 value2,
1011 condition3,
1012 value3,
1013 condition4,
1014 value4,
1015 condition5,
1016 value5,
1017defaultValue): CaseWhenResult<[V1, V2, V3, V4, V5, D], true>;
1018```
1019​
1020Defined in: [packages/db/src/query/builder/functions.ts:535](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L535)
1021​
1022Returns the value for the first matching condition, similar to SQL
1023`CASE WHEN`.
1024​
1025Arguments are evaluated as condition/value pairs followed by an optional
1026default value. Scalar branch values return a query expression and can be used
1027in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
1028`having`, and equality join operands. If no scalar branch matches and no
1029default is provided, the result is `null`.
1030​
1031When a branch value is a projection object, `caseWhen` becomes a select-only
1032projection value. Projection branches can include nested fields, ref spreads,
1033and includes. If no projection branch matches and no default is provided, the
1034result is `undefined`.
1035​
1036### Type Parameters
1037​
1038#### C1
1039​
1040`C1` *extends* `ExpressionLike`
1041​
1042#### V1
1043​
1044`V1` *extends* `CaseWhenValue`
1045​
1046#### C2
1047​
1048`C2` *extends* `ExpressionLike`
1049​
1050#### V2
1051​
1052`V2` *extends* `CaseWhenValue`
1053​
1054#### C3
1055​
1056`C3` *extends* `ExpressionLike`
1057​
1058#### V3
1059​
1060`V3` *extends* `CaseWhenValue`
1061​
1062#### C4
1063​
1064`C4` *extends* `ExpressionLike`
1065​
1066#### V4
1067​
1068`V4` *extends* `CaseWhenValue`
1069​
1070#### C5
1071​
1072`C5` *extends* `ExpressionLike`
1073​
1074#### V5
1075​
1076`V5` *extends* `CaseWhenValue`
1077​
1078#### D
1079​
1080`D` *extends* `CaseWhenValue`
1081​
1082### Parameters
1083​
1084#### condition1
1085​
1086`C1`
1087​
1088#### value1
1089​
1090`V1`
1091​
1092#### condition2
1093​
1094`C2`
1095​
1096#### value2
1097​
1098`V2`
1099​
1100#### condition3
1101​
1102`C3`
1103​
1104#### value3
1105​
1106`V3`
1107​
1108#### condition4
1109​
1110`C4`
1111​
1112#### value4
1113​
1114`V4`
1115​
1116#### condition5
1117​
1118`C5`
1119​
1120#### value5
1121​
1122`V5`
1123​
1124#### defaultValue
1125​
1126`D`
1127​
1128### Returns
1129​
1130`CaseWhenResult`\<\[`V1`, `V2`, `V3`, `V4`, `V5`, `D`\], `true`\>
1131​
1132### Examples
1133​
1134```ts
1135caseWhen(gt(user.age, 18), `adult`, `minor`)
1136```
1137​
1138```ts
1139caseWhen(
1140 gt(user.age, 65),
1141 `senior`,
1142 gt(user.age, 18),
1143 `adult`,
1144 `minor`,
1146```
1147​
1148```ts
1149caseWhen(gt(user.age, 18), {
1150 ...user,
1151 posts: include(
1153 .from({ post: postsCollection })
1154 .where(({ post }) => eq(post.userId, user.id)),
1155 ),
1157```
1158​
1159## Call Signature
1160​
1161```ts
1162function caseWhen<C1, V1, C2, V2, C3, V3, C4, V4, C5, V5>(
1163 condition1,
1164 value1,
1165 condition2,
1166 value2,
1167 condition3,
1168 value3,
1169 condition4,
1170 value4,
1171 condition5,
1172 value5,
1173 condition6,
1174 value6, ...
1175 rest): any;
1176```
1177​
1178Defined in: [packages/db/src/query/builder/functions.ts:560](https://github.com/TanStack/db/blob/main/packages/db/src/query/builder/functions.ts#L560)
1179​
1180Returns the value for the first matching condition, similar to SQL
1181`CASE WHEN`.
1182​
1183Arguments are evaluated as condition/value pairs followed by an optional
1184default value. Scalar branch values return a query expression and can be used
1185in expression contexts like `select`, `where`, `orderBy`, `groupBy`,
1186`having`, and equality join operands. If no scalar branch matches and no
1187default is provided, the result is `null`.
1188​
1189When a branch value is a projection object, `caseWhen` becomes a select-only
1190projection value. Projection branches can include nested fields, ref spreads,
1191and includes. If no projection branch matches and no default is provided, the
1192result is `undefined`.
1193​
1194### Type Parameters
1195​
1196#### C1
1197​
1198`C1` *extends* `ExpressionLike`
1199​
1200#### V1
1201​
1202`V1` *extends* `CaseWhenValue`
1203​
1204#### C2
1205​
1206`C2` *extends* `ExpressionLike`
1207​
1208#### V2
1209​
1210`V2` *extends* `CaseWhenValue`
1211​
1212#### C3
1213​
1214`C3` *extends* `ExpressionLike`
1215​
1216#### V3
1217​
1218`V3` *extends* `CaseWhenValue`
1219​
1220#### C4
1221​
1222`C4` *extends* `ExpressionLike`
1223​
1224#### V4
1225​
1226`V4` *extends* `CaseWhenValue`
1227​
1228#### C5
1229​
1230`C5` *extends* `ExpressionLike`
1231​
1232#### V5
1233​
1234`V5` *extends* `CaseWhenValue`
1235​
1236### Parameters
1237​
1238#### condition1
1239​
1240`C1`
1241​
1242#### value1
1243​
1244`V1`
1245​
1246#### condition2
1247​
1248`C2`
1249​
1250#### value2
1251​
1252`V2`
1253​
1254#### condition3
1255​
1256`C3`
1257​
1258#### value3
1259​
1260`V3`
1261​
1262#### condition4
1263​
1264`C4`
1265​
1266#### value4
1267​
1268`V4`
1269​
1270#### condition5
1271​
1272`C5`
1273​
1274#### value5
1275​
1276`V5`
1277​
1278#### condition6
1279​
1280`ExpressionLike`
1281​
1282#### value6
1283​
1284`CaseWhenValue`
1285​
1286#### rest
1287​
1288...`CaseWhenValue`[]
1289​
1290### Returns
1291​
1292`any`
1293​
1294### Examples
1295​
1296```ts
1297caseWhen(gt(user.age, 18), `adult`, `minor`)
1298```
1299​
1300```ts
1301caseWhen(
1302 gt(user.age, 65),
1303 `senior`,
1304 gt(user.age, 18),
1305 `adult`,
1306 `minor`,
1308```
1309​
1310```ts
1311caseWhen(gt(user.age, 18), {
1312 ...user,
1313 posts: include(
1315 .from({ post: postsCollection })
1316 .where(({ post }) => eq(post.userId, user.id)),
1317 ),
1319```