databasecfg

package
v3.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: AGPL-3.0 Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	ProviderPostgres = "postgres"
)

Variables

This section is empty.

Functions

func ProvideClientConfig

func ProvideClientConfig(cfg Config) database.ClientConfig

ProvideClientConfig converts Config to database.ClientConfig.

func ProvideDatabase

func ProvideDatabase(
	ctx context.Context,
	logger logging.Logger,
	tracerProvider tracing.TracerProvider,
	cfg *Config,
	migrator database.Migrator,
	metricsProvider metrics.Provider,
) (client database.Client, err error)

ProvideDatabase creates a database client based on the configured provider and optionally runs migrations if RunMigrations is true and a migrator is provided. If metricsProvider is non-nil and cfg.EnableDatabaseMetrics is true, the client will emit db.sql.* metrics (e.g. db_sql_latency_milliseconds). DB metrics are off by default to avoid high cardinality.

func RegisterClientConfig

func RegisterClientConfig(i do.Injector)

RegisterClientConfig registers a database.ClientConfig with the injector.

func RegisterDatabase

func RegisterDatabase(i do.Injector)

RegisterDatabase registers a database.Client with the injector. Prerequisite: *Config and database.Migrator must be registered in the injector.

Types

type Config

type Config struct {
	Encryption                   encryptioncfg.Config `env:"init"                             envPrefix:"ENCRYPTION_"             json:"encryption"`
	OAuth2TokenEncryptionKey     string               `env:"OAUTH2_TOKEN_ENCRYPTION_KEY"      json:"oauth2TokenEncryptionKey"`
	UserDeviceTokenEncryptionKey string               `env:"USER_DEVICE_TOKEN_ENCRYPTION_KEY" json:"userDeviceTokenEncryptionKey"`
	Provider                     string               `env:"PROVIDER"                         envDefault:"postgres"               json:"provider"`
	ReadConnection               ConnectionDetails    `envPrefix:"READ_CONNECTION_"           json:"readConnection"`
	WriteConnection              ConnectionDetails    `envPrefix:"WRITE_CONNECTION_"          json:"writeConnection"`
	PingWaitPeriod               time.Duration        `env:"PING_WAIT_PERIOD"                 envDefault:"1s"                     json:"pingWaitPeriod"`
	MaxPingAttempts              uint64               `env:"MAX_PING_ATTEMPTS"                json:"maxPingAttempts"`
	ConnMaxLifetime              time.Duration        `env:"CONN_MAX_LIFETIME"                envDefault:"30m"                    json:"connMaxLifetime"`
	MaxIdleConns                 uint16               `env:"MAX_IDLE_CONNS"                   envDefault:"5"                      json:"maxIdleConns"`
	MaxOpenConns                 uint16               `env:"MAX_OPEN_CONNS"                   envDefault:"7"                      json:"maxOpenConns"`
	Debug                        bool                 `env:"DEBUG"                            json:"debug"`
	LogQueries                   bool                 `env:"LOG_QUERIES"                      json:"logQueries"`
	RunMigrations                bool                 `env:"RUN_MIGRATIONS"                   json:"runMigrations"`
	EnableDatabaseMetrics        bool                 `env:"ENABLE_DATABASE_METRICS"          json:"enableDatabaseMetrics"`
	// contains filtered or unexported fields
}

Config represents our database configuration.

func (*Config) ConnectToDatabase

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

func (*Config) EnsureDefaults

func (cfg *Config) EnsureDefaults()

EnsureDefaults sets sensible defaults for zero-valued fields.

func (*Config) GetConnMaxLifetime

func (cfg *Config) GetConnMaxLifetime() time.Duration

GetConnMaxLifetime implements database.ClientConfig. Returns 30m when unset (zero).

func (*Config) GetMaxIdleConns

func (cfg *Config) GetMaxIdleConns() int

GetMaxIdleConns implements database.ClientConfig. Returns 5 when unset (zero).

func (*Config) GetMaxOpenConns

func (cfg *Config) GetMaxOpenConns() int

GetMaxOpenConns implements database.ClientConfig. Returns 7 when unset (zero).

func (*Config) GetMaxPingAttempts

func (cfg *Config) GetMaxPingAttempts() uint64

GetMaxPingAttempts implements database.ClientConfig.

func (*Config) GetPingWaitPeriod

func (cfg *Config) GetPingWaitPeriod() time.Duration

GetPingWaitPeriod implements database.ClientConfig.

func (*Config) GetReadConnectionString

func (cfg *Config) GetReadConnectionString() string

GetConnectionString implements database.ClientConfig.

func (*Config) GetWriteConnectionString

func (cfg *Config) GetWriteConnectionString() string

GetWriteConnectionString implements database.ClientConfig.

func (*Config) LoadConnectionDetailsFromURL

func (cfg *Config) LoadConnectionDetailsFromURL(u string) error

LoadConnectionDetailsFromURL wraps an inner function.

func (*Config) ValidateWithContext

func (cfg *Config) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates an DatabaseSettings struct.

type ConnectionDetails

type ConnectionDetails struct {
	Username   string `env:"USERNAME"    json:"username"`
	Password   string `env:"PASSWORD"    json:"password"`
	Database   string `env:"DATABASE"    json:"database"`
	Host       string `env:"HOST"        json:"hostname"`
	Port       uint16 `env:"PORT"        json:"port"`
	DisableSSL bool   `env:"DISABLE_SSL" json:"disableSSL"`
	// contains filtered or unexported fields
}

func (*ConnectionDetails) LoadFromURL

func (x *ConnectionDetails) LoadFromURL(u string) error

LoadFromURL accepts a Postgres connection string and parses it into the ConnectionDetails struct.

func (*ConnectionDetails) String

func (x *ConnectionDetails) String() string

func (*ConnectionDetails) URI

func (x *ConnectionDetails) URI() string

func (*ConnectionDetails) ValidateWithContext

func (x *ConnectionDetails) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates an DatabaseSettings struct.

Jump to

Keyboard shortcuts

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