repository

package
v0.1.0-preview.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package repository provides bounded, typed database/sql query primitives for application-owned and generated repositories.

Index

Constants

View Source
const (
	// DefaultMaxRows is the recommended list bound for generated repositories.
	DefaultMaxRows = 1_000
	// MaxRows is the largest list bound accepted by NewQuery.
	MaxRows = 100_000
)

Variables

View Source
var (
	// ErrNotFound indicates that a required single-row query returned no rows.
	ErrNotFound = errors.New("repository query returned no rows")
	// ErrMultipleRows indicates that a single-row query returned more than one row.
	ErrMultipleRows = errors.New("repository query returned multiple rows")
	// ErrRowLimitExceeded indicates that a list query produced more than its
	// declared in-memory result bound.
	ErrRowLimitExceeded = errors.New("repository query row limit exceeded")
)

Functions

This section is empty.

Types

type Decoder

type Decoder[T any] func(Scanner) (T, error)

Decoder maps the current database row to T without reflection.

type Query

type Query[T any] struct {
	// contains filtered or unexported fields
}

Query is an immutable, typed repository query safe for concurrent use when its decoder is safe for concurrent use.

func NewQuery

func NewQuery[T any](spec QuerySpec[T]) (*Query[T], error)

NewQuery validates and freezes a repository query definition.

func (*Query[T]) ID

func (query *Query[T]) ID() string

ID returns the stable compiler- or application-owned operation identity.

func (*Query[T]) List

func (query *Query[T]) List(
	ctx context.Context,
	executor data.Executor,
	args ...any,
) ([]T, error)

List returns rows in driver order. It rejects the result before decoding any row beyond MaxRows. Callers should also bound work in SQL so the database does not produce an unnecessarily large result.

func (*Query[T]) Module

func (query *Query[T]) Module() string

Module returns the owning application module identity.

func (*Query[T]) One

func (query *Query[T]) One(
	ctx context.Context,
	executor data.Executor,
	args ...any,
) (T, error)

One returns exactly one row. Zero and multiple rows are errors identifiable with errors.Is.

func (*Query[T]) Optional

func (query *Query[T]) Optional(
	ctx context.Context,
	executor data.Executor,
	args ...any,
) (value T, found bool, err error)

Optional returns found=false when no row exists and rejects multiple rows.

type QuerySpec

type QuerySpec[T any] struct {
	ID        string
	Module    string
	Statement string
	MaxRows   int
	Decode    Decoder[T]
}

QuerySpec describes one immutable repository query. Statement syntax and placeholders remain owned by the selected database dialect.

type Scanner

type Scanner interface {
	Scan(...any) error
}

Scanner is implemented by *sql.Row and *sql.Rows. Decoders should call Scan exactly once and return a newly owned value.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL