scope

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Applier

type Applier interface {
	ApplyWhere(clause string, args []any)
	ApplyOrderBy(clause string)
	ApplyLimit(n int)
	ApplyOffset(n int)
	ApplySelect(columns string)
	ApplyJoin(name string)
	ApplyLeftJoin(name string)
	ApplyPreload(name string)
}

Applier is implemented by query builders to receive scope fragments. This interface lives in the scope package so that orm can import scope without creating circular dependencies.

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

Scope represents a single query condition fragment. Scopes are immutable and safe to reuse across queries.

func In

func In[T any](column string, values []T) Scope

In returns a WHERE scope with an IN clause, expanding the slice into individual placeholders. No reflection is used; generics handle the type conversion.

scope.In("id", []int{1, 2, 3})  // → WHERE id IN (?, ?, ?)

func Join added in v0.0.15

func Join(name string) Scope

Join returns a Scope that adds an INNER JOIN for the named relation.

func LeftJoin added in v0.0.15

func LeftJoin(name string) Scope

LeftJoin returns a Scope that adds a LEFT JOIN for the named relation.

func Limit

func Limit(n int) Scope

Limit returns a Scope that sets the LIMIT.

func Offset

func Offset(n int) Scope

Offset returns a Scope that sets the OFFSET.

func OrderBy

func OrderBy(clause string) Scope

OrderBy returns a Scope that sets the ORDER BY clause.

scope.OrderBy("created_at DESC")

func Preload added in v0.0.15

func Preload(name string) Scope

Preload returns a Scope that registers a relation for eager loading.

func Select

func Select(columns ...string) Scope

Select returns a Scope that overrides the SELECT column list.

scope.Select("id", "name")

func Where

func Where(clause string, args ...any) Scope

Where returns a Scope that adds a WHERE clause fragment.

scope.Where("age > ?", 18)
scope.Where("name = ? AND role = ?", "alice", "admin")

func (Scope) Apply

func (s Scope) Apply(a Applier)

Apply dispatches this Scope to the given Applier.

type Scopes

type Scopes []Scope

Scopes is a named slice of Scope, useful for conditionally building up a set of scopes.

var s scope.Scopes
if onlyActive {
    s = s.Append(Active)
}
s = s.Append(Paginate(page, perPage))
Users(db).Scopes(s...).All(ctx)

func Combine

func Combine(scopes ...Scope) Scopes

Combine creates a Scopes from the given scopes.

scope.Combine(scope.Limit(10), scope.Offset(20))

func (Scopes) Append

func (ss Scopes) Append(scopes ...Scope) Scopes

Append adds scopes and returns a new Scopes. The receiver is not modified.

func (Scopes) Merge

func (ss Scopes) Merge(other Scopes) Scopes

Merge concatenates two Scopes and returns a new Scopes. Neither receiver nor argument is modified.

Jump to

Keyboard shortcuts

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