Skip to content

Bun Connector

If you chose Bun as your SQL layer, this is the provider that opens the connection at startup from your config files and makes it available to the rest of your application — so you add one line to bootstrap/providers.go instead of writing connection code.

It connects using uptrace/bun and optionally registers a GPA provider.

Usage

func LoadProviders() []app.Provider {
    return []app.Provider{
        &bunconnector.Provider{
            UseGPA: true,
        },
    }
}

Configuration

Reads from your application configuration (internal/configs/database.go):

config.Set("sql", config.M{
    "default": "postgres",
    "connections": config.M{
        "postgres": config.M{
            "host":     config.MustEnv("DB_HOST", "127.0.0.1"),
            "port":     config.MustEnv("DB_PORT", 5432),
            "database": config.MustEnv("DB_DATABASE", "lemmego"),
            "username": config.MustEnv("DB_USERNAME", "root"),
            "password": config.MustEnv("DB_PASSWORD", ""),
        },
    },
})

Without GPA

&bunconnector.Provider{
    UseGPA: false,
}

When GPA is disabled, the raw *bun.DB is registered as a service:

db := app.Get[*bun.DB](a)

Supported Drivers

  • PostgreSQL (pgdialect)
  • MySQL (mysqldialect)
  • SQLite (sqlitedialect)

Connection Pooling

config.Set("sql", config.M{
    "connections": config.M{
        "postgres": config.M{
            "host":           "127.0.0.1",
            "max_open_conns": 25,
            "max_idle_conns": 10,
            "conn_max_lifetime": "5m",
        },
    },
})

Differences from GORM Connector

FeatureGORM ConnectorBun Connector
Auto-MigrationYesNo
Supported DriversPostgreSQL, MySQL, SQLite, SQL ServerPostgreSQL, MySQL, SQLite
DialectsGORMBun native
GPA IntegrationYes (gpagorm)Yes (gpabun)