pgdb

package
v0.4.6 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MPL-2.0-no-copyleft-exception Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func Connect

func Connect(parent context.Context, pgURL string) (*pgx.Conn, error)

Connect establishes a single PostgreSQL connection with a 5-second timeout. For servers, prefer pgxpool.New and pass the pool as DBT.

func Migrate

func Migrate(ctx context.Context, conn *pgx.Conn, migrationsFS fs.FS) ([]rowsync.Message, error)

Migrate runs pending migrations from an fs.FS on the connection. Returns messages about what was applied, or nil if nothing changed.

Types

type DBT

type DBT interface {
	Ping(context.Context) error
	Begin(context.Context) (pgx.Tx, error)
}

DBT is the minimum interface satisfied by both *pgx.Conn and *pgxpool.Pool.

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)
    },
}

func (*Store[T]) Fetch

func (s *Store[T]) Fetch(ctx context.Context) ([]T, error)

Fetch reads all rows from PostgreSQL using FetchFunc.

func (*Store[T]) Init

func (s *Store[T]) Init(ctx context.Context) ([]rowsync.Message, error)

Init pings the connection and optionally runs migrations.

func (*Store[T]) Update

func (s *Store[T]) Update(ctx context.Context, plan rowsync.Plan[T]) error

Update applies a plan within one transaction: insert and update actions upsert Action.After, delete actions pass Action.Before to DeleteFunc.

Jump to

Keyboard shortcuts

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