MCPcopy
hub / github.com/uptrace/bun

github.com/uptrace/bun @v1.2.18 sqlite

repository ↗ · DeepWiki ↗ · release v1.2.18 ↗
2,593 symbols 9,736 edges 184 files 718 documented · 28%
README

Bun: SQL-first Golang ORM

build workflow PkgGoDev Documentation Chat Gurubase

Lightweight, SQL-first Golang ORM for PostgreSQL, MySQL, MSSQL, SQLite, and Oracle

Bun is a modern ORM that embraces SQL rather than hiding it. Write complex queries in Go with type safety, powerful scanning capabilities, and database-agnostic code that works across multiple SQL databases.

✨ Key Features

  • SQL-first approach - Write elegant, readable queries that feel like SQL
  • Multi-database support - PostgreSQL, MySQL/MariaDB, MSSQL, SQLite, and Oracle
  • Type-safe operations - Leverage Go's static typing for compile-time safety
  • Flexible scanning - Query results into structs, maps, scalars, or slices
  • Performance optimized - Built on database/sql with minimal overhead
  • Rich relationships - Define complex table relationships with struct tags
  • Production ready - Migrations, fixtures, soft deletes, and OpenTelemetry support

🚀 Quick Start

go get github.com/uptrace/bun

Basic Example

package main

import (
    "context"
    "database/sql"
    "fmt"

    "github.com/uptrace/bun"
    "github.com/uptrace/bun/dialect/sqlitedialect"
    "github.com/uptrace/bun/driver/sqliteshim"
)

func main() {
    ctx := context.Background()

    // Open database
    sqldb, err := sql.Open(sqliteshim.ShimName, "file::memory:")
    if err != nil {
        panic(err)
    }

    // Create Bun instance
    db := bun.NewDB(sqldb, sqlitedialect.New())

    // Define model
    type User struct {
        ID   int64  `bun:",pk,autoincrement"`
        Name string `bun:",notnull"`
    }

    // Create table
    db.NewCreateTable().Model((*User)(nil)).Exec(ctx)

    // Insert user
    user := &User{Name: "John Doe"}
    db.NewInsert().Model(user).Exec(ctx)

    // Query user
    err = db.NewSelect().Model(user).Where("id = ?", user.ID).Scan(ctx)
    fmt.Printf("User: %+v\n", user)
}

🎯 Why Choose Bun?

Elegant Complex Queries

Write sophisticated queries that remain readable and maintainable:

regionalSales := db.NewSelect().
    ColumnExpr("region").
    ColumnExpr("SUM(amount) AS total_sales").
    TableExpr("orders").
    GroupExpr("region")

topRegions := db.NewSelect().
    ColumnExpr("region").
    TableExpr("regional_sales").
    Where("total_sales > (SELECT SUM(total_sales) / 10 FROM regional_sales)")

var results []struct {
    Region       string `bun:"region"`
    Product      string `bun:"product"`
    ProductUnits int    `bun:"product_units"`
    ProductSales int    `bun:"product_sales"`
}

err := db.NewSelect().
    With("regional_sales", regionalSales).
    With("top_regions", topRegions).
    ColumnExpr("region, product").
    ColumnExpr("SUM(quantity) AS product_units").
    ColumnExpr("SUM(amount) AS product_sales").
    TableExpr("orders").
    Where("region IN (SELECT region FROM top_regions)").
    GroupExpr("region, product").
    Scan(ctx, &results)

Flexible Result Scanning

Scan query results into various Go types:

// Into structs
var users []User
db.NewSelect().Model(&users).Scan(ctx)

// Into maps
var userMaps []map[string]interface{}
db.NewSelect().Table("users").Scan(ctx, &userMaps)

// Into scalars
var count int
db.NewSelect().Table("users").ColumnExpr("COUNT(*)").Scan(ctx, &count)

// Into individual variables
var id int64
var name string
db.NewSelect().Table("users").Column("id", "name").Limit(1).Scan(ctx, &id, &name)

📊 Database Support

Database Driver Dialect
PostgreSQL github.com/uptrace/bun/driver/pgdriver pgdialect.New()
MySQL/MariaDB github.com/go-sql-driver/mysql mysqldialect.New()
SQLite github.com/uptrace/bun/driver/sqliteshim sqlitedialect.New()
SQL Server github.com/denisenkom/go-mssqldb mssqldialect.New()
Oracle github.com/sijms/go-ora/v2 oracledialect.New()

🔧 Advanced Features

Table Relationships

Define complex relationships with struct tags:

type User struct {
    ID      int64   `bun:",pk,autoincrement"`
    Name    string  `bun:",notnull"`
    Posts   []Post  `bun:"rel:has-many,join:id=user_id"`
    Profile Profile `bun:"rel:has-one,join:id=user_id"`
}

type Post struct {
    ID     int64 `bun:",pk,autoincrement"`
    Title  string
    UserID int64
    User   *User `bun:"rel:belongs-to,join:user_id=id"`
}

// Load users with their posts
var users []User
err := db.NewSelect().
    Model(&users).
    Relation("Posts").
    Scan(ctx)

Bulk Operations

Efficient bulk operations for large datasets:

// Bulk insert
users := []User{{Name: "John"}, {Name: "Jane"}, {Name: "Bob"}}
_, err := db.NewInsert().Model(&users).Exec(ctx)

// Bulk update with CTE
_, err = db.NewUpdate().
    Model(&users).
    Set("updated_at = NOW()").
    Where("active = ?", true).
    Exec(ctx)

// Bulk delete
_, err = db.NewDelete().
    Model((*User)(nil)).
    Where("created_at < ?", time.Now().AddDate(-1, 0, 0)).
    Exec(ctx)

Migrations

Version your database schema:

import "github.com/uptrace/bun/migrate"

migrations := migrate.NewMigrations()

migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
    _, err := db.NewCreateTable().Model((*User)(nil)).Exec(ctx)
    return err
}, func(ctx context.Context, db *bun.DB) error {
    _, err := db.NewDropTable().Model((*User)(nil)).Exec(ctx)
    return err
})

migrator := migrate.NewMigrator(db, migrations)
err := migrator.Init(ctx)
err = migrator.Up(ctx)

📈 Monitoring & Observability

Debug Queries

Enable query logging for development:

import "github.com/uptrace/bun/extra/bundebug"

db.AddQueryHook(bundebug.NewQueryHook(
    bundebug.WithVerbose(true),
))

OpenTelemetry Integration

Production-ready observability with distributed tracing:

import "github.com/uptrace/bun/extra/bunotel"

db.AddQueryHook(bunotel.NewQueryHook(
    bunotel.WithDBName("myapp"),
))

Monitoring made easy: Bun is brought to you by ⭐ uptrace/uptrace. Uptrace is an open-source APM tool that supports distributed tracing, metrics, and logs. You can use it to monitor applications and set up automatic alerts to receive notifications via email, Slack, Telegram, and others.

See OpenTelemetry example which demonstrates how you can use Uptrace to monitor Bun.

📚 Documentation & Resources

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details on how to get started.

Thanks to all our contributors:

Contributors

🔗 Related Projects


Star ⭐ this repo if you find Bun useful!

Join our community on Discord • Follow updates on GitHub

Extension points exported contracts — how you extend this code

QueryHook (Interface)
QueryHook allows observing queries before and after execution. [6 implementers]
hook.go
Operation (Interface)
Operation encapsulates the request to change a database definition and knowns which operation can revert it. It is usef [15 …
migrate/operations.go
NamedArgAppender (Interface)
------------------------------------------------------------------------------ [7 implementers]
schema/querygen.go
BeforeAppendModelHook (Interface)
------------------------------------------------------------------------------ [6 implementers]
schema/hook.go
QueryAppender (Interface)
QueryAppender is implemented by types that can append themselves to a SQL query. [33 implementers]
schema/sqlfmt.go
IConn (Interface)
IConn is a common interface for *sql.DB, *sql.Conn, and *sql.Tx. [3 implementers]
query_base.go
BeforeSelectHook (Interface)
BeforeSelectHook is invoked before executing SELECT queries. [2 implementers]
bun.go
Inspector (Interface)
Inspector reads schema state. [2 implementers]
migrate/sqlschema/inspector.go

Core symbols most depended-on inside this repo

Model
called by 476
query_merge.go
NewSelect
called by 229
query_base.go
Exec
called by 198
db.go
NewInsert
called by 152
query_base.go
Where
called by 122
query_base.go
Elem
called by 86
dialect/pgdialect/array_parser.go
ColumnExpr
called by 84
query_select.go
Dialect
called by 79
query_base.go

Shape

Method 1,393
Function 767
Struct 327
Interface 46
FuncType 35
TypeAlias 25

Languages

Go100%

Modules by API surface

query_base.go148 symbols
db.go111 symbols
query_select.go103 symbols
internal/dbtest/db_test.go98 symbols
query_update.go61 symbols
internal/dbtest/migrate_test.go57 symbols
driver/pgdriver/driver.go57 symbols
query_insert.go45 symbols
query_delete.go44 symbols
schema/table.go43 symbols
internal/dbtest/pg_test.go43 symbols
driver/pgdriver/proto.go42 symbols

Dependencies from manifests, versioned

filippo.io/edwards25519v1.2.0 · 1×
github.com/bradleyjkemp/cupaloyv2.3.0+incompatible · 1×
github.com/cespare/xxhash/v2v2.3.0 · 1×
github.com/cpuguy83/go-md2man/v2v2.0.7 · 1×
github.com/go-logr/logrv1.4.3 · 1×

Datastores touched

postgresDatabase · 1 repos
(mysql)Database · 1 repos
dbnameDatabase · 1 repos
testDatabaseDatabase · 1 repos
uptraceDatabase · 1 repos

For agents

$ claude mcp add bun \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact