Skip to content

Commit

Permalink
Support subquery in case then. Closes #811
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Dec 29, 2023
1 parent b5a48a7 commit 125b000
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

1 comment on commit 125b000

@vercel
Copy link

@vercel vercel bot commented on 125b000 Dec 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

kysely – ./

www.kysely.dev
kysely-kysely-team.vercel.app
kysely-git-master-kysely-team.vercel.app
kysely.dev

Please sign in to comment.