Last Updated: 3/9/2026
- ExpressionWrapper
Class ExpressionWrapper
An expression with an as method.
Type Parameters
- DB
- TB extends keyof DB
- T
Implements
Index
Constructors
Methods
$castTo $notNull and as or toOperationNode
Constructors
constructor
-
new ExpressionWrapper<DB, TB extends string | number | symbol, T>(
node: OperationNode,
): ExpressionWrapper<DB, TB, T>Type Parameters
- DB
- TB extends string | number | symbol
- T
Parameters
- node: OperationNode
Returns ExpressionWrapper<DB, TB, T>
Methods
$castTo
-
$castTo<C>(): ExpressionWrapper<DB, TB, C>
Change the output type of the expression.
This method call doesn’t change the SQL in any way. This methods simply returns a copy of this
ExpressionWrapperwith a new output type.Type Parameters
- C
Returns ExpressionWrapper<DB, TB, C>
$notNull
-
$notNull(): ExpressionWrapper<DB, TB, Exclude<T, null>>
Omit null from the expression’s type.
This function can be useful in cases where you know an expression can’t be null, but Kysely is unable to infer it.
This method call doesn’t change the SQL in any way. This methods simply returns a copy of
thiswith a new output type.Returns ExpressionWrapper<DB, TB, Exclude<T, null>>
and
-
and<
RE extends
| string
| Expression
| DynamicReferenceBuilder
| SelectQueryBuilderExpression<Record<string, any>>
| OperandExpressionFactory<DB, TB, any>,
VE extends any,(
lhs: RE,
op: ComparisonOperatorExpression,
rhs: VE,
): T extends SqlBool
? AndWrapper<DB, TB, SqlBool>
: KyselyTypeError<“and() method can only be called on boolean expressions”>Combines
thisand another expression usingAND.Also see ExpressionBuilder.and
Examples
const result = await db. selectFrom('person') . selectAll() . where(eb => eb('first_name', '=', 'Jennifer') . and('last_name', '=', 'Aniston') . and('age', '>', 40) ) . execute()The generated SQL (PostgreSQL):
select * from "person" where ( "first_name" = $1 and "last_name" = $2 and "age" > $3 )You can also pass any expression as the only argument to this method:
const result = await db. selectFrom('person') . selectAll() . where(eb => eb('first_name', '=', 'Jennifer') . and(eb('first_name', '=', 'Sylvester'). or('last_name', '=', 'Stallone')) . and(eb. exists( eb. selectFrom('pet') . select('id') . whereRef('pet.owner_id', '=', 'person.id') )) ) . execute()The generated SQL (PostgreSQL):
select * from "person" where ( "first_name" = $1 and ("first_name" = $2 or "last_name" = $3) and exists ( select "id" from "pet" where "pet"."owner_id" = "person"."id" ) )Type Parameters
- RE extends
| string
| Expression
| DynamicReferenceBuilder
| SelectQueryBuilderExpression<Record<string, any>>
| OperandExpressionFactory<DB, TB, any> - VE extends any
Parameters
- lhs: RE
- op: ComparisonOperatorExpression
- rhs: VE
Returns T extends SqlBool ? AndWrapper<DB, TB, SqlBool> : KyselyTypeError<“and() method can only be called on boolean expressions”>
- RE extends
-
and<E extends OperandExpression<SqlBool>>(
expression: E,
): T extends SqlBool
? AndWrapper<DB, TB, SqlBool>
: KyselyTypeError<“and() method can only be called on boolean expressions”>Combines
thisand another expression usingAND.Also see ExpressionBuilder.and
Examples
const result = await db. selectFrom('person') . selectAll() . where(eb => eb('first_name', '=', 'Jennifer') . and('last_name', '=', 'Aniston') . and('age', '>', 40) ) . execute()The generated SQL (PostgreSQL):
select * from "person" where ( "first_name" = $1 and "last_name" = $2 and "age" > $3 )You can also pass any expression as the only argument to this method:
const result = await db. selectFrom('person') . selectAll() . where(eb => eb('first_name', '=', 'Jennifer') . and(eb('first_name', '=', 'Sylvester'). or('last_name', '=', 'Stallone')) . and(eb. exists( eb. selectFrom('pet') . select('id') . whereRef('pet.owner_id', '=', 'person.id') )) ) . execute()The generated SQL (PostgreSQL):
select * from "person" where ( "first_name" = $1 and ("first_name" = $2 or "last_name" = $3) and exists ( select "id" from "pet" where "pet"."owner_id" = "person"."id" ) )Type Parameters
- E extends OperandExpression<SqlBool>
Parameters
- expression: E
Returns T extends SqlBool ? AndWrapper<DB, TB, SqlBool> : KyselyTypeError<“and() method can only be called on boolean expressions”>
as
-
as<A extends string>(alias: A): AliasedExpression<T, A>
Returns an aliased version of the expression.
Examples
In addition to slapping
as "the_alias"to the end of the SQL, this method also provides strict typing:const result = await db . selectFrom('person') . select((eb) => eb('first_name', '=', 'Jennifer'). as('is_jennifer') ) . executeTakeFirstOrThrow()// `is_jennifer: SqlBool` field exists in the result type. console. log(result. is_jennifer)The generated SQL (PostgreSQL):
select "first_name" = $1 as "is_jennifer" from "person"Type Parameters
- A extends string
Parameters
- alias: A
Returns AliasedExpression<T, A>
-
as<A extends string>(alias: Expression
): AliasedExpression<T, A> Returns an aliased version of the expression.
Examples
In addition to slapping
as "the_alias"to the end of the SQL, this method also provides strict typing:const result = await db . selectFrom('person') . select((eb) => eb('first_name', '=', 'Jennifer'). as('is_jennifer') ) . executeTakeFirstOrThrow()// `is_jennifer: SqlBool` field exists in the result type. console. log(result. is_jennifer)The generated SQL (PostgreSQL):
select "first_name" = $1 as "is_jennifer" from "person"Type Parameters
- A extends string
Parameters
- alias: Expression
Returns AliasedExpression<T, A>
or
-
or<
RE extends
| string
| Expression
| DynamicReferenceBuilder
| SelectQueryBuilderExpression<Record<string, any>>
| OperandExpressionFactory<DB, TB, any>,
VE extends any,(
lhs: RE,
op: ComparisonOperatorExpression,
rhs: VE,
): T extends SqlBool
? OrWrapper<DB, TB, SqlBool>
: KyselyTypeError<“or() method can only be called on boolean expressions”>Combines
thisand another expression usingOR.Also see ExpressionBuilder.or
Examples
const result = await db. selectFrom('person') . selectAll() . where(eb => eb('first_name', '=', 'Jennifer') . or('first_name', '=', 'Arnold') . or('first_name', '=', 'Sylvester') ) . execute()The generated SQL (PostgreSQL):
select * from "person" where ( "first_name" = $1 or "first_name" = $2 or "first_name" = $3 )You can also pass any expression as the only argument to this method:
const result = await db. selectFrom('person') . selectAll() . where(eb => eb('first_name', '=', 'Jennifer') . or(eb('first_name', '=', 'Sylvester'). and('last_name', '=', 'Stallone')) . or(eb. exists( eb. selectFrom('pet') . select('id') . whereRef('pet.owner_id', '=', 'person.id') )) ) . execute()The generated SQL (PostgreSQL):
select * from "person" where ( "first_name" = $1 or ("first_name" = $2 and "last_name" = $3) or exists ( select "id" from "pet" where "pet"."owner_id" = "person"."id" ) )Type Parameters
- RE extends
| string
| Expression
| DynamicReferenceBuilder
| SelectQueryBuilderExpression<Record<string, any>>
| OperandExpressionFactory<DB, TB, any> - VE extends any
Parameters
- lhs: RE
- op: ComparisonOperatorExpression
- rhs: VE
Returns T extends SqlBool ? OrWrapper<DB, TB, SqlBool> : KyselyTypeError<“or() method can only be called on boolean expressions”>
- RE extends
-
or<E extends OperandExpression<SqlBool>>(
expression: E,
): T extends SqlBool
? OrWrapper<DB, TB, SqlBool>
: KyselyTypeError<“or() method can only be called on boolean expressions”>Combines
thisand another expression usingOR.Also see ExpressionBuilder.or
Examples
const result = await db. selectFrom('person') . selectAll() . where(eb => eb('first_name', '=', 'Jennifer') . or('first_name', '=', 'Arnold') . or('first_name', '=', 'Sylvester') ) . execute()The generated SQL (PostgreSQL):
select * from "person" where ( "first_name" = $1 or "first_name" = $2 or "first_name" = $3 )You can also pass any expression as the only argument to this method:
const result = await db. selectFrom('person') . selectAll() . where(eb => eb('first_name', '=', 'Jennifer') . or(eb('first_name', '=', 'Sylvester'). and('last_name', '=', 'Stallone')) . or(eb. exists( eb. selectFrom('pet') . select('id') . whereRef('pet.owner_id', '=', 'person.id') )) ) . execute()The generated SQL (PostgreSQL):
select * from "person" where ( "first_name" = $1 or ("first_name" = $2 and "last_name" = $3) or exists ( select "id" from "pet" where "pet"."owner_id" = "person"."id" ) )Type Parameters
- E extends OperandExpression<SqlBool>
Parameters
- expression: E
Returns T extends SqlBool ? OrWrapper<DB, TB, SqlBool> : KyselyTypeError<“or() method can only be called on boolean expressions”>
toOperationNode
-
toOperationNode(): OperationNode
Creates the OperationNode that describes how to compile this expression into SQL.
Examples
If you are creating a custom expression, it’s often easiest to use the sql template tag to build the node:
import { type Expression, type OperationNode, sql } from 'kysely' class SomeExpression< T> implements Expression< T> { get expressionType(): T | undefined { return undefined } toOperationNode(): OperationNode { return sql `some sql here`. toOperationNode() }}Returns OperationNode
Settings
Member Visibility
On This Page
Constructors
Methods