Skip to content

Commit

Permalink
allow nullable right operand in comparisons. Closes #261
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Dec 29, 2022
1 parent d57ead2 commit 3a19e86
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 80 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kysely",
"version": "0.23.2",
"version": "0.23.3",
"description": "Type safe SQL query builder",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/parser/binary-operation-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export type OperandValueExpressionOrList<
> = ValueExpressionOrList<
DB,
TB,
ExtractTypeFromReferenceExpression<DB, TB, RE>
ExtractTypeFromReferenceExpression<DB, TB, RE> | null
>

export type WhereGrouper<DB, TB extends keyof DB> = (
Expand Down
78 changes: 0 additions & 78 deletions test/typings/test-d/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,84 +324,6 @@ async function testSelectDynamic(db: Kysely<Database>) {
}>(r4)
}

function testWhere(db: Kysely<Database>) {
// Column name
db.selectFrom('person').where('first_name', '=', 'Arnold')

// Table and column
db.selectFrom('person').where('person.age', '=', 25)

// Schema, table and column
db.selectFrom('some_schema.movie').where('some_schema.movie.id', '=', '1')

// Subquery in LHS
db.selectFrom('movie').where(
(qb) => qb.selectFrom('person').select('gender'),
'=',
'female'
)

// Subquery in RHS
db.selectFrom('movie').where(sql`${'female'}`, '=', (qb) =>
qb.selectFrom('person').select('gender')
)

// Raw expression
db.selectFrom('person').where('first_name', '=', sql`'foo'`)
db.selectFrom('person').where('first_name', '=', sql<string>`'foo'`)
db.selectFrom('person').where(sql`whatever`, '=', 1)
db.selectFrom('person').where(sql`whatever`, '=', true)
db.selectFrom('person').where(sql`whatever`, '=', '1')

// List value
db.selectFrom('person').where('gender', 'in', ['female', 'male'])

// Raw operator
db.selectFrom('person').where('person.age', sql`lol`, 25)

// Invalid operator
expectError(db.selectFrom('person').where('person.age', 'lol', 25))

// Invalid table
expectError(db.selectFrom('person').where('movie.stars', '=', 25))

// Invalid column
expectError(db.selectFrom('person').where('stars', '=', 25))

// Invalid type for column
expectError(db.selectFrom('person').where('age', '=', '25'))

// Invalid type for column
expectError(db.selectFrom('person').where('gender', '=', 'not_a_gender'))

// Invalid type for column
expectError(
db.selectFrom('person').where('gender', 'in', ['female', 'not_a_gender'])
)

// Invalid type for column
expectError(
db.selectFrom('some_schema.movie').where('some_schema.movie.id', '=', 1)
)

// Invalid type for column
expectError(
db
.selectFrom('some_schema.movie')
.where(
(qb) => qb.selectFrom('person').select('gender'),
'=',
'not_a_gender'
)
)

// Invalid type for column
expectError(db.selectFrom('person').where('first_name', '=', sql<number>`1`))

// Invalid type for column
expectError(db.selectFrom('person').where(sql<string>`first_name`, '=', 1))
}

async function testConditionalJoinWhere(db: Kysely<Database>) {
let qb = db.selectFrom('person')
let petName: string | undefined = 'catto'
Expand Down
106 changes: 106 additions & 0 deletions test/typings/test-d/where.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { Kysely, sql } from '..'
import { Database } from '../shared'
import { expectError } from 'tsd'

function testWhere(db: Kysely<Database>) {
// Column name
db.selectFrom('person').where('first_name', '=', 'Arnold')

// Table and column
db.selectFrom('person').where('person.age', '=', 25)

// Schema, table and column
db.selectFrom('some_schema.movie').where('some_schema.movie.id', '=', '1')

const nullableAge = 25 as number | null
// Nullable RHS value
db.selectFrom('person').where('age', 'in', nullableAge)

// Nullable RHS reference
db.selectFrom('person').whereRef('first_name', '=', 'last_name')

// Nullable LHS with value
db.selectFrom('person').where('last_name', '=', 'Jennifer')

// Nullable LHS with reference
db.selectFrom('person').whereRef('last_name', '=', 'first_name')

// Subquery in LHS
db.selectFrom('movie').where(
(qb) => qb.selectFrom('person').select('gender'),
'=',
'female'
)

// Nullable subquery in LHS
db.selectFrom('movie').where(
(qb) => qb.selectFrom('person').select('last_name'),
'=',
'female'
)

// Subquery in RHS
db.selectFrom('movie').where(sql`${'female'}`, '=', (qb) =>
qb.selectFrom('person').select('gender')
)

// Nullable subquery in RHS
db.selectFrom('person').where('first_name', 'in', (qb) =>
qb.selectFrom('person').select('last_name')
)

// Raw expression
db.selectFrom('person').where('first_name', '=', sql`'foo'`)
db.selectFrom('person').where('first_name', '=', sql<string>`'foo'`)
db.selectFrom('person').where(sql`whatever`, '=', 1)
db.selectFrom('person').where(sql`whatever`, '=', true)
db.selectFrom('person').where(sql`whatever`, '=', '1')

// List value
db.selectFrom('person').where('gender', 'in', ['female', 'male'])

// Raw operator
db.selectFrom('person').where('person.age', sql`lol`, 25)

// Invalid operator
expectError(db.selectFrom('person').where('person.age', 'lol', 25))

// Invalid table
expectError(db.selectFrom('person').where('movie.stars', '=', 25))

// Invalid column
expectError(db.selectFrom('person').where('stars', '=', 25))

// Invalid type for column
expectError(db.selectFrom('person').where('age', '=', '25'))

// Invalid type for column
expectError(db.selectFrom('person').where('gender', '=', 'not_a_gender'))

// Invalid type for column
expectError(
db.selectFrom('person').where('gender', 'in', ['female', 'not_a_gender'])
)

// Invalid type for column
expectError(
db.selectFrom('some_schema.movie').where('some_schema.movie.id', '=', 1)
)

// Invalid type for column
expectError(
db
.selectFrom('some_schema.movie')
.where(
(qb) => qb.selectFrom('person').select('gender'),
'=',
'not_a_gender'
)
)

// Invalid type for column
expectError(db.selectFrom('person').where('first_name', '=', sql<number>`1`))

// Invalid type for column
expectError(db.selectFrom('person').where(sql<string>`first_name`, '=', 1))
}

0 comments on commit 3a19e86

Please sign in to comment.