github.com/go-srvc/mods/sqlmod
go get github.com/go-srvc/mods/sqlmod@v0.1.5
README
sqlmod
sqlmod takes care of gracefully closing connection pool when application exits.
package main
import (
"context"
"os"
"github.com/XSAM/otelsql"
"github.com/go-srvc/mods/sqlmod"
"github.com/go-srvc/srvc"
semconv "go.opentelemetry.io/otel/semconv/v1.30.0"
_ "github.com/jackc/pgx/v5/stdlib"
)
func main() {
srvc.RunAndExit(
New(),
)
}
// Store embeds the sqlmod.DB
type Store struct {
*sqlmod.DB
}
func New() *Store {
db := sqlmod.New(
sqlmod.WithOtel("pgx", os.Getenv("DSN"),
otelsql.WithAttributes(semconv.DBSystemNamePostgreSQL),
),
)
return &Store{DB: db}
}
func (s *Store) Healthy(ctx context.Context) error {
return s.DB.DB().PingContext(ctx)
}
Overview
Package sqlmod wraps database/sql as a module.
Constants
const (
ErrDBNotSet = errStr("db not set")
ErrFailedOpenDB = errStr("failed to open db")
)const ID = "sqlmod"Types
type DB
type DB struct {
// contains filtered or unexported fields
}func (*DB) DB
func (d *DB) DB() *sql.DBDB returns *sql.DB instance. This should be only call after Init.
func (*DB) ID
func (d *DB) ID() stringfunc (*DB) Init
func (d *DB) Init() errorfunc (*DB) Run
func (d *DB) Run() errorfunc (*DB) Stop
func (d *DB) Stop() errortype Opt
type Opt func(*DB) errorExamples
ExampleNew
package main
import (
"github.com/XSAM/otelsql"
"github.com/go-srvc/mods/sqlmod"
"github.com/go-srvc/srvc"
semconv "go.opentelemetry.io/otel/semconv/v1.30.0"
)
func main() {
srvc.RunAndExit(
sqlmod.New(
sqlmod.WithOtel("postgres", "user=foo dbname=bar sslmode=disable",
otelsql.WithAttributes(semconv.DBSystemNamePostgreSQL),
),
),
)
}