API Reference
Core ExoQuery API types and functions
Core Types
SqlQuery<T>- Represents a SQL query that returns a result of type TSqlExpression<T>- Represents a SQL expression of type TSqlAction<T, R>- Represents a SQL action (INSERT, UPDATE, DELETE) that operates on type T and returns RSqlCompiledQuery<T>- A compiled query ready to be executedSqlCompiledAction<T, R>- A compiled action ready to be executed
Main Functions
sql { ... }- Create a SQL query or actionsql.select { ... }- Create a SQL select query with joins and groupingsql.expression { ... }- Create a SQL expressionTable<T>()- Create a table referenceparam(value)- Pass a runtime parameter to a queryparams(values)- Pass multiple runtime parameters (for IN clauses)
Query Operations
Transformation
.map { ... }- Project columns (SELECT).flatMap { ... }- Monadic composition for complex queries (see Basic Operations)
Filtering
.filter { ... }- Filter rows (WHERE).where { ... }- Filter rows (WHERE), alternative syntax
Aggregate Functions
Aggregate functions are used inside sql.select blocks, typically with groupBy.
| Function | Input Type | Return Type | SQL |
|---|---|---|---|
count() | - | Int | COUNT(*) |
count(field) | T | Int | COUNT(field) |
countDistinct(field) | T | Int | COUNT(DISTINCT field) |
sum(field) | Int | Long | SUM(field) |
sum(field) | Long | Long | SUM(field) |
sum(field) | Double | Double | SUM(field) |
avg(field) | Numeric | Double | AVG(field) |
min(field) | T | T | MIN(field) |
max(field) | T | T | MAX(field) |
Important: When defining result data classes for aggregations, match these types exactly:
// CORRECT
@Serializable
data class Stats(
val totalCount: Int, // count() returns Int
val distinctUsers: Int, // countDistinct() returns Int
val totalRevenue: Long, // sum(Long) returns Long
val avgOrderValue: Double // avg() returns Double
)
// WRONG - will cause compile errors
@Serializable
data class Stats(
val totalCount: Long, // โ count() returns Int, not Long
val distinctUsers: Long, // โ countDistinct() returns Int
...
)
Sorting
.sortedBy { ... }- Sort results ascending (ORDER BY ASC).sortedByDescending { ... }- Sort results descending (ORDER BY DESC)
Pagination
.limit(n)- Limit number of results (LIMIT).take(n)- Alias for.limit(n).offset(n)- Skip rows (OFFSET).drop(n)- Alias for.offset(n).distinct()- Remove duplicate rows (SELECT DISTINCT)
Set Operations
.union(query)- Combine queries, remove duplicates (UNION).unionAll(query)- Combine queries, keep duplicates (UNION ALL)
Subquery Helpers
.nested()- Force subquery boundary, prevent automatic flattening.value()- Convert single-rowSqlQuery<T>to scalarTfor use in expressions.isEmpty()- Check if subquery returns no rows (NOT EXISTS).isNotEmpty()- Check if subquery returns rows (EXISTS)
Conditional Expressions
if (cond) a else b- Translates to CASE WHEN ... THEN ... ELSE ... ENDwhen { ... }- Translates to multi-branch CASE expressiona ?: b- Translates to COALESCE(a, b)
See Basic Operations for examples.
Build and Execute
.buildFor.Postgres()- Build query for PostgreSQL.buildFor.Sqlite()- Build query for SQLite.buildFor.SqlServer()- Build query for SQL Server.buildFor.MySql()- Build query for MySQL.runOn(controller)- Execute the query on a database controller