databasecfg

package
v2.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ProviderPostgres = "postgres"
	ProviderMySQL    = "mysql"
	ProviderSQLite   = "sqlite"
)

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"`
	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) ConnectToReadDatabase

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

func (*Config) ConnectToWriteDatabase

func (cfg *Config) ConnectToWriteDatabase() (*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

GetReadConnectionString 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) MySQLDSN

func (x *ConnectionDetails) MySQLDSN() string

MySQLDSN returns a MySQL DSN connection string.

func (*ConnectionDetails) SQLiteDSN

func (x *ConnectionDetails) SQLiteDSN() string

SQLiteDSN returns the database file path for SQLite.

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