Help us shape the future of Go! Take the 2026 Go Developer Survey

relation

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package relation provides explicit loaded states for typed model relations. Reading a relation value never performs database I/O.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Link[M, P any] struct {
	Model M
	Pivot P
}

Link keeps each related model beside the concrete pivot model for that edge. A target shared by several parents never acquires another parent's pivot data.

type Many

type Many[M any] struct {
	// contains filtered or unexported fields
}

Many distinguishes not loaded from a loaded collection, including an empty collection. Its private slice is copied at construction and retrieval.

func Collection

func Collection[M any](items []M) Many[M]

Collection constructs a loaded relation with a shallow copy of items, including an empty collection.

func FoundryCollection

func FoundryCollection[M any](seal sqlowner.CollectionSeal, items []M) Many[M]

FoundryCollection publishes a caller-independent collection for framework loaders. An invalid capability leaves the relation unloaded.

func (Many[M]) All

func (r Many[M]) All() iter.Seq2[int, M]

All yields each loaded item by value without copying the collection. Nothing is yielded when the relation is not loaded.

func (Many[M]) Get

func (r Many[M]) Get() ([]M, bool)

Get returns a caller-owned copy and whether the collection was loaded. Use Len and All to read a large collection without copying it.

func (Many[M]) IsLoaded

func (r Many[M]) IsLoaded() bool

IsLoaded reports whether the collection was loaded, including a loaded empty collection.

func (Many[M]) Len

func (r Many[M]) Len() int

Len reports the number of loaded items; it is zero when not loaded.

type One

type One[M any] struct {
	// contains filtered or unexported fields
}

One distinguishes not loaded, loaded empty and loaded with a model. Its private pointer permits self-relations without an infinitely sized Go struct.

func Single

func Single[M any](model value.Optional[M]) One[M]

Single constructs a loaded singular relation, including an absent model.

func (One[M]) Get

func (r One[M]) Get() (value.Optional[M], bool)

Get returns the optional related model and whether the relation was loaded. A loaded but absent model differs from a relation that has not been loaded.

func (One[M]) IsLoaded

func (r One[M]) IsLoaded() bool

IsLoaded reports whether the singular relation was loaded, including a loaded empty relation.

type Through

type Through[M, P any] struct {
	// contains filtered or unexported fields
}

Through is a loaded many-to-many collection with typed pivot data. Distinct pivot rows remain distinct links even when they refer to the same target.

func FoundryLinked

func FoundryLinked[M, P any](seal sqlowner.CollectionSeal, items []Link[M, P]) Through[M, P]

FoundryLinked publishes a caller-independent link collection for framework loaders. Only framework loaders can issue a valid seal; applications use Linked.

func Linked

func Linked[M, P any](items []Link[M, P]) Through[M, P]

Linked constructs a loaded collection, including a loaded empty collection.

func (Through[M, P]) All

func (r Through[M, P]) All() iter.Seq2[int, Link[M, P]]

All yields each loaded link by value without copying the collection.

func (Through[M, P]) Get

func (r Through[M, P]) Get() ([]Link[M, P], bool)

Get returns a copied link collection and whether it was loaded. Model fields inside each link retain ordinary Go value-copy semantics.

func (Through[M, P]) IsLoaded

func (r Through[M, P]) IsLoaded() bool

IsLoaded reports whether the pivot collection was loaded, including a loaded empty collection.

func (Through[M, P]) Len

func (r Through[M, P]) Len() int

Len reports the number of loaded links; it is zero when not loaded.

type Value

type Value[V any] struct {
	// contains filtered or unexported fields
}

Value is a loaded aggregate slot. V describes SQL nullability independently of loaded state: for example Value[value.Nullable[decimal.Decimal]].

func Computed

func Computed[V any](v V) Value[V]

Computed constructs a loaded aggregate, including zero or a SQL NULL value.

func (Value[V]) Get

func (v Value[V]) Get() (V, bool)

Get returns the aggregate and whether it was loaded; an unloaded slot returns the zero V and false.

func (Value[V]) IsLoaded

func (v Value[V]) IsLoaded() bool

IsLoaded reports whether the aggregate slot was populated.

Jump to

Keyboard shortcuts

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