Skip to content
skudra-goPublic

About

🐜 Typed Go ORM: plain structs, compiler-checked queries, change tracking and migrations, with no code generation. PostgreSQL, MySQL, MariaDB, SQLite, SQL Server.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

66 stars

Watchers

2 watching

Forks

Latest commit

Β 

History

130 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ant

A typed ORM for Go. Plain structs, compiler-checked queries, no code generation.

Documentation Β· Quick start Β· Choose your path Β· Examples Β· Blog

CI Go Reference Go 1.27+ MIT license


Columns are field pointers, not strings. Write ant.Gt(&Users.Age, 18) and the compiler checks both the column and the value. Rename a field and every query follows.

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]()

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn) // shared: reads and inserts, no tracking

session := ant.Session(db) // one per request: it tracks what it loads, for SaveChanges
adults, err := session.Users.
    Where(ant.Gt(&Users.Age, 18)). // ant.Gt(&Users.Age, "18") won't compile
    Include(&Users.Posts).         // one batched query, not one per user
    All(ctx)

adults[0].Age++
err = session.SaveChanges(ctx) // writes only the columns that changed

See it

ant-showcase.mp4

Install

go get github.com/skudra-go/ant
go get github.com/jackc/pgx/v5/stdlib  # or the driver for your database

Requires Go 1.27 or later, for generic methods.

Try it in a minute

hello is one file on SQLite. It needs only Go:

go run github.com/skudra-go/ant-examples/examples/hello@latest
Created Alice
Alice is 30 and has 2 posts
Happy birthday, Alice

The first run takes about 30 seconds, mostly compiling the SQLite driver. To read or change the code, clone ant-examples and run go run . in examples/hello. The quick start walks through the same program.

Next to GORM, Bun and ent

The same query in each: users over 18 with their posts, sorted by name, 10 at a time. A test checks that all four return the same rows.

ant: checked, with nothing generated

users, err := db.Users.
	Include(&Users.Posts).
	Where(ant.Gt(&Users.Age, 18)).
	OrderBy(&Users.Name).
	Limit(10).
	All(ctx)

GORM 1.31: strings, so a typo fails at run time

var users []User

err := db.WithContext(ctx).
	Preload("Posts").
	Where("age > ?", 18).
	Order("name").
	Limit(10).
	Find(&users).
	Error

Bun 1.3: strings, so a typo fails at run time

var users []User

err := db.NewSelect().
	Model(&users).
	Relation("Posts").
	Where("age > ?", 18).
	Order("name").
	Limit(10).
	Scan(ctx)

ent 0.14: checked, through generated code

users, err := client.User.Query().
	WithPosts().
	Where(user.AgeGT(18)).
	Order(user.ByName()).
	Limit(10).
	All(ctx)

What the compiler catches

Mistake ant GORM Bun ent sqlc
Misspelled column, agee βœ… ❌ ❌ βœ… ⚠️
"18" for an int βœ… ❌ ❌ βœ… βœ…
Misspelled relation, Psots βœ… ❌ ❌ βœ… ⚠️
Column of another model ⚠️ ❌ ❌ βœ… ⚠️
No code generation step βœ… βœ… βœ… – –

βœ… compile error Β  ⚠️ caught by a separate step (sqlc generate, or antvet for ant) Β  ❌ compiles, fails at run time Β  – needs a generate step

Each cell was tested by compiling the mistake. GORM also has an optional CLI that generates typed field helpers.

Performance

tools/compare runs the same operations in each library against PostgreSQL 17 on one machine (Ryzen 5 3600, Docker on WSL2). Each number is the median of 10 runs, and each run repeats the operation hundreds to thousands of times. The database round trip is included, which is why small queries are close. Commits don't wait for the disk, so writes measure the library and not the drive. Plain SQL (a hand-written query and Scan, what sqlc generates) is the baseline. Lower is better; 🟒 is the best in each row, shared by any within 1%. Time per operation, in microseconds:

Β΅s per operation ant ant
NoTracking
GORM Bun ent Plain SQL
baseline
Read 1000 rows 2,075 🟒 1,692 3,361 2,370 2,240 1,692
Find one by key 442 397 405 🟒 384 398 398
Filter, sort, 10 rows 557 🟒 539 568 🟒 535 556 535
100 users, 500 posts 2,238 🟒 1,774 2,731 2,030 2,031 1,581
Insert one 410 – 420 1,112 🟒 404 392
Update one column 🟒 399 – 407 424 🟒 402 390
Load, edit, save 🟒 805 – 818 🟒 812 1,869 798

What the numbers say:

  • Small queries cost about the same in every library.
  • Change tracking costs on large reads: 1000 rows take 1.2 times as long and twice the memory, because ant keeps a copy of each row to find changes. With NoTracking, ant is level with plain SQL here.
  • With NoTracking where it applies, ant uses the least memory of the four libraries in every operation. Benchmarks has the memory and allocation tables.
  • Bun's insert is slow by default (it prepares every insert as a new statement), and ent's update reads the row back. tools/compare has the details.

Features

  • Queries the compiler checks: conditions, joins, projections, aggregates, subqueries, CTEs and window functions.
  • Relations: one-to-one, one-to-many and many-to-many, loaded in batches with Include. Nothing loads behind your back.
  • Change tracking: load, edit, call SaveChanges, and only the changed columns are written.
  • Writes: whole graphs in one call, Upsert, MERGE, COPY and bulk updates, and a guard against an UPDATE or DELETE with no condition.
  • Transactions with savepoints, isolation levels, retries and read replicas.
  • Lifecycle: soft deletes, timestamps, tenant filters, hooks, validation and optimistic concurrency.
  • Migrations: plain SQL that diff writes from your structs and drift checks against the database, with no down migrations and no lock-in.
  • Tooling: N+1 warnings, log/slog logging, a go vet analyzer, metrics, OpenTelemetry, and test helpers in anttest.

In real code

Four of the example projects are much bigger than a demo, and each README writes down what didn't fit.

  • listmonk: part of the data layer of listmonk, a newsletter manager in Go, ported to ant on the schema its own installer creates. Each ported query is checked against listmonk's SQL on the same rows.
  • pagila: ant adopted on the Pagila sample database. The models were written by hand and checked with drift, then adopt and baseline started the history without touching the data.
  • azugo: a web service with a UUID key on every model from one embedded struct, validation errors returned as 422, and cursor paging over a nullable column.
  • ledger: double-entry bookkeeping for several tenants, with transfers that are safe to retry, balances the database enforces, an outbox and encrypted fields.

Databases

PostgreSQL MySQL MariaDB SQLite SQL Server
Queries and writes βœ… βœ… βœ… βœ… βœ…
Migrations βœ…

PostgreSQL gets every feature first. Databases lists what each one lacks and how ant handles the differences.

What's missing

  • Migrations are PostgreSQL only. On MySQL, MariaDB, SQLite and SQL Server, ant reads and writes, and you manage the schema with your own tool or plain SQL.
  • The full test suite runs on PostgreSQL only. The other databases run a smaller one, so expect more rough edges there.
  • ant is pre-1.0, so the API can still change. Each release lists its breaking changes.

The roadmap lists what's planned, and why some items were skipped.

Learn more

With an AI assistant

ant is newer than most models' training data, so an assistant left to guess writes GORM. Give it the docs:

  • Claude Code: /plugin marketplace add skudra-go/ant, then /plugin install ant@ant. The skill loads whenever a task touches ant.
  • Codex, Cursor, Copilot, Gemini CLI and other agents: npx skills add skudra-go/ant installs the same skill.
  • Any assistant: point it at skudra.net/llms.txt, the docs as Markdown, or llms-full.txt, all of them in one file.

To have every assistant on a project pick it up, add a line to its AGENTS.md or CLAUDE.md:

This project uses the ant ORM (github.com/skudra-go/ant), not GORM. Before writing ant code, read https://skudra.net/llms.txt.

Contributing

Issues and pull requests are welcome. Read CONTRIBUTING.md first. Report vulnerabilities as SECURITY.md describes.

Thank you to everyone who has filed an issue, fixed a bug or improved the docs:

Contributors

About

Skudra means ant in Latvian, which is where skudra.net gets its name :)

The feature list started from the catalogs of Entity Framework Core, Laravel's Eloquent and the Go libraries above, so Include and SaveChanges will look familiar to anyone who has used EF Core.

License

MIT

About

🐜 Typed Go ORM: plain structs, compiler-checked queries, change tracking and migrations, with no code generation. PostgreSQL, MySQL, MariaDB, SQLite, SQL Server.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

66 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages