Help us shape the future of Go! Take the 2026 Go Developer Survey

sqlxmod

package module
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: Apache-2.0 Imports: 4 Imported by: 1

README

sqlxmod

sqlxmod wraps jmoiron/sqlx as a module and closes the connection pool gracefully when the application exits.

package main

import (
	"fmt"
	"net/http"
	"os"

	"github.com/XSAM/otelsql"
	"github.com/go-srvc/mods/httpmod"
	"github.com/go-srvc/mods/sigmod"
	"github.com/go-srvc/mods/sqlxmod"
	"github.com/go-srvc/srvc"
	semconv "go.opentelemetry.io/otel/semconv/v1.30.0"

	_ "github.com/jackc/pgx/v5/stdlib"
)

func main() {
	db := sqlxmod.New(
		sqlxmod.WithOtel("pgx", os.Getenv("DSN"),
			otelsql.WithAttributes(semconv.DBSystemNamePostgreSQL),
		),
	)
	srvc.RunAndExit(
		sigmod.New(os.Interrupt),
		db,
		httpmod.New(
			httpmod.WithAddr(":8080"),
			httpmod.WithHandler(handler(db)),
		),
	)
}

func handler(db *sqlxmod.DB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		var version string
		if err := db.DB().GetContext(r.Context(), &version, "SELECT version()"); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		fmt.Fprintf(w, "db version: %s\n", version)
	})
}

Documentation

Overview

Package sqlxmod wraps https://pkg.go.dev/github.com/jmoiron/sqlx as a module.

Index

Examples

Constants

View Source
const (
	ErrDBNotSet     = errStr("db not set")
	ErrFailedOpenDB = errStr("failed to open db")
)
View Source
const ID = "sqlxmod"

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Opt) *DB

New creates new sqlx module with given options. Remember to import required driver for your database.

Example
package main

import (
	"github.com/XSAM/otelsql"
	"github.com/go-srvc/mods/sqlxmod"
	"github.com/go-srvc/srvc"

	semconv "go.opentelemetry.io/otel/semconv/v1.30.0"
)

func main() {
	srvc.RunAndExit(
		sqlxmod.New(
			sqlxmod.WithOtel("postgres", "user=foo dbname=bar sslmode=disable",
				otelsql.WithAttributes(semconv.DBSystemNamePostgreSQL),
			),
		),
	)
}

func (*DB) DB

func (d *DB) DB() *sqlx.DB

DB returns the underlying *sqlx.DB. Only valid after Init has run.

func (*DB) ID

func (d *DB) ID() string

func (*DB) Init

func (d *DB) Init() error

func (*DB) Run

func (d *DB) Run() error

func (*DB) Stop

func (d *DB) Stop() error

type Opt

type Opt func(*DB) error

func WithDB

func WithDB(db *sql.DB, driver string) Opt

WithDB creates *sqlx.DB from *sql.DB.

func WithDBx

func WithDBx(dbx *sqlx.DB) Opt

WithDBx sets *sqlx.DB for module.

func WithDBxFn

func WithDBxFn(fn func() (*sqlx.DB, error)) Opt

WithDBxFn sets *sqlx.DB using value returned from fn.

func WithDSN

func WithDSN(driver, dsn string) Opt

WithDSN opens a *sqlx.DB from the given driver name and DSN.

func WithOtel

func WithOtel(driver, dsn string, opts ...otelsql.Option) Opt

WithOtel registers *sqlx.DB with OpenTelemetry.

Jump to

Keyboard shortcuts

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