SQL query builder and ORM/Factory generator for Go with support for PostgreSQL, MySQL and SQLite
See the codeBob is a set of Go packages and tools to work with SQL databases.
Bob's philosophy centres around the following:
Bob can be progressively adopted from raw SQL query strings, to fully typed queries with models and factories generated for your database.
Bob consists of several components that build on each other for the full experience.
sqlc.Check out the documentation for more information.
| Queries | Models | ORM Gen | Factory Gen | Query Gen | |
|---|---|---|---|---|---|
| Postgres | ✅ | ✅ | ✅ | ✅ | ✅ |
| MySQL/MariaDB | ✅ | ✅ | ✅ | ✅ | ✅ |
| SQLite | ✅ | ✅ | ✅ | ✅ | ✅ |
This is just a fluent query builder that has no concept of your DB, and by extension cannot offer any type-safety.
The main reason, I consider it better than most alternatives is that since each dialect is hand-crafted, it can support building ANY query for that dialect.
However, each dialect is also independent, so you don't have to worry about creating an invalid query.
IMPORTANT: Queries are built using "Query Mods"
psql.Select(
sm.From("users"), // This is a query mod
sm.Where(psql.Quote("age").GTE(psql.Arg(21))), // This is also a mod
)
This is where the type safety comes.
A full ORM, and query mods that is based on the database schema. If you use the generated query mods, these will ensure correct type safety.
Here is the above query using generated query-mods.
models.Users.Query(
models.SelectWhere.Users.Age.GTE(21), // This is type-safe
)
Factories make testing much much easier. Especially when the test depends on a database entry that depends on relations in other tables (e.g. testing comments that rely on posts which in turn rely on users).
With knowledge of the database schema, Bob can generate factories for each table.
// Quickly create a 10 comments (posts and users are created appropriately)
comments, err := f.NewComment().CreateMany(ctx, db, 10)
I believe this is the final peice of the puzzle, and extends the type-safety to hand-crafted SQL queries.
For example, you could generate code for the query:
-- UserPosts
SELECT * FROM posts WHERE user_id = $1
This will generate a function UserPosts that takes an int32.
// UserPosts
userPosts, err := queries.UserPosts(1).All(ctx, db)
Then, if you need to, you can add an extra filter to get only published posts.
However whether it is type safe or not depends on if you use the generated mods or not:
// Get only published posts
query := psql.Select(
UserPosts(1),
models.PostWhere.Status.EQ("published"), // type-safe
sm.Where(psql.Quote("posts", "status").Eq(psql.Arg("published"))), // not type-safe
)
You can get all the tools you need for developing against this repository with nix. Use nix-shell in the root of the repository to get a shell with all the dependencies and tools for development.
This repository uses golangci-lint for linting. You can run the linter with:
$ golangci-lint run
Before submitting pull requests you should ensure that your changes lint clean:
$ golangci-lint run
0 issues.
This repository uses gofumpt for formatting. It's more strict than go fmt. The linter will fail if you haven't formatted for code correctly. You can format your code with:
$ gofumpt -l -w ./some/file
You can test this repository using go test. A simple test of a single module can be run with:
$ go test ./dialect/psql/
A comprehensive test of all modules could be done with:
$ go test ./...
If you're interested in data race detection and coverage, both of which are done by the Github workflow prior to merging code, you'll need to add a few more arguments:
$ go test -race -covermode atomic --coverprofile=covprofile.out -coverpkg=github.com/stephenafamo/bob/... ./...
The project uses Github Actions as defined in .github/workflows. Pull requests are expected to pass linting and testing workflows before being accepted.
Thanks to all the people who have contributed to Bob!
Overall
Maintained
Code review
Go module
Latest release
Releases this year
(top 24 of 30)
388 followers · starred Sep 2025
219 followers · starred Apr 2026
392 followers · starred Aug 2023
77 followers · starred May 2025
SQL query builder and ORM/Factory generator for Go with support for PostgreSQL, MySQL and SQLite
See the codeBob is a set of Go packages and tools to work with SQL databases.
Bob's philosophy centres around the following:
Bob can be progressively adopted from raw SQL query strings, to fully typed queries with models and factories generated for your database.
Bob consists of several components that build on each other for the full experience.
sqlc.Check out the documentation for more information.
| Queries | Models | ORM Gen | Factory Gen | Query Gen | |
|---|---|---|---|---|---|
| Postgres | ✅ | ✅ | ✅ | ✅ | ✅ |
| MySQL/MariaDB | ✅ | ✅ | ✅ | ✅ | ✅ |
| SQLite | ✅ | ✅ | ✅ | ✅ | ✅ |
This is just a fluent query builder that has no concept of your DB, and by extension cannot offer any type-safety.
The main reason, I consider it better than most alternatives is that since each dialect is hand-crafted, it can support building ANY query for that dialect.
However, each dialect is also independent, so you don't have to worry about creating an invalid query.
IMPORTANT: Queries are built using "Query Mods"
psql.Select(
sm.From("users"), // This is a query mod
sm.Where(psql.Quote("age").GTE(psql.Arg(21))), // This is also a mod
)
This is where the type safety comes.
A full ORM, and query mods that is based on the database schema. If you use the generated query mods, these will ensure correct type safety.
Here is the above query using generated query-mods.
models.Users.Query(
models.SelectWhere.Users.Age.GTE(21), // This is type-safe
)
Factories make testing much much easier. Especially when the test depends on a database entry that depends on relations in other tables (e.g. testing comments that rely on posts which in turn rely on users).
With knowledge of the database schema, Bob can generate factories for each table.
// Quickly create a 10 comments (posts and users are created appropriately)
comments, err := f.NewComment().CreateMany(ctx, db, 10)
I believe this is the final peice of the puzzle, and extends the type-safety to hand-crafted SQL queries.
For example, you could generate code for the query:
-- UserPosts
SELECT * FROM posts WHERE user_id = $1
This will generate a function UserPosts that takes an int32.
// UserPosts
userPosts, err := queries.UserPosts(1).All(ctx, db)
Then, if you need to, you can add an extra filter to get only published posts.
However whether it is type safe or not depends on if you use the generated mods or not:
// Get only published posts
query := psql.Select(
UserPosts(1),
models.PostWhere.Status.EQ("published"), // type-safe
sm.Where(psql.Quote("posts", "status").Eq(psql.Arg("published"))), // not type-safe
)
You can get all the tools you need for developing against this repository with nix. Use nix-shell in the root of the repository to get a shell with all the dependencies and tools for development.
This repository uses golangci-lint for linting. You can run the linter with:
$ golangci-lint run
Before submitting pull requests you should ensure that your changes lint clean:
$ golangci-lint run
0 issues.
This repository uses gofumpt for formatting. It's more strict than go fmt. The linter will fail if you haven't formatted for code correctly. You can format your code with:
$ gofumpt -l -w ./some/file
You can test this repository using go test. A simple test of a single module can be run with:
$ go test ./dialect/psql/
A comprehensive test of all modules could be done with:
$ go test ./...
If you're interested in data race detection and coverage, both of which are done by the Github workflow prior to merging code, you'll need to add a few more arguments:
$ go test -race -covermode atomic --coverprofile=covprofile.out -coverpkg=github.com/stephenafamo/bob/... ./...
The project uses Github Actions as defined in .github/workflows. Pull requests are expected to pass linting and testing workflows before being accepted.
Thanks to all the people who have contributed to Bob!
Overall
Maintained
Code review
Go module
Latest release
Releases this year
(top 24 of 30)
388 followers · starred Sep 2025
219 followers · starred Apr 2026
392 followers · starred Aug 2023
77 followers · starred May 2025