Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 34 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<p align="center">
<a href="https://skudra.net">Documentation</a> ·
<a href="https://skudra.net/quick-start">Quick start</a> ·
<a href="https://github.com/skudra-go/ant-examples">Examples</a>
<a href="https://skudra.net/choose-your-path">Choose your path</a> ·
<a href="https://skudra.net/examples">Examples</a> ·
<a href="https://skudra.net/blog">Blog</a>
</p>

<p align="center">
Expand Down Expand Up @@ -48,9 +50,9 @@ type AppDB struct {

var Users = ant.Fields[User]()

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

session := ant.Session(db) // one unit of work, such as a request
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
Expand All @@ -75,12 +77,10 @@ Requires Go 1.27 or later, for generic methods.

## Try it in a minute

[examples/hello](https://github.com/skudra-go/ant-examples/tree/main/examples/hello) is one file on SQLite. It needs only Go:
[hello](https://skudra.net/examples#hello) is one file on SQLite. It needs only Go:

```sh
git clone https://github.com/skudra-go/ant-examples
cd ant-examples/examples/hello
go run .
go run github.com/skudra-go/ant-examples/examples/hello@latest
```

```text
Expand All @@ -89,7 +89,7 @@ Alice is 30 and has 2 posts
Happy birthday, Alice
```

The first run takes about 30 seconds, mostly compiling the SQLite driver. The [quick start](https://skudra.net/quick-start) walks through the same program.
The first run takes about 30 seconds, mostly compiling the SQLite driver. To read or change the code, clone [ant-examples](https://github.com/skudra-go/ant-examples) and run `go run .` in `examples/hello`. The [quick start](https://skudra.net/quick-start) walks through the same program.

## Next to GORM, Bun and ent

Expand Down Expand Up @@ -180,7 +180,7 @@ Each cell was tested by compiling the mistake. GORM also has an optional [CLI](h

## Performance

[tools/compare](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%.
[tools/compare](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<br><sub>NoTracking</sub> | GORM | Bun | ent | | Plain SQL<br><sub>baseline</sub> |
| --- | ---: | ---: | ---: | ---: | ---: | - | ---: |
Expand All @@ -192,34 +192,32 @@ Each cell was tested by compiling the mistake. GORM also has an optional [CLI](h
| Update one column | 🟢 **399** | – | 407 | 424 | 🟢 **402** | | 390 |
| Load, edit, save | 🟢 **805** | – | 818 | 🟢 **812** | 1,869 | | 798 |

| KiB per operation | ant | ant<br><sub>NoTracking</sub> | GORM | Bun | ent | | Plain SQL<br><sub>baseline</sub> |
| --- | ---: | ---: | ---: | ---: | ---: | - | ---: |
| Read 1000 rows | 582 | 🟢 **275** | 392 | 295 | 589 | | 257 |
| Find one by key | 3.8 | 🟢 **3.5** | 5.7 | 6.3 | 4.9 | | 1.5 |
| Filter, sort, 10 rows | 11.5 | 🟢 **7.9** | 9.7 | 9.2 | 11.9 | | 3.8 |
| 100 users, 500 posts | 486 | 🟢 **241** | 362 | 277 | 335 | | 257 |
| Insert one | 🟢 **2.5** | – | 7.0 | 6.8 | 3.7 | | 1.3 |
| Update one column | 🟢 **2.5** | – | 5.6 | 5.0 | 3.1 | | 0.3 |
| Load, edit, save | 🟢 **5.8** | – | 11.2 | 11.2 | 10.7 | | 1.8 |

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.
- ant uses the least memory of the four libraries in every row.
- With `NoTracking` where it applies, ant uses the least memory of the four libraries in every operation. [Benchmarks](https://skudra.net/reference/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](tools/compare#reading-them) has the details.

## Features

- **Typed queries:** conditions, joins, projections, aggregates, subqueries, CTEs and window functions, all checked by the compiler
- **Plain structs:** your types are the models, with conventions you can override in `Configure`
- **Relations:** 1:1, 1:N and many-to-many, loaded explicitly with `Include`, in batches. Nothing loads behind your back
- **Change tracking:** load, edit, `SaveChanges`. Only the columns that changed are written
- **Writes:** insert, update, delete, upserts, `MERGE` and bulk writes, with a guard against unfiltered updates and deletes
- **Transactions:** savepoints, isolation levels, retries and read replicas
- **Lifecycle:** soft deletes, timestamps, global filters, hooks, validation and optimistic concurrency
- **Migrations:** plain SQL generated from your structs, with drift checks. No down migrations, and no lock-in
- **Tooling:** a `go vet` analyzer, query logging, metrics, OpenTelemetry, and test fixtures and factories in `anttest`
- [Queries](https://skudra.net/queries/conditions) the compiler checks: conditions, joins, projections, aggregates, subqueries, CTEs and window functions.
- [Relations](https://skudra.net/relations/loading): one-to-one, one-to-many and many-to-many, loaded in batches with `Include`. Nothing loads behind your back.
- [Change tracking](https://skudra.net/tracking/save-changes): load, edit, call `SaveChanges`, and only the changed columns are written.
- [Writes](https://skudra.net/writing/insert): whole graphs in one call, `Upsert`, `MERGE`, `COPY` and bulk updates, and a guard against an `UPDATE` or `DELETE` with no condition.
- [Transactions](https://skudra.net/transactions) with savepoints, isolation levels, retries and read replicas.
- [Lifecycle](https://skudra.net/lifecycle/soft-deletes): soft deletes, timestamps, tenant filters, hooks, validation and optimistic concurrency.
- [Migrations](https://skudra.net/migrations/overview): plain SQL that `diff` writes from your structs and `drift` checks against the database, with no down migrations and no lock-in.
- [Tooling](https://skudra.net/tooling/diagnostics): 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](https://skudra.net/examples) are much bigger than a demo, and each README writes down what didn't fit.

- [listmonk](https://skudra.net/examples#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](https://skudra.net/examples#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](https://skudra.net/examples#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](https://skudra.net/examples#ledger): double-entry bookkeeping for several tenants, with transfers that are safe to retry, balances the database enforces, an outbox and encrypted fields.

## Databases

Expand All @@ -228,7 +226,7 @@ What the numbers say:
| Queries and writes | ✅ | ✅ | ✅ | ✅ | ✅ |
| Migrations | ✅ | | | | |

PostgreSQL gets every feature first. [docs/databases.md](docs/databases.md) lists what each database lacks.
PostgreSQL gets every feature first. [Databases](https://skudra.net/reference/databases) lists what each one lacks and how ant handles the differences.

## What's missing

Expand All @@ -241,9 +239,10 @@ The [roadmap](docs/ROADMAP.md) lists what's planned, and why some items were ski
## Learn more

- [Documentation](https://skudra.net): guides, recipes and reference.
- [Feature examples](tests/features/): a short test for every feature, edge cases included
- [ant-examples](https://github.com/skudra-go/ant-examples): small projects built on ant
- [Roadmap](docs/ROADMAP.md) and [design decisions](docs/decisions/README.md)
- Guides: [migrating from GORM](https://skudra.net/guides/migrating-from-gorm), [ant vs GORM, ent, Bun and sqlc](https://skudra.net/guides/ant-vs-gorm-ent-sqlc), [type-safe SQL in Go](https://skudra.net/guides/type-safe-sql-in-go), [database migrations in Go](https://skudra.net/guides/migrations-in-go), [UUID primary keys](https://skudra.net/guides/uuid-primary-keys) and [using an existing database](https://skudra.net/guides/existing-database).
- [Feature examples](tests/features/): a short test for every feature, edge cases included.
- [Example projects](https://skudra.net/examples): from one file on SQLite to a port of a real application.
- [Roadmap](docs/ROADMAP.md) and [design decisions](docs/decisions/README.md).

## With an AI assistant

Expand Down Expand Up @@ -273,6 +272,8 @@ Thank you to everyone who has filed an issue, fixed a bug or improved the docs:

*Skudra* means *ant* in Latvian, which is where [skudra.net](https://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](LICENSE)
2 changes: 2 additions & 0 deletions ant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Package ant is a Go ORM with plain struct models, compiler-checked queries,
// and change tracking, and no code generation.
//
// The documentation, with guides and a quick start, is at https://skudra.net.
//
// Declare a context, open it, and query through field probes:
//
// type AppDB struct {
Expand Down