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

DB
object
required
The database schema type
TB
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.
lhs
ReferenceExpression
required
Left-hand side of the expression (typically a column reference)
op
BinaryOperatorExpression
required
The operator (e.g., =, >, <, +, -, in, like)
rhs
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.
from
TableExpressionOrList
required
Table expression or list of tables to select from
Example:

case

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

ref

Creates a column reference expression.
reference
StringReference
required
Column reference string (e.g., first_name, person.id)
op
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.
table
string
required
Table name
Example:

val

Returns a value expression.
value
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.
value1, value2, ...
ReferenceExpression
required
Column references for the tuple
Example:

tuple

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

lit

Returns a literal value expression.
literal
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.
op
UnaryOperator
required
Unary operator (e.g., not, exists, -)
expr
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.
expr
ReferenceExpression
required
Expression to check
start
OperandValueExpression
required
Start of range
end
OperandValueExpression
required
End of range
Example:

betweenSymmetric

Creates a between symmetric expression.
Example:

and

Combines expressions using the logical and operator.
exprs
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.
exprs
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.
expr
ReferenceExpression
required
Expression to cast
dataType
DataTypeExpression
required
Target data type
T
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.