Documentation
¶
Overview ¶
Package pgdb provides a generic PostgreSQL database adapter. It can serve as both a source (Fetcher) and destination (Updater), enabling pipelines like gsheet -> pg or pg -> tsv.
Store.DB accepts either *pgx.Conn (CLI, tests) or *pgxpool.Pool (servers). Import "github.com/jackc/pgx/v5/pgxpool" if using a pool.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Store ¶
type Store[T any] struct { DB DBT Migrations fs.FS // optional; if set, Init runs migrations FetchFunc func(ctx context.Context) ([]T, error) UpsertFunc func(ctx context.Context, tx pgx.Tx, row T) error DeleteFunc func(ctx context.Context, tx pgx.Tx, row T) error // required when a plan carries deletes Normalize func([]T) }
Store is a generic PostgreSQL Fetcher and Updater. The application provides FetchFunc, UpsertFunc, and optionally DeleteFunc to define the SQL. Capture the connection or pool in the function closures:
store := &dbpg.Store[MyRow]{
DB: pool,
FetchFunc: func(ctx context.Context) ([]MyRow, error) {
return queries.MyRowList(ctx)
},
UpsertFunc: func(ctx context.Context, tx pgx.Tx, row MyRow) error {
return queries.New(tx).MyRowUpsert(ctx, row)
},
DeleteFunc: func(ctx context.Context, tx pgx.Tx, row MyRow) error {
return queries.New(tx).MyRowDelete(ctx, row.ID)
},
}