database

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2025 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Overview

Package database provides PostgreSQL database configuration and connection management. It supports environment-based configuration, DSN parsing, and connection pooling using the pgx driver for optimal performance and reliability.

Example usage:

// Create database connection from environment variables
ctx := context.Background()
db, err := database.NewFromEnv(ctx)
if err != nil {
	log.Fatal(err)
}
defer db.Close(ctx)

// Create configuration from environment
config := database.NewConfigFromEnv()
connectionURL := config.ConnectionURL()

// Create configuration from DSN
config, err := database.NewFromDSN("postgres://user:pass@localhost/mydb")
if err != nil {
	log.Fatal(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Name               string        // Database name
	User               string        // Database user
	Host               string        // Database host
	Port               string        // Database port
	SSLMode            string        // SSL mode (disable, require, verify-ca, verify-full)
	ConnectionTimeout  int           // Connection timeout in seconds
	Password           string        // Database password
	SSLCertPath        string        // Path to SSL certificate file
	SSLKeyPath         string        // Path to SSL key file
	SSLRootCertPath    string        // Path to SSL root certificate file
	PoolMinConnections string        // Minimum connections in pool
	PoolMaxConnections string        // Maximum connections in pool
	PoolMaxConnLife    time.Duration // Maximum connection lifetime
	PoolMaxConnIdle    time.Duration // Maximum connection idle time
	PoolHealthCheck    time.Duration // Health check period for connections
}

Config holds database connection configuration parameters. It supports PostgreSQL connection settings including SSL, connection pooling, and timeout configurations for production database deployments.

func NewConfigFromEnv

func NewConfigFromEnv() *Config

NewConfigFromEnv creates a new database configuration from environment variables. It loads configuration from .env file if present and supports both individual environment variables and a complete DSN via DB_DSN. If DB_DSN is provided, it takes precedence over individual variables. Default values are applied for connection pool settings when not specified.

func NewFromDSN

func NewFromDSN(dsn string) (*Config, error)

NewFromDSN creates a new database configuration from a PostgreSQL DSN (Data Source Name). The DSN should be in the format: postgres://user:password@host:port/database?options Returns an error if the DSN cannot be parsed by the pgx driver.

func (*Config) ConnectionURL

func (c *Config) ConnectionURL() string

ConnectionURL generates a PostgreSQL connection URL from the configuration. Returns a properly formatted postgres:// URL with all configured parameters. Returns an empty string if the configuration is nil.

func (*Config) DatabaseConfig

func (c *Config) DatabaseConfig() *Config

DatabaseConfig returns the database configuration. This method provides a consistent interface for accessing configuration settings.

type DB

type DB struct {
	Pool *pgxpool.Pool // PostgreSQL connection pool
}

DB represents a database connection with connection pooling. It wraps a pgxpool.Pool to provide high-performance PostgreSQL connectivity with automatic connection management and health checking.

func NewFromEnv

func NewFromEnv(ctx context.Context) (*DB, error)

NewFromEnv creates a new database connection using environment configuration. It automatically configures connection pooling, health checks, and connection validation. The context is used for connection establishment and should have appropriate timeout. Returns an error if the configuration is invalid or connection fails.

func (*DB) Close

func (db *DB) Close(ctx context.Context)

Close gracefully closes the database connection pool. It logs the closure and ensures all connections are properly released. The context can be used to set a timeout for the close operation.

Jump to

Keyboard shortcuts

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