Skip to content

Commit

Permalink
Support subquery in case then. Closes kysely-org#811
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas authored and thecodrr committed Sep 3, 2024
1 parent 47d04f8 commit 20492a8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/expression/expression-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ export interface ExpressionBuilder<DB, TB extends keyof DB> {
column: C
): CaseBuilder<DB, TB, ExtractTypeFromReferenceExpression<DB, TB, C>>

case<O>(expression: Expression<O>): CaseBuilder<DB, TB, O>
case<E extends Expression<any>>(
expression: E
): CaseBuilder<DB, TB, ExtractTypeFromValueExpression<E>>

/**
* This method can be used to reference columns within the query's context. For
Expand Down
5 changes: 4 additions & 1 deletion src/query-builder/case-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
parseValueBinaryOperationOrExpression,
} from '../parser/binary-operation-parser.js'
import {
ExtractTypeFromValueExpression,
isSafeImmediateValue,
parseSafeImmediateValue,
parseValueExpression,
Expand Down Expand Up @@ -72,7 +73,9 @@ export class CaseThenBuilder<DB, TB extends keyof DB, W, O> {
* A `then` call can be followed by {@link Whenable.when}, {@link CaseWhenBuilder.else},
* {@link CaseWhenBuilder.end} or {@link CaseWhenBuilder.endCase} call.
*/
then<O2>(expression: Expression<O2>): CaseWhenBuilder<DB, TB, W, O | O2>
then<E extends Expression<any>>(
expression: E
): CaseWhenBuilder<DB, TB, W, O | ExtractTypeFromValueExpression<E>>

then<V>(value: V): CaseWhenBuilder<DB, TB, W, O | V>

Expand Down
9 changes: 9 additions & 0 deletions test/typings/test-d/case.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ async function testCase(eb: ExpressionBuilder<Database, 'person'>) {
.end()
)

// subquery
expectType<ExpressionWrapper<Database, 'person', string | null>>(
eb
.case()
.when('gender', '=', 'male')
.then(eb.selectFrom('person').select('first_name'))
.end()
)

// errors

expectError(eb.case().when('no_such_column', '=', 'male').then('Mr.').end())
Expand Down

0 comments on commit 20492a8

Please sign in to comment.