Skip to main content

ExpressionBuilder

ExpressionBuilder provides a type-safe way to build SQL expressions in Kysely. It’s commonly passed as a callback parameter (often named eb) in methods like where, select, set, and many others.

Type Parameters

object
required
The database schema type
keyof DB
required
The table references available in the current query context

Methods

Binary Expression (Call Signature)

Creates a binary expression for comparing values or performing arithmetic operations.
ReferenceExpression
required
Left-hand side of the expression (typically a column reference)
BinaryOperatorExpression
required
The operator (e.g., =, >, <, +, -, in, like)
OperandValueExpressionOrList
required
Right-hand side value or expression
Example:

eb

Returns a copy of the expression builder for destructuring.
Example:

fn

Returns a FunctionModule for writing type-safe function calls.
The difference between this and Kysely.fn is that this one is more type safe - you can only refer to columns visible to the part of the query you are building. Example:

selectFrom

Creates a subquery that can reference parent query tables.
TableExpressionOrList
required
Table expression or list of tables to select from
Example:

case

Creates a case statement/operator.
SimpleReferenceExpression
Optional column or expression to compare in when clauses
Example:

ref

Creates a column reference expression.
StringReference
required
Column reference string (e.g., first_name, person.id)
JSONOperatorWith$
JSON path operator (->$ or ->>$) for JSON traversal
Example:
JSON path example:

jsonPath

Creates a JSON path expression with the provided column as root document.
StringReference
required
Column reference to use as JSON path root (passed as type parameter)
Example:

table

Creates a table reference.
string
required
Table name
Example:

val

Returns a value expression.
any
required
The value to use in the expression
This can be used to pass in a value where a reference is taken by default. Example:

refTuple

Creates a tuple expression using column references.
Supports 2-5 tuple elements.
ReferenceExpression
required
Column references for the tuple
Example:

tuple

Creates a value tuple expression.
Supports 2-5 tuple elements.
any
required
Values for the tuple
Example:

lit

Returns a literal value expression.
number | boolean | null
required
Literal value (only safe types allowed to prevent SQL injection)
Just like val but creates a literal value that gets merged in the SQL. To prevent SQL injections, only boolean, number and null values are accepted. Example:

unary

Creates a unary expression.
UnaryOperator
required
Unary operator (e.g., not, exists, -)
ReferenceExpression
required
Expression to apply the operator to
Example:

not

Creates a not operation.
A shortcut for unary('not', expr).

exists

Creates an exists operation.
A shortcut for unary('exists', expr).

neg

Creates a negation operation.
A shortcut for unary('-', expr).

between

Creates a between expression.
ReferenceExpression
required
Expression to check
OperandValueExpression
required
Start of range
OperandValueExpression
required
End of range
Example:

betweenSymmetric

Creates a between symmetric expression.
Example:

and

Combines expressions using the logical and operator.
array | object
required
Array of expressions or filter object for equality comparisons
An empty array produces a true expression. Example:
Object notation:

or

Combines expressions using the logical or operator.
array | object
required
Array of expressions or filter object for equality comparisons
An empty array produces a false expression. Example:

parens

Wraps the expression in parentheses.
Example:

cast

Creates a cast(expr as dataType) expression.
ReferenceExpression
required
Expression to cast
DataTypeExpression
required
Target data type
type parameter
required
TypeScript type to cast to (must be provided as type parameter)
Example:

withSchema

Deprecated. Will be removed in kysely 0.25.0.