Skip to main content

Concepts

Models are plain structs​

Each struct is a table, and each field a column named after the field. There is no generated code and no schema file. Embedding ant.Base adds ID, CreatedAt and UpdatedAt, but it's optional: Book below declares its own ID.

type Book struct {
ID int64
Title string
Pages int
}

type LibraryDB struct {
ant.Context
Books ant.Table[Book]
}

Book is the table books, with id as its generated primary key. See defining models for names and overrides.

A probe names a column​

ant.Fields[T]() returns a probe: a *T that never holds data. &Books.Pages is a field pointer. ant reads it as the pages column. The compiler sees a *int, so the value compared with it must be an int.

var Books = ant.Fields[Book]()

books, err := db.Books.
Where(ant.Gt(&Books.Pages, 100)).
All(ctx)
// SELECT ... FROM "books" WHERE "books"."pages" > $1

See probes.

The context is your database​

Declare a struct with an ant.Table[T] for each model. ant.Open fills it in. Tables, transactions and sessions hand you back the same struct type, so db.Users works the same everywhere.

type AppDB struct {
ant.Context
Users ant.Table[User]
Posts ant.Table[Post]
}

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn)

Open reads every model, so a model it can't map fails at startup rather than on first use.

Errors arrive at the last call​

Builder methods such as Where and OrderBy never return an error. The call that runs the query returns any mistakes ant collected. That call takes a context.Context first.

query := db.Users.
Where(ant.Gt(&Users.Age, 18)).
OrderBy(&Users.Name)

users, err := query.All(ctx) // runs here, and returns any error here
// SELECT ... FROM "users" WHERE "users"."age" > $1 ORDER BY "users"."name"

These calls run something: All, First, Single, Find, Count, Exists, Each, Insert, Update, Delete, Exec and SaveChanges. Everything before them describes the query.

No implicit queries​

ant never runs a query on field access. An unloaded relation is nil. A loaded relation with no rows is an empty slice. Include adds one query per relation, however many parents.

user, err := db.Users.Find(ctx, id)

fmt.Println(user.Posts == nil) // true: not loaded, and no query ran

user, err = db.Users.Include(&Users.Posts).Find(ctx, id)
// SELECT ... FROM "users" WHERE "users"."id" = $1 LIMIT $2
// SELECT ... FROM "posts" WHERE "posts"."user_id" IN ($1)

fmt.Println(user.Posts == nil) // false: loaded, even if there are none

Saving changes​

Entities that All, First, Single, Find and Include return are tracked: ant keeps a copy of each one, and SaveChanges writes only the columns that differ, in one transaction. A tracker belongs to one unit of work, so a server creates an ant.Session per request. NoTracking on a query, or ant.WithTracking(false) on Open, turns tracking off for read-only work.

session := ant.Session(db)

user, err := session.Users.Find(ctx, id)
if err != nil {
return err
}

user.Age++

err = session.SaveChanges(ctx)
// UPDATE "users" SET "age" = $1, "updated_at" = $2 WHERE "users"."id" = $3

See SaveChanges and Sessions.

What the compiler cannot catch​

These compile, and ant returns the error when the query runs. antvet reports the ones marked ✓ during go vet.

MistakeExampleErrorantvet
A field pointer from another model's probedb.Posts.Where(ant.Gt(&Users.Age, 18))ant.ErrWrongModel✓
A pointer not taken from a probeant.Gt(&age, 18)ant.ErrNotProbe✓
A key of the wrong typedb.Users.Find(ctx, "1")ant.ErrKeyType✓
A relation used as a columndb.Posts.Where(ant.IsNull(&Posts.User))ant.ErrNotColumn–
A column used as a relationdb.Users.IncludeOne(&Users.Bio)ant.ErrNotRelation–