postgresql

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package postgresql implements the provider.Provider interface for PostgreSQL configuration backends. It polls a database table at a configurable interval to sync dynamic configuration values. Supports custom Repository implementations for non-standard table schemas. All keys are fetched in a single query per poll cycle for efficiency.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Repository is the data access layer. If nil, a DefaultRepository
	// must be provided via the DB/Table/KeyColumn/ValueColumn fields.
	Repository Repository

	// DB is an existing [*sql.DB] connection. Users manage the lifecycle
	// (open, close, connection pooling) themselves.
	// Only used when Repository is nil.
	DB *sql.DB

	// TableName is the table to query. Default: "config".
	// Only used when Repository is nil.
	TableName string

	// KeyColumn is the column name for config keys. Default: "config_key".
	// Only used when Repository is nil.
	KeyColumn string

	// ValueColumn is the column name for config values. Default: "config_value".
	// Only used when Repository is nil.
	ValueColumn string

	// PollInterval is how often to check for changes. Default: 5s.
	PollInterval time.Duration
}

Config holds PostgreSQL-specific configuration.

type DefaultRepository

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

DefaultRepository provides a simple key-value query against a PostgreSQL table.

func NewDefaultRepository

func NewDefaultRepository(db *sql.DB, table, keyColumn, valueColumn string) *DefaultRepository

NewDefaultRepository creates the default repository that queries a key-value table. The table is expected to have at least a key column and a value column. Example schema:

CREATE TABLE config (
    config_key   VARCHAR(255) PRIMARY KEY,
    config_value TEXT
);

func (*DefaultRepository) Get

func (r *DefaultRepository) Get(ctx context.Context, key string) (string, error)

Get retrieves the value for a key from the PostgreSQL table.

func (*DefaultRepository) GetAll

func (r *DefaultRepository) GetAll(ctx context.Context, keys []string) (map[string]string, error)

GetAll retrieves values for multiple keys in a single query using $N placeholders.

type Provider

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

Provider implements the poya Provider interface using polling against PostgreSQL. All keys are fetched in a single query per poll cycle.

func New

func New(cfg Config) (*Provider, error)

New creates a new PostgreSQL provider. Pass a Config with either a custom Repository or a pre-configured *sql.DB.

Using the default repository:

provider, err := postgresql.New(postgresql.Config{
    DB:           db,
    TableName:    "config",
    KeyColumn:    "config_key",
    ValueColumn:  "config_value",
    PollInterval: 5 * time.Second,
})

Using a custom repository:

provider, err := postgresql.New(postgresql.Config{
    Repository:   myCustomRepo,
    PollInterval: 5 * time.Second,
})

func (*Provider) Close

func (p *Provider) Close() error

Close releases PostgreSQL provider resources. Note: the *sql.DB is managed by the caller and is not closed here.

func (*Provider) Get

func (p *Provider) Get(ctx context.Context, key string) (string, error)

Get retrieves the current value for a key from PostgreSQL.

func (*Provider) Watch

func (p *Provider) Watch(ctx context.Context, keys []string, onChange func(key string, value string), onDelete func(key string)) error

Watch polls all keys at the configured interval using a single query. When any value changes, onChange is called with the key and new value. When a key is deleted from the table, onDelete is called so the SDK reverts it to its default.

type Repository

type Repository interface {
	Get(ctx context.Context, key string) (string, error)
	GetAll(ctx context.Context, keys []string) (map[string]string, error)
}

Repository defines how config values are read from PostgreSQL. Users can implement this interface for custom table schemas, or use the DefaultRepository for a simple key-value table.

Jump to

Keyboard shortcuts

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