Documentation
¶
Overview ¶
Package sqlboiler provides adapters for integrating SQLBoiler with paging-go.
This package provides a generic Fetcher[T] implementation that works with SQLBoiler-generated models, plus strategy-specific query builders for offset and cursor pagination.
The design separates ORM integration (generic) from pagination strategy (specific), making it easy to:
- Add new pagination strategies without changing the fetcher
- Port to other ORMs (GORM, sqlc, etc.) by implementing Fetcher[T]
Example usage:
// Create fetcher (ORM-specific, strategy-agnostic)
fetcher := sqlboiler.NewFetcher(
func(ctx context.Context, mods ...qm.QueryMod) ([]*models.User, error) {
return models.Users(mods...).All(ctx, db)
},
func(ctx context.Context, mods ...qm.QueryMod) (int64, error) {
return models.Users(mods...).Count(ctx, db)
},
)
// Use with offset pagination
offsetPaginator := offset.NewPaginator(fetcher, ...)
// Or use with cursor pagination (Phase 2)
cursorPaginator := cursor.NewPaginator(fetcher, ...)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CursorToQueryMods ¶
func CursorToQueryMods(params paging.FetchParams) []qm.QueryMod
CursorToQueryMods converts FetchParams into SQLBoiler query mods for cursor-based pagination. This is the strategy-specific query builder for keyset pagination.
The conversion follows these rules:
- Cursor → qm.Where("(col1, col2) OP (?, ?)", val1, val2) using tuple comparison
- Limit → qm.Limit(n)
- OrderBy → qm.OrderBy("col1 DESC, col2 DESC")
This function is used by cursor.Paginator when creating a SQLBoiler fetcher.
Example:
fetcher := sqlboiler.NewFetcher(
queryFunc,
countFunc,
sqlboiler.CursorToQueryMods, // ← Use cursor strategy
)
Requirements:
- PostgreSQL database (for tuple comparison syntax)
- Composite index on sort columns: CREATE INDEX idx ON table(col1 DESC, col2 DESC)
func NewFetcher ¶
func NewFetcher[T any]( queryFunc QueryFunc[T], countFunc CountFunc, queryModsFn func(paging.FetchParams) []qm.QueryMod, ) paging.Fetcher[T]
NewFetcher creates a new SQLBoiler fetcher with a strategy-specific query builder.
Parameters:
- queryFunc: Function that executes SQLBoiler queries with query mods
- countFunc: Function that counts total records with query mods
- queryModsFn: Strategy-specific function to convert FetchParams to QueryMods
Example (offset pagination):
fetcher := sqlboiler.NewFetcher(
func(ctx context.Context, mods ...qm.QueryMod) ([]*models.User, error) {
return models.Users(mods...).All(ctx, db)
},
func(ctx context.Context, mods ...qm.QueryMod) (int64, error) {
return models.Users(mods...).Count(ctx, db)
},
sqlboiler.OffsetToQueryMods, // ← Strategy-specific!
)
func OffsetToQueryMods ¶
func OffsetToQueryMods(params paging.FetchParams) []qm.QueryMod
OffsetToQueryMods converts FetchParams into SQLBoiler query mods for offset pagination. This is the strategy-specific query builder for offset-based pagination.
The conversion follows these rules:
- Offset → qm.Offset(n)
- Limit → qm.Limit(n)
- OrderBy → qm.OrderBy("col1 DESC, col2 ASC")
This function is used by offset.Paginator when creating a SQLBoiler fetcher.
Example:
fetcher := sqlboiler.NewFetcher(
queryFunc,
countFunc,
sqlboiler.OffsetToQueryMods, // ← Use offset strategy
)
Types ¶
type CountFunc ¶
CountFunc executes a SQLBoiler count query. This is ORM-specific but strategy-agnostic.
type Fetcher ¶
type Fetcher[T any] struct { // contains filtered or unexported fields }
Fetcher implements paging.Fetcher[T] for SQLBoiler queries. It's generic and works with any pagination strategy by converting FetchParams into SQLBoiler query mods.
The actual conversion logic is strategy-specific and provided by functions like OffsetToQueryMods() or CursorToQueryMods() (Phase 2).