db

package
v0.0.2-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 11, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package db provides driver-agnostic helpers for opening *sql.DB connections and tuning them for hex applications.

hex/db imports no SQL driver and no migration tooling. Consumers:

  • Blank-import their driver of choice (e.g. modernc.org/sqlite, github.com/lib/pq).
  • Import a companion migration subpackage (hex/db/sqlite, hex/db/postgres) if they use golang-migrate.

This keeps hex/db's dependency footprint tiny and prevents unused drivers from being linked into every binary. See ADR-0004.

Example:

import (
    _ "modernc.org/sqlite"
    "github.com/jordanbrauer/hex/db"
    hexsqlite "github.com/jordanbrauer/hex/db/sqlite"
)

//go:embed migrations/*.sql
var migrations embed.FS

conn, err := db.Open(ctx, db.Config{
    Driver:  "sqlite",
    DSN:     "/var/lib/myapp.db",
    Pragmas: []string{"journal_mode = WAL"},
})
if err != nil { return err }
if err := hexsqlite.Migrate(conn, migrations, "migrations"); err != nil {
    return err
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Open

func Open(ctx context.Context, cfg Config) (*sql.DB, error)

Open opens a connection, applies pragmas and pool tuning, and verifies the connection with PingContext. The returned *sql.DB is the caller's to close.

func Tune

func Tune(ctx context.Context, sqldb *sql.DB, cfg Config) error

Tune applies pragmas and pool settings to an already-opened connection. Useful when a consumer opens *sql.DB themselves (for a special driver setup) but still wants hex's uniform tuning.

Types

type Config

type Config struct {
	// Driver is the sql.Open driver name (e.g. "sqlite", "postgres"). The
	// consumer must blank-import the driver package for this name to
	// resolve at runtime.
	Driver string

	// DSN is the data source name passed to sql.Open. Format is
	// driver-specific.
	DSN string

	// Pragmas are executed once immediately after the connection is opened.
	// The strings are passed through to Exec verbatim, so callers control
	// whether to prefix "PRAGMA ", "SET ", etc.
	Pragmas []string

	// Pool configuration. Zero means "use driver default".
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
	ConnMaxIdleTime time.Duration
}

Config describes how to open a database connection.

Directories

Path Synopsis
Package lua exposes hex/db to Lua scripts via a gopher-lua module named "db".
Package lua exposes hex/db to Lua scripts via a gopher-lua module named "db".
Package postgres runs golang-migrate migrations against a Postgres database.
Package postgres runs golang-migrate migrations against a Postgres database.
Package provider is the default hex/db service provider.
Package provider is the default hex/db service provider.
Package sqlite runs golang-migrate migrations against a SQLite database.
Package sqlite runs golang-migrate migrations against a SQLite database.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL