Documentation
¶
Index ¶
- Constants
- Variables
- func ProvideClientConfig(cfg Config) database.ClientConfig
- func ProvideDatabase(ctx context.Context, logger logging.Logger, ...) (client database.Client, err error)
- func RegisterClientConfig(i do.Injector)
- func RegisterDatabase(i do.Injector)
- type Config
- func (cfg *Config) ConnectToDatabase() (*sql.DB, error)
- func (cfg *Config) EnsureDefaults()
- func (cfg *Config) GetConnMaxLifetime() time.Duration
- func (cfg *Config) GetMaxIdleConns() int
- func (cfg *Config) GetMaxOpenConns() int
- func (cfg *Config) GetMaxPingAttempts() uint64
- func (cfg *Config) GetPingWaitPeriod() time.Duration
- func (cfg *Config) GetReadConnectionString() string
- func (cfg *Config) GetWriteConnectionString() string
- func (cfg *Config) LoadConnectionDetailsFromURL(u string) error
- func (cfg *Config) ValidateWithContext(ctx context.Context) error
- type ConnectionDetails
Constants ¶
const (
ProviderPostgres = "postgres"
)
Variables ¶
var ( // ClientConfigProviders provides the conversion from Config to database.ClientConfig. // Include this in wire builds that need to use postgres.PGProviders. ClientConfigProviders = wire.NewSet( ProvideClientConfig, ) // DatabaseConfigProviders are what we provide to dependency injection. // Include with repositories.RepositoryProviders for builds that run migrations on startup. // Requires wire.FieldsOf(..., "Database") on the parent config to supply *Config. DatabaseConfigProviders = wire.NewSet( ProvideDatabase, ProvideClientConfig, ) )
Functions ¶
func ProvideClientConfig ¶
func ProvideClientConfig(cfg Config) database.ClientConfig
ProvideClientConfig converts Config to database.ClientConfig for wire. Wire extracts fields by value from config structs, so we accept a value and return a pointer.
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 ¶
RegisterClientConfig registers a database.ClientConfig with the injector.
func RegisterDatabase ¶
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) EnsureDefaults ¶
func (cfg *Config) EnsureDefaults()
EnsureDefaults sets sensible defaults for zero-valued fields.
func (*Config) GetConnMaxLifetime ¶
GetConnMaxLifetime implements database.ClientConfig. Returns 30m when unset (zero).
func (*Config) GetMaxIdleConns ¶
GetMaxIdleConns implements database.ClientConfig. Returns 5 when unset (zero).
func (*Config) GetMaxOpenConns ¶
GetMaxOpenConns implements database.ClientConfig. Returns 7 when unset (zero).
func (*Config) GetMaxPingAttempts ¶
GetMaxPingAttempts implements database.ClientConfig.
func (*Config) GetPingWaitPeriod ¶
GetPingWaitPeriod implements database.ClientConfig.
func (*Config) GetReadConnectionString ¶
GetConnectionString implements database.ClientConfig.
func (*Config) GetWriteConnectionString ¶
GetWriteConnectionString implements database.ClientConfig.
func (*Config) LoadConnectionDetailsFromURL ¶
LoadConnectionDetailsFromURL wraps an inner function.
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.