Overview
The Query Builder provides a chainable API for constructing queries with filtering, relation loading, sorting, pagination, and field selection.Basic Usage
Query()
Starts a new query for the given entity.Execute()
Executes the query and returns results.context.Context
required
Context for query execution (supports cancellation and timeouts)
*QueryResult
Query results containing rows and eager-loaded relations
string
Name of the entity queried
[]Row
Main query results (slice of Row maps)
map[string][]Row
Eager-loaded relations (relation name → rows)
Filtering
Filter()
Adds a filter condition to the query.string
required
Field name or relation path (e.g., “email”, “posts.published”)
string
required
Filter operator:
"eq", "neq", "gt", "gte", "lt", "lte", "like", "in"interface{}
required
Value to compare against (string, int, float64, bool)
Examples:
Field Selection
Select()
Specifies which fields to retrieve (partial selection)....string
required
Field names to select (variadic)
- If not called, defaults to
SELECT *(all fields) - Reduces data transfer and improves performance
- Selected fields are available in
result.Rows
Eager Loading
Include()
Eager loads related entities (prevents N+1 queries).string
required
Relation name or nested path (e.g., “posts”, “posts.comments”)
- Loads relations in separate queries
- Results available in
result.Relations["relation_name"] - Supports nested includes with dot notation
- Uses IdentityMap for automatic deduplication
Sorting
OrderBy()
Adds a sort clause to the query.string
required
Field name to sort by
string
required
Sort direction:
"asc" or "desc"Pagination
Limit()
Limits the number of results returned.uint64
required
Maximum number of rows to return
Offset()
Skips the first N results.uint64
required
Number of rows to skip
Debug Mode
Debug()
Enables SQL debug output for this query.- Generated SQL query
- Does not show execution time or row counts (use
DebugTrace()for that)
DebugTrace()
Enables full trace output (SQL + timing + row counts).Advanced Usage
Combining All Features
Working with Results
Query Result Reference
QueryResult
int
Returns the number of rows in the main result
bool
Returns true if no rows were returned
Row
interface{}
Returns the raw value of a field
string
Returns the string value of a field (or "" if not found)
int64
Returns the int64 value of a field (or 0 if not found)
Complete Example
See Also
- Engine - Initialize and configure the engine
- Mutations - Insert, update, and delete data
- Error Handling - Handle query errors