Skip to main content

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.
Example:

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
QueryResult fields:
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)
Supported operators: Examples:

Field Selection

Select()

Specifies which fields to retrieve (partial selection).
...string
required
Field names to select (variadic)
Behavior:
  • If not called, defaults to SELECT * (all fields)
  • Reduces data transfer and improves performance
  • Selected fields are available in result.Rows
Examples:

Eager Loading

Include()

Eager loads related entities (prevents N+1 queries).
string
required
Relation name or nested path (e.g., “posts”, “posts.comments”)
Behavior:
  • Loads relations in separate queries
  • Results available in result.Relations["relation_name"]
  • Supports nested includes with dot notation
  • Uses IdentityMap for automatic deduplication
Examples:

Sorting

OrderBy()

Adds a sort clause to the query.
string
required
Field name to sort by
string
required
Sort direction: "asc" or "desc"
Examples:

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
Examples:

Debug Mode

Debug()

Enables SQL debug output for this query.
Output:
  • Generated SQL query
  • Does not show execution time or row counts (use DebugTrace() for that)
Example:

DebugTrace()

Enables full trace output (SQL + timing + row counts).
Example:

Advanced Usage

Combining All Features

Working with Results

Query Result Reference

QueryResult

Methods:
int
Returns the number of rows in the main result
bool
Returns true if no rows were returned

Row

Methods:
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