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
The database schema type
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.Left-hand side of the expression (typically a column reference)
The operator (e.g.,
=, >, <, +, -, in, like)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.Table expression or list of tables to select from
case
Creates acase statement/operator.
Optional column or expression to compare in when clauses
ref
Creates a column reference expression.Column reference string (e.g.,
first_name, person.id)JSON path operator (
->$ or ->>$) for JSON traversaljsonPath
Creates a JSON path expression with the provided column as root document.Column reference to use as JSON path root (passed as type parameter)
table
Creates a table reference.Table name
val
Returns a value expression.The value to use in the expression
refTuple
Creates a tuple expression using column references.Column references for the tuple
tuple
Creates a value tuple expression.Values for the tuple
lit
Returns a literal value expression.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.Unary operator (e.g.,
not, exists, -)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.
Expression to check
Start of range
End of range
betweenSymmetric
Creates abetween symmetric expression.
and
Combines expressions using the logicaland operator.
Array of expressions or filter object for equality comparisons
true expression.
Example:
or
Combines expressions using the logicalor operator.
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.
Expression to cast
Target data type
TypeScript type to cast to (must be provided as type parameter)