postgres

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPostgresSetupCheck is the error returned when the Postgres setup check fails.
	ErrPostgresSetupCheck = errors.New("failed to perform Postgres setup check")
)
View Source
var (
	// ErrSemVer indicates a semver is invalid.
	ErrSemVer = errors.New("invalid semver")
)

Functions

func DecodeAES256Base64

func DecodeAES256Base64(aes256KeyBase64 string) ([32]byte, error)

DecodeAES256Base64 decodes a Base64 encoded AES256 key.

func New

func New(ctx context.Context, config Config) (storage.Storage, *pgxpool.Pool, error)

New creates a new Postgres storage and returns its connection pool.

func NewWithSetup

func NewWithSetup(ctx context.Context, config Config, setupLogger *slog.Logger) (storage.Storage, *pgxpool.Pool, error)

NewWithSetup creates a new Postgres storage and returns its connection pool. It also performs a setup check.

Types

type Config

type Config struct {
	AES256KeyBase64 string                      `json:"aes256KeyBase64"`
	AutoMigrate     bool                        `json:"autoMigrate"`
	DSN             string                      `json:"dsn"`
	Health          *jt.JSONType[time.Duration] `json:"health"`
	InitialTimeout  *jt.JSONType[time.Duration] `json:"initialTimeout"`
	MaxIdle         *jt.JSONType[time.Duration] `json:"maxIdle"`
	MinConns        int32                       `json:"minConns"`
	PlaintextClaims bool                        `json:"plaintextClaims"`
	PlaintextJWK    bool                        `json:"plaintextJWK"`
}

Config is the configuration for Postgres storage.

func (Config) DefaultsAndValidate

func (c Config) DefaultsAndValidate() (Config, error)

DefaultsAndValidate implements the jsontype.Config interface.

type Migrator

type Migrator interface {
	// Migrate applies all migrations in order. It will automatically skip migrations that have .
	Migrate(ctx context.Context) error
}

Migrator is the interface for applying migrations.

Migrator only migrates in the forward direction. It does not support rolling back migrations. Ensure you have quick and robust database backup and restore procedure before running migrations.

No other program should interact with the database while the Migrator is running.

No Migrator implementation should depend on code elsewhere in this project.

func NewMigrator

func NewMigrator(pool *pgxpool.Pool, options MigratorOptions) (Migrator, error)

NewMigrator returns a new Migrator for a Postgres storage implementation.

type MigratorOptions

type MigratorOptions struct {
	EncryptionKey [32]byte
	Logger        *slog.Logger
	SetupCtx      context.Context
}

MigratorOptions are options for creating a Migrator.

type Setup

type Setup struct {
	PlaintextClaims bool   `json:"plaintextClaims,omitempty"`
	PlaintextJWK    bool   `json:"plaintextJWK,omitempty"`
	SemVer          string `json:"semver,omitempty"` // https://pkg.go.dev/golang.org/x/mod/semver
}

Setup is the JSON data that sits in the setup table.

func ReadSetup

func ReadSetup(ctx context.Context, tx pgx.Tx) (Setup, error)

ReadSetup reads the setup information in the database.

type Transaction

type Transaction struct {
	Tx pgx.Tx
}

Transaction is a storage.Tx implementation for Postgres.

func (*Transaction) Commit

func (t *Transaction) Commit(ctx context.Context) error

Commit helps implement the storage.Tx interface.

func (*Transaction) Rollback

func (t *Transaction) Rollback(ctx context.Context) error

Rollback helps implement the storage.Tx interface.

Jump to

Keyboard shortcuts

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