githubEdit

Query Builder

The query builder provides a fluent interface for constructing database queries. It's generated for each model and provides compile-time type safety.

Getting a Query Builder

Access through the repository:

query := client.UserRepo().Query()

Builder Methods (Chainable)

All builder methods return the builder for chaining.

Filter

Add WHERE conditions. Multiple filters are ANDed together:

Query().Where(conditions...)
query.Where(
    filter.User.IsActive.IsTrue(),
    filter.User.Age.GreaterThan(18),
)

Add full-text search conditions. Multiple conditions are ORed (any match):

SearchAll

Add full-text search conditions with AND semantics (all must match):

Order

Sort results by one or more fields:

OrderRandom

Sort results randomly:

Limit

Restrict maximum number of results:

Start

Skip first n results (for pagination):

Fetch

Eager load related records:

Timeout

Set query execution timeout:

Parallel

Enable parallel query execution:

TempFiles

Enable temporary file-based query processing for large result sets:

Note: TempFiles reduces memory usage at the cost of slower performance. Not available with Live queries.

WithDeleted

Include soft-deleted records in results (only for models with som.SoftDelete):

Range

Range query for models with string IDs (ULID, UUID, Rand) or complex IDs (ArrayID, ObjectID):

Range boundary constructors:

Constructor
Description

som.From[T](val)

Inclusive start bound

som.FromExclusive[T](val)

Exclusive start bound

som.FromStart()

Open-ended start (no lower bound)

som.To[T](val)

Exclusive end bound

som.ToInclusive[T](val)

Inclusive end bound

som.ToEnd()

Open-ended end (no upper bound)

String ID example:

Open-ended range:

Complex ID example:

Note: Range() returns a BuilderNoLive — live queries are not supported with range queries.

Execution Methods

These methods execute the query and return results.

All

Get all matching records:

First

Get the first matching record. Returns ErrNotFound if no match:

Count

Get count of matching records:

Exists

Check if any matching records exist:

Live

Subscribe to real-time updates:

AllMatches

Get all search results with metadata (scores, highlights, offsets):

FirstMatch

Get the first search result with metadata:

Iterator Methods

For processing large result sets efficiently, use the iterator methods. These leverage Go's range-over-func feature to stream results in batches.

Iterate

Iterate over all matching records in batches:

IterateID

Iterate over record IDs only (more efficient when you only need IDs):

Early Termination

Iterators support breaking out early:

When to Use Iterators

Scenario
Method

Process all records without loading into memory

Iterate

Need only record IDs

IterateID

Fixed number of results

Limit().All()

Need random access to results

All()

Async Methods

Every execution method has an async variant that returns immediately:

Sync
Async

All(ctx)

AllAsync(ctx)

First(ctx)

FirstAsync(ctx)

Count(ctx)

CountAsync(ctx)

Exists(ctx)

ExistsAsync(ctx)

Live(ctx)

LiveAsync(ctx)

Using Async Methods

Async Result Type

LiveResult Type

SearchResult Type

Returned by AllMatches() and FirstMatch():

Helper Methods

Score Sorting

The query package provides score-based sorting for search queries:

Basic Score Sort

Multiple Refs

Combination Modes

Usage Example

Complete Example

Pagination Helper

Query Reuse

Queries can be built incrementally:

Note: Each execution creates a new query based on the builder state at that point.

Last updated