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)
}
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.