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
eb
Returns a copy of the expression builder for destructuring.fn
Returns aFunctionModule for writing type-safe function calls.
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
case
Creates acase statement/operator.
SimpleReferenceExpression
Optional column or expression to compare in when clauses
ref
Creates a column reference expression.StringReference
required
Column reference string (e.g.,
first_name, person.id)JSONOperatorWith$
JSON path operator (
->$ or ->>$) for JSON traversaljsonPath
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)
table
Creates a table reference.string
required
Table name
val
Returns a value expression.any
required
The value to use in the expression
refTuple
Creates a tuple expression using column references.ReferenceExpression
required
Column references for the tuple
tuple
Creates a value tuple expression.any
required
Values for the tuple
lit
Returns a literal value expression.number | boolean | null
required
Literal value (only safe types allowed to prevent SQL injection)
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
not
Creates anot operation.
unary('not', expr).
exists
Creates anexists operation.
unary('exists', expr).
neg
Creates a negation operation.unary('-', expr).
between
Creates abetween expression.
ReferenceExpression
required
Expression to check
OperandValueExpression
required
Start of range
OperandValueExpression
required
End of range
betweenSymmetric
Creates abetween symmetric expression.
and
Combines expressions using the logicaland operator.
array | object
required
Array of expressions or filter object for equality comparisons
true expression.
Example:
or
Combines expressions using the logicalor operator.
array | object
required
Array of expressions or filter object for equality comparisons
false expression.
Example:
parens
Wraps the expression in parentheses.cast
Creates acast(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)