connection

package
v0.0.0-...-dac6614 Latest Latest
Warning

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

Go to latest
Published: May 3, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package connection provides a multi-profile PostgreSQL connection registry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateConfigJSON

func GenerateConfigJSON(t *target.Target) ([]byte, error)

GenerateConfigJSON derives a CompatConfig from a Target and returns its JSON representation. It creates a "primary" profile from Target.Primary and an optional "replica" profile from Target.Replica.

Types

type CompatConfig

type CompatConfig struct {
	Profiles []CompatProfile `json:"profiles"`
	Active   string          `json:"active"`
}

CompatConfig is the JSON-serializable form of a multi-profile connection config.

type CompatProfile

type CompatProfile struct {
	Name   string        `json:"name"`
	Config ProfileConfig `json:"config"`
}

CompatProfile is the JSON-serializable form of a single profile.

type Profile

type Profile struct {
	Name   string        `json:"name"`
	Config ProfileConfig `json:"config"`
	Pool   *pgxpool.Pool `json:"-"`
}

Profile represents a named database connection profile.

type ProfileConfig

type ProfileConfig struct {
	Host     string `json:"host" yaml:"host"`
	Port     int    `json:"port" yaml:"port"`
	Database string `json:"database" yaml:"database"`
	SSLMode  string `json:"sslmode" yaml:"sslmode"`
	User     string `json:"user" yaml:"user"`
	Password string `json:"-" yaml:"-"`
	PoolMin  int32  `json:"pool_min" yaml:"pool_min"`
	PoolMax  int32  `json:"pool_max" yaml:"pool_max"`
}

ProfileConfig holds the connection parameters for a PostgreSQL profile.

func (ProfileConfig) DSN

func (c ProfileConfig) DSN() string

DSN returns the PostgreSQL connection string for this config.

type ProfileInfo

type ProfileInfo struct {
	Name      string `json:"name"`
	Host      string `json:"host"`
	Port      int    `json:"port"`
	Database  string `json:"database"`
	SSLMode   string `json:"sslmode"`
	User      string `json:"user"`
	Connected bool   `json:"connected"`
}

ProfileInfo is a serializable representation of a profile (no pool, no password).

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages multiple named PostgreSQL connection profiles.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty connection registry.

func (*Registry) Active

func (r *Registry) Active() string

Active returns the name of the currently active profile.

func (*Registry) ActivePool

func (r *Registry) ActivePool() (*pgxpool.Pool, error)

ActivePool returns the pgxpool.Pool for the active profile, or an error if no profile is active or the active profile is not connected.

func (*Registry) Add

func (r *Registry) Add(name string, cfg ProfileConfig) error

Add registers a new profile. Returns an error if the name is already taken.

func (*Registry) Connect

func (r *Registry) Connect(ctx context.Context, name string) error

Connect opens a pgxpool connection for the named profile.

func (*Registry) Disconnect

func (r *Registry) Disconnect(name string) error

Disconnect closes the pool for the named profile.

func (*Registry) Get

func (r *Registry) Get(name string) (*Profile, error)

Get returns the profile with the given name, or an error if not found.

func (*Registry) List

func (r *Registry) List() []ProfileInfo

List returns a sorted slice of ProfileInfo for all registered profiles.

func (*Registry) Remove

func (r *Registry) Remove(name string) error

Remove deletes a profile, closing its pool if connected. Returns an error if the profile does not exist.

func (*Registry) Switch

func (r *Registry) Switch(name string) error

Switch sets the active profile. Returns an error if the profile does not exist.

Jump to

Keyboard shortcuts

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