What is Bun?

Bun is a SQL-first database client for Go. SQL-first means that most SQL queries can be automatically compiled to Bun expressions and Bun expressions look and feel like SQL queries.

The purpose of Bun is to allow writing queries using the good old SQL and to help scanning results into common Go types: structs, maps, slices, and scalars.

How it works

Bun wraps sql.DB to provide query builder and hooks. The original sql.DB is available as db.DB and can be used without any restrictions.

  1. type DB struct {
  2. *sql.DB
  3. ...
  4. }

Bun comes with dialects for each supported database. Bun uses dialects to discover available features when building queries and scanning query results. For example, to connect to a PostgreSQL server, you should use PostgreSQL driver (for example, pgdriverIntroduction - 图1open in new window) and PostgreSQL dialect (pgdialect).

Bun provides fixtures to load initial data and migrations to update database schema. You can also use Bun starter kit to quickly bootstrap an app using those packages.

Why another database client?

So you can elegantly write complex queries:

  1. regionalSales := db.NewSelect().
  2. ColumnExpr("region").
  3. ColumnExpr("SUM(amount) AS total_sales").
  4. TableExpr("orders").
  5. GroupExpr("region")
  6. topRegions := db.NewSelect().
  7. ColumnExpr("region").
  8. TableExpr("regional_sales").
  9. Where("total_sales > (SELECT SUM(total_sales) / 10 FROM regional_sales)")
  10. err := db.NewSelect().
  11. With("regional_sales", regionalSales).
  12. With("top_regions", topRegions).
  13. ColumnExpr("region").
  14. ColumnExpr("product").
  15. ColumnExpr("SUM(quantity) AS product_units").
  16. ColumnExpr("SUM(amount) AS product_sales").
  17. TableExpr("orders").
  18. Where("region IN (SELECT region FROM top_regions)").
  19. GroupExpr("region").
  20. GroupExpr("product").
  21. Scan(ctx)
  1. WITH regional_sales AS (
  2. SELECT region, SUM(amount) AS total_sales
  3. FROM orders
  4. GROUP BY region
  5. ), top_regions AS (
  6. SELECT region
  7. FROM regional_sales
  8. WHERE total_sales > (SELECT SUM(total_sales)/10 FROM regional_sales)
  9. )
  10. SELECT region,
  11. product,
  12. SUM(quantity) AS product_units,
  13. SUM(amount) AS product_sales
  14. FROM orders
  15. WHERE region IN (SELECT region FROM top_regions)
  16. GROUP BY region, product

Why Not …?

GORM

It is usually easier to write complex queries with Bun rather than GORMIntroduction - 图2open in new window. Out of the box, Bun has better integration with database-specific functionality, for example, PostgreSQL arrays. Bun is also faster, partly because Bun is smaller in size and scope.

Bun does not support such popular GORM features like automatic migrations (you can try fixtures instead), optimizer/index/comment hints, and database resolver.

Ent

With Bun you can use your previous experience working with SQL DBMS and Go to write fast and idiomatic code. Bun’s goal is to help you write SQL, not replace or hide it.

With EntIntroduction - 图3open in new window your previous experience does not mean as much and can be even misleading because Ent provides a new/different way to write Go apps and you don’t have much choice but to follow it.

go-pg

Bun is a rewrite of go-pg that works on top of sql.DB instead of using custom API. As a consequence, Bun is slightly less efficient than go-pg but works with different databases.

Eventually Bun will replace go-pg. To migrate an existing go-pg app to Bun, see this guide.