Skip to content

Latest commit

 

History

History

pginit

pginit

import "github.com/induzo/gocom/database/pginit/v2"

This package allows you to init a connection pool to postgres database via pgx

Index

func ConnPoolHealthCheck

func ConnPoolHealthCheck(pool *pgxpool.Pool) func(ctx context.Context) error

ConnPoolHealthCheck returns a health check function for pgxpool.Pool that can be used in health endpoint.

Example

Using standard net/http package. We can also simply pass healthCheck as a CheckFn in gocom/http/health/v2.

{
	pgi, err := pginit.New("postgres://postgres:postgres@localhost:5432/datawarehouse?sslmode=disable&pool_max_conns=10&pool_max_conn_lifetime=1m")
	if err != nil {
		log.Fatalf("init pgi config: %v", err)
	}

	ctx := context.Background()

	pool, err := pgi.ConnPool(ctx)
	if err != nil {
		log.Fatalf("init pgi config: %v", err)
	}

	defer pool.Close()

	healthCheck := pginit.ConnPoolHealthCheck(pool)

	mux := http.NewServeMux()

	mux.HandleFunc("/sys/health", func(rw http.ResponseWriter, req *http.Request) {
		if err := healthCheck(ctx); err != nil {
			rw.WriteHeader(http.StatusServiceUnavailable)
		}
	})
}

func JSONRowToAddrOfStruct

func JSONRowToAddrOfStruct[T any](row pgx.CollectableRow) (*T, error)

type Option

Option configures PGInit behaviour.

type Option func(*PGInit)

func WithDecimalType

func WithDecimalType() Option

WithDecimalType set pgx decimal type to shopspring/decimal.

func WithGoogleUUIDType

func WithGoogleUUIDType() Option

WithGoogleUUIDType set pgx uuid type to google/uuid.

func WithLogger

func WithLogger(logger *slog.Logger, _ string) Option

WithLogger Add logger to pgx. if the request context contains request id, can pass in the request id context key to reqIDKeyFromCtx and logger will log with the request id.

func WithTracer

func WithTracer(opts ...otelpgx.Option) Option

WithTracer Add tracer to pgx.

func WithUUIDType

func WithUUIDType() Option

WithUUIDType set pgx uuid type to gofrs/uuid.

type PGInit

PGInit provides capabilities for connect to postgres with pgx.pool.

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

func New

func New(connString string, opts ...Option) (*PGInit, error)

New initializes a PGInit using the provided Config and options. If opts is not provided it will initializes PGInit with default configuration.

func (*PGInit) ConnPool

func (pgi *PGInit) ConnPool(ctx context.Context) (*pgxpool.Pool, error)

ConnPool initiates connection to database and return a pgxpool.Pool.

Example

{
	pgi, err := pginit.New("postgres://postgres:postgres@localhost:5432/datawarehouse?sslmode=disable&pool_max_conns=10&pool_max_conn_lifetime=1m")
	if err != nil {
		log.Fatalf("init pgi config: %v", err)
	}

	ctx := context.Background()

	pool, err := pgi.ConnPool(ctx)
	if err != nil {
		log.Fatalf("init pgi config: %v", err)
	}

	defer pool.Close()

	if err := pool.Ping(ctx); err != nil {
		log.Fatalf("ping: %v", err)
	}
}

Example (Withlogger)

{
	textHandler := slog.NewTextHandler(io.Discard, nil)
	logger := slog.New(textHandler)

	pgi, err := pginit.New(
		"postgres://postgres:postgres@localhost:5432/datawarehouse?sslmode=disable&pool_max_conns=10&pool_max_conn_lifetime=1m",
		pginit.WithLogger(logger, "request-id"),
		pginit.WithDecimalType(),
		pginit.WithUUIDType(),
	)
	if err != nil {
		log.Fatalf("init pgi config: %v", err)
	}

	ctx := context.Background()

	pool, err := pgi.ConnPool(ctx)
	if err != nil {
		log.Fatalf("init pgi config: %v", err)
	}

	defer pool.Close()

	if err := pool.Ping(ctx); err != nil {
		log.Fatalf("ping: %v", err)
	}
}

Generated by gomarkdoc