db

package
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: Apache-2.0, MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Validation Errors
	ErrMissingHost   = errors.New("database host is required")
	ErrInvalidPort   = errors.New("database port must be greater than zero")
	ErrMissingUser   = errors.New("database user is required")
	ErrMissingDBName = errors.New("database name is required")

	// Connection Errors
	ErrInvalidConfig = errors.New("invalid database configuration")
	ErrFailedConnect = errors.New("failed to connect to database")
	ErrFailedPing    = errors.New("failed to ping database")
	ErrNilDatabase   = errors.New("database connection is nil")
	ErrFailedClose   = errors.New("failed to close database connection")
)

Functions

func NewDB

func NewDB(cfg Config) (*sql.DB, error)

Types

type Config

type Config struct {
	// Host specifies the database server's hostname or IP address.
	// Example: "localhost" or "192.168.1.100".
	Host string `toml:"host" mapstructure:"host"`

	// Port defines the port on which the database server is listening.
	// Default PostgreSQL port: 5432.
	Port int `toml:"port" mapstructure:"port"`

	// User represents the database username for authentication.
	// Example: "postgres" for a default PostgreSQL setup.
	User string `toml:"user" mapstructure:"user"`

	// Password is the database user's password.
	Password string `toml:"password" mapstructure:"password"`

	// DBName specifies the name of the PostgreSQL database to connect to.
	DBName string `toml:"dbname" mapstructure:"dbname"`

	// SSLMode determines whether to use SSL/TLS for database connections.
	// Example values: "disable", "require", "verify-full".
	// If empty, it defaults to "disable".
	SSLMode string `toml:"sslmode" mapstructure:"sslmode"`
}

Config represents the database connection settings.

func (Config) DSN

func (cfg Config) DSN() (string, error)

DSN constructs the PostgreSQL connection string.

func (*Config) Validate

func (cfg *Config) Validate() error

Validate ensures required fields are set and applies default values where necessary.

Jump to

Keyboard shortcuts

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