postgres

package
v0.16.1 Latest Latest
Warning

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

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

Documentation

Overview

Package postgres provides a ready Postgres connection pool for dim: Resource opens it with pg.Open, checks it is reachable and closes it on shutdown.

Usage

// in an app initializer (app.Initializer[Deps]):
c.DB, cleanup = dim.NewResource("Store", postgres.Resource(pg.Config{
    User:           opts.DB.User,
    Password:       opts.DB.Password,
    Host:           opts.DB.Host,
    Name:           opts.DB.Name,
    Metrics:        c.Metrics,
    TracerProvider: c.TracerProvider,
}))
return cleanup, nil

The pool is opened on first use of c.DB. pg.Config is passed through unchanged: see the dbx/pg package for its fields.

Example

The pool is a resource of the dependencies of a service: opened and checked on first use, closed by the cleanup at shutdown. The example needs Postgres, so go test builds it but does not run it.

package main

import (
	"context"
	"log/slog"

	"github.com/assanoff/skit/closer"
	"github.com/assanoff/skit/dbx"
	"github.com/assanoff/skit/dbx/pg"
	"github.com/assanoff/skit/dim"
	"github.com/assanoff/skit/metrics"
	"github.com/assanoff/skit/provider/postgres"
)

func main() {
	ctx := context.Background()
	log := slog.Default()
	db, cleanup := dim.NewResource("Store", postgres.Resource(pg.Config{
		User:     "shop",
		Password: "secret",
		Host:     "localhost:5432",
		Name:     "shop",
		Metrics:  metrics.New(),
	}))
	closer.Add(cleanup)

	if err := dbx.StatusCheck(ctx, db(ctx)); err != nil {
		log.ErrorContext(ctx, "status", "err", err)
	}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Resource

func Resource(cfg pg.Config) func(ctx context.Context) (*dbx.DB, dim.CleanupFunc, error)

Resource returns a dim factory that opens a connection pool from cfg (pg.Open) and verifies it (dbx.StatusCheck). The cleanup closes the pool.

Types

This section is empty.

Jump to

Keyboard shortcuts

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