Documentation
¶
Overview ¶
Package fn is the ORM's expressions over Vuka field references, typed: each takes field references (Post.Title, Post.Author.Name) or other terms and gives an orm.Term of the Go type the database computes, so only the comparisons that make sense type-check.
Post.Objects.Filter(fn.Lower(Post.Title).Eq("go"), fn.Year(Post.Published).Ge(2025))
Post.Objects.Annotate(fn.Count(Post.Tags).As("tags")).OrderBy(fn.Length(Post.Title).Desc())
Post.Objects.Update(ctx, orm.AssignF(Post.Views, fn.Num(Post.Views).Add(1)))
The names are the engine's (orm.Count, orm.Lower, …), which take field names as strings; this package is their Vuka form.
Deprecated: Vuka now writes a model's field as its typed field (PostFields.Title), whose Expr and Name the engine's functions take: orm.Lower(Post.Title.Expr()). Package fn goes in the next minor release.
Index ¶
- func Abs[T any, V Number](a orm.Arg[T, V]) orm.OrderedTerm[T, V]
- func Call[R, T, V any](f interface{ ... }, a orm.Arg[T, V], rest ...any) orm.Term[T, R]
- func Cast[R, T, V any](a orm.Arg[T, V], t orm.CastType) orm.Term[T, R]
- func Coalesce[T, V any](a orm.Arg[T, V], rest ...any) orm.Term[T, V]
- func CosineDistance[T any](field orm.Arg[T, orm.Vector], v orm.Vector) orm.OrderedTerm[T, float64]
- func Date[T, V any](a orm.Arg[T, V]) orm.Term[T, string]
- func Day[T, V any](a orm.Arg[T, V]) orm.OrderedTerm[T, int64]
- func Exists[T, R any](q orm.Query[R]) vuka.Pred[T]
- func Fuse[T any](orders ...orm.TermOrder[T]) orm.OrderedTerm[T, float64]
- func Headline[T any, V ~string](a orm.Arg[T, V], q orm.TextQuery) orm.StringTerm[T, string]
- func In[T, R, V, W any](a orm.Arg[T, V], q orm.Query[R], col orm.Arg[R, W]) vuka.Pred[T]
- func InnerProduct[T any](field orm.Arg[T, orm.Vector], v orm.Vector) orm.OrderedTerm[T, float64]
- func L2Distance[T any](field orm.Arg[T, orm.Vector], v orm.Vector) orm.OrderedTerm[T, float64]
- func Length[T any, V ~string](a orm.Arg[T, V]) orm.OrderedTerm[T, int64]
- func Lit[T, V any](v V) orm.Term[T, V]
- func Lower[T any, V ~string](a orm.Arg[T, V]) orm.StringTerm[T, V]
- func Match[T any](d orm.SearchDoc[T], q orm.TextQuery) vuka.Pred[T]
- func Month[T, V any](a orm.Arg[T, V]) orm.OrderedTerm[T, int64]
- func Num[T any, V cmp.Ordered](a orm.Arg[T, V]) orm.OrderedTerm[T, V]
- func OuterEq[T, U, V, W any](inner orm.Arg[T, V], outer orm.Arg[U, W]) vuka.Pred[T]
- func OuterRef[U, W any](outer orm.Arg[U, W]) orm.Expr
- func SQL[R, T, V any](template string, a orm.Arg[T, V], rest ...any) orm.Term[T, R]
- func SearchRank[T any](d orm.SearchDoc[T], q orm.TextQuery) orm.OrderedTerm[T, float64]
- func SearchVector[T any](fields ...orm.Path[T]) orm.SearchDoc[T]
- func Similarity[T any, V ~string](a orm.Arg[T, V], term string) orm.OrderedTerm[T, float64]
- func Str[T any, V ~string](a orm.Arg[T, V]) orm.StringTerm[T, V]
- func Subquery[T, R, V any](q orm.Query[R], col orm.Arg[R, V]) orm.Term[T, V]
- func Time[T any](a orm.Arg[T, time.Time]) orm.CompareTerm[T, time.Time]
- func Trim[T any, V ~string](a orm.Arg[T, V]) orm.StringTerm[T, V]
- func Upper[T any, V ~string](a orm.Arg[T, V]) orm.StringTerm[T, V]
- func Val[T, V any](a orm.Arg[T, V]) orm.Term[T, V]
- func Year[T, V any](a orm.Arg[T, V]) orm.OrderedTerm[T, int64]
- type Agg
- type Branch
- type CaseTerm
- type FilteredRel
- type Number
- type SwitchTerm
- type Switcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Call ¶
func Call[R, T, V any](f interface{ Of(args ...any) orm.Expr }, a orm.Arg[T, V], rest ...any) orm.Term[T, R]
Call is a database function — orm.Function's, or a typed orm.Func's — called on field references and values, as a term of T typed R:
var unaccent = orm.Function("unaccent")
fn.Str(fn.Call[string](unaccent, Post.Title)).IContains("jose")
func Cast ¶
Cast converts to the database type t (orm.AsInt, orm.AsText, …); R is the Go type it reads as: fn.Cast[int64](Post.Code, orm.AsInt).
func Coalesce ¶
Coalesce is the first of its arguments that isn't NULL; the term is of the first's type.
func CosineDistance ¶
func Exists ¶
Exists holds for the rows of T another model's query has a row for (with OuterEq, a related row).
func Fuse ¶
Fuse is reciprocal-rank fusion of orderings by named terms (hybrid search): order by it descending.
rank := fn.SearchRank(doc, q).As("rank")
dist := fn.CosineDistance(Doc.Embedding, v).As("dist")
Doc.Objects.Annotate(rank, dist, fn.Fuse(rank.Desc(), dist.Asc()).As("score")).OrderBy(...)
func In ¶
In holds where a field of T is among a column of another model's query (the types may differ: a key and an orm.FK to its row).
func InnerProduct ¶
func L2Distance ¶
L2Distance, CosineDistance and InnerProduct are a vector field's distance to v.
func Num ¶
Num, Str, Val and Time make a field reference (or term) a term, for arithmetic and comparisons with other fields: fn.Num(Post.Views).Add(1), fn.Num(Post.Views).GtF(Post.Likes).
func OuterEq ¶
OuterEq is the predicate of a subquery that its field equals a field of the query it stands in, Django's OuterRef: Post.Objects.Filter(fn.OuterEq(Post.Author, User.ID)).
func OuterRef ¶
OuterRef is a field of the query a subquery stands in, as an expression for templates and functions.
func SQL ¶
SQL is a template ({0}, {1}, …) of field references, terms and values, typed R: the escape hatch for what has no function.
func SearchRank ¶
SearchRank is how well the document matches q.
func SearchVector ¶
SearchVector is fields as a document: orm.Search's.
func Similarity ¶
Similarity is the trigram similarity of a field's text and term.
func Subquery ¶
Subquery is a column of another model's query as a term of T: its first row's (order and limit the query).
latest := Post.Objects.Filter(fn.OuterEq(Post.Author, User.ID)).OrderBy(Post.Published.Desc()).Limit(1)
User.Objects.Annotate(fn.Subquery[User](latest, Post.Title).As("latest"))
Types ¶
type Agg ¶
type Agg[T any, V cmp.Ordered] struct { orm.OrderedTerm[T, V] // contains filtered or unexported fields }
Agg is an aggregate term: Filter and Distinct give its variants.
func Count ¶
Count, Sum, Avg, Min and Max aggregate a field: of the rows, of related rows through a relation (fn.Count(Post.Tags) counts tags).
type Branch ¶
type Branch[T, V any] struct { // contains filtered or unexported fields }
Branch is a Case's: a predicate and the result where it holds.
type CaseTerm ¶
CaseTerm is a Case: the result of its first branch whose predicate holds, else Else's, else NULL.
type FilteredRel ¶
type FilteredRel[T, R any] struct { // contains filtered or unexported fields }
FilteredRel is a relation whose rows are only those a predicate holds for, Django's FilteredRelation. Name it with As, Annotate it, then read through it with Field and Count.
func FilteredRelation ¶
FilteredRelation is rel (a relation of T to R) with the rows p holds for.
g := fn.FilteredRelation(Post.Tags, Tag.Name.StartsWith("g")).As("gtags")
Post.Objects.Annotate(g, g.Count().As("n"))
func (FilteredRel[T, R]) Annotation ¶
func (f FilteredRel[T, R]) Annotation() orm.Term[T, R]
Annotation makes it something Annotate takes.
func (FilteredRel[T, R]) As ¶
func (f FilteredRel[T, R]) As(name string) FilteredRel[T, R]
As names the relation.
func (FilteredRel[T, R]) Count ¶
func (f FilteredRel[T, R]) Count() Agg[T, int64]
Count counts the related rows.
type Number ¶
type Number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64
}
Number is what Sum and arithmetic take.
type SwitchTerm ¶
SwitchTerm is a Switch: a term of its results' type R.
func (SwitchTerm[T, V, R]) Case ¶
func (s SwitchTerm[T, V, R]) Case(v V, then R) SwitchTerm[T, V, R]
Case adds a branch; Else gives the result where no value equals.
func (SwitchTerm[T, V, R]) Else ¶
func (s SwitchTerm[T, V, R]) Else(r R) orm.Term[T, R]
type Switcher ¶
type Switcher[T, V any] struct { // contains filtered or unexported fields }
Switcher is a Switch before its first Case.
func Switch ¶
Switch compares a field or term with values, Case by Case:
fn.Switch(Post.Status).Case("paid", 1).Case("refunded", -1).Else(0)
func (Switcher[T, V]) Case ¶
func (s Switcher[T, V]) Case[R any](v V, then R) SwitchTerm[T, V, R]
Case is the first branch: then, of type R, where the switch equals v.