db

package
v0.0.0-...-a670c85 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2025 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyUsername indicates database username is not configured.
	ErrEmptyUsername = errors.New("database username cannot be empty")
	// ErrEmptyPassword indicates database password is not configured.
	ErrEmptyPassword = errors.New("database password cannot be empty")
	// ErrEmptyDatabase indicates database name is not configured.
	ErrEmptyDatabase = errors.New("database name cannot be empty")
	// ErrInvalidPort indicates database port is out of valid range.
	ErrInvalidPort = errors.New("database port must be between 1 and 65535")
)
View Source
var Module = fx.Module("db",
	fx.Provide(func(lc fx.Lifecycle) (*pgxpool.Pool, error) {
		var pool *pgxpool.Pool
		var err error

		lc.Append(fx.Hook{
			OnStart: func(ctx context.Context) error {
				pool, err = NewDBPool(ctx)
				if err != nil {
					return err
				}
				return pool.Ping(ctx)
			},
			OnStop: func(ctx context.Context) error {
				if pool != nil {
					pool.Close()
				}
				return nil
			},
		})

		return pool, err
	}),
)

Module provides database connection pool to the fx application.

Functions

func NewDBPool

func NewDBPool(ctx context.Context) (*pgxpool.Pool, error)

NewDBPool creates a new database connection pool with default configuration. It reads configuration from environment variables with sensible defaults. Note: Password must be explicitly set via PSYCONOTE_PSQL_PASSWORD environment variable.

Types

type DBConfig

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

DBConfig holds database connection parameters.

func NewDBConfig

func NewDBConfig(raw *config.Config) (*DBConfig, error)

NewDBConfig creates a DBConfig from the raw configuration.

func (*DBConfig) ConnectPool

func (dbc *DBConfig) ConnectPool(ctx context.Context) (*pgxpool.Pool, error)

ConnectPool creates a new connection pool to the database with UUID support.

func (*DBConfig) ConnectionString

func (dbc *DBConfig) ConnectionString() string

ConnectionString returns the PostgreSQL connection string. Note: This exposes credentials. Avoid logging this value.

func (*DBConfig) Ping

func (dbc *DBConfig) Ping(ctx context.Context) error

Ping checks if the database is reachable.

func (*DBConfig) Validate

func (dbc *DBConfig) Validate() error

Validate checks that all required configuration fields are properly set.

Jump to

Keyboard shortcuts

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