v0.3.1 · pre-1.0 · Go 1.27+
A typed ORM for Go.
Plain structs, queries the compiler checks, and no code generation. Built on database/sql, for PostgreSQL, MySQL, MariaDB, SQLite and SQL Server.
go get github.com/skudra-go/antOverview
Columns are field pointers, not strings, so the compiler checks them. Renaming a field renames it in every query.
models.go· User, Post, AppDB
type User struct {
ant.Base // ID, CreatedAt, UpdatedAt
Name string
Age int
Posts []Post // 1:N through Post.UserID
}
type Post struct {
ant.Base
UserID int64
Title string
}
type AppDB struct {
ant.Context
Users ant.Table[User]
Posts ant.Table[Post]
}
var Users = ant.Fields[User]() // the probe: &Users.Age names the age column
db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn)
adults, err := db.Users.
Where(ant.Gt(&Users.Age, 18)). // ant.Gt(&Users.Age, "18") won't compile
Include(&Users.Posts). // one query for all the posts
All(ctx)
// SELECT ... FROM "users" WHERE "users"."age" > $1
// SELECT ... FROM "posts" WHERE "posts"."user_id" IN ($1, $2, ...)
adults[0].Age++
err = db.SaveChanges(ctx)
// UPDATE "users" SET "age" = $1, "updated_at" = $2 WHERE "users"."id" = $3
Users.Agee undefined (type *User has no field or method Agee)
See it
Next to GORM, Bun and ent
What the compiler catches in the query on Why ant.
| Mistake | ant | GORM | Bun | ent | sqlc |
|---|---|---|---|---|---|
| Misspelled column | Compile error: your editor and go build flag it | Not caught: it compiles and fails at runtime | Not caught: it compiles and fails at runtime | Compile error: your editor and go build flag it | Caught by sqlc generate, before the build |
"18" for an int | Compile error: your editor and go build flag it | Not caught: it compiles and fails at runtime | Not caught: it compiles and fails at runtime | Compile error: your editor and go build flag it | Compile error: your editor and go build flag it |
| Misspelled relation | Compile error: your editor and go build flag it | Not caught: it compiles and fails at runtime | Not caught: it compiles and fails at runtime | Compile error: your editor and go build flag it | Caught by sqlc generate, before the build |
| No code generation step | Nothing to generate | Nothing to generate | Nothing to generate | Needs go generate after a schema change | Needs sqlc generate after a query or schema change |
Compile error, shown in your editor Caught by a separate step: sqlc generate, or antvet for ant Compiles and fails at runtime Needs go generate or sqlc generate
Single-row queries cost about the same in every library. Change tracking makes large reads slower, and NoTracking turns it off for read-only queries. Benchmarks has the numbers.
Features
Install
go get github.com/skudra-go/ant
go get github.com/jackc/pgx/v5/stdlib # or the driver for your database
ant needs Go 1.27 or later, because it uses generic methods. It is pre-1.0, so the API can still change; each release lists its breaking changes.
Databases
| PostgreSQL | MySQL | MariaDB | SQLite | SQL Server | |
|---|---|---|---|---|---|
| Queries, writes, relations, tracking, transactions | ✓ | ✓ | ✓ | ✓ | ✓ |
| Migrations | ✓ |
PostgreSQL gets every feature first. Databases lists the gaps and how ant handles them.
Feedback
ant is pre-1.0, and the API is still open to change. If something reads awkwardly, is missing, or doesn't work with your database, open an issue. Questions and ideas go in Discussions.