Documentation ¶
Index ¶
- Variables
- func New(pcfg *pgxpool.Config, m *Migrater, tp trace.TracerProvider) (db *sql.DB)
- func NewReadOnlyConfig(cfg Config, logs *zap.Logger, awsc aws.Config) (*pgxpool.Config, error)
- func NewReadWriteConfig(cfg Config, logs *zap.Logger, awsc aws.Config) (*pgxpool.Config, error)
- type Config
- type Logger
- type Migrater
Constants ¶
This section is empty.
Variables ¶
var Prod = fx.Module(moduleName, fx.Decorate(func(l *zap.Logger) *zap.Logger { return l.Named(moduleName) }), fx.Provide(fx.Annotate( func(o env.Options) (c Config, err error) { o.Prefix = strings.ToUpper(moduleName) + "_" return c, env.Parse(&c, o) }, fx.ParamTags(`optional:"true"`))), fx.Provide(fx.Annotate(NewReadOnlyConfig, fx.ParamTags(``, ``, `optional:"true"`), fx.ResultTags(`name:"ro"`))), fx.Provide(fx.Annotate(NewReadWriteConfig, fx.ParamTags(``, ``, `optional:"true"`), fx.ResultTags(`name:"rw"`))), fx.Provide(fx.Annotate(New, fx.ParamTags(`name:"ro"`, `optional:"true"`, `optional:"true"`), fx.ResultTags(`name:"ro"`), fx.OnStart(func(ctx context.Context, in struct { fx.In DB *sql.DB `name:"ro"` }) error { return in.DB.PingContext(ctx) }), fx.OnStop(func(ctx context.Context, in struct { fx.In DB *sql.DB `name:"ro"` }) error { return in.DB.Close() }), )), fx.Provide(fx.Annotate(New, fx.ParamTags(`name:"rw"`, `optional:"true"`, `optional:"true"`), fx.ResultTags(`name:"rw"`), fx.OnStart(func(ctx context.Context, in struct { fx.In DB *sql.DB `name:"rw"` }) error { return in.DB.PingContext(ctx) }), fx.OnStop(func(ctx context.Context, in struct { fx.In DB *sql.DB `name:"rw"` }) error { return in.DB.Close() }), )), )
Prod configures the DI for providng database connectivity
var Test = fx.Options(Prod, fx.Provide(fx.Annotate( NewMigrater, fx.OnStart(func(ctx context.Context, m *Migrater) error { return m.Migrate(ctx) }), fx.OnStop(func(ctx context.Context, m *Migrater) error { return m.DropDatabase(ctx) }), fx.ParamTags(``, ``, `name:"rw"`)), ), fx.Provide(fx.Annotate(func(rw *sql.DB) *sql.DB { return rw }, fx.ParamTags(`name:"rw"`))), fx.Provide(fx.Annotate(func(rw *pgxpool.Config) *pgxpool.Config { return rw }, fx.ParamTags(`name:"rw"`))), )
Test configures the DI for a test environment
Functions ¶
func New ¶
New inits a stdlib sql connection. Any other dependency can optionall ybe provided as migrated to force it's lifecycle to be run before the database is connected. This is mostly usefull to run migration logic (such as initializing the database)
func NewReadOnlyConfig ¶
NewReadOnlyConfig constructs a config for a read-only database connecion. The aws config is optional and is only used when IamAuth option is set.
Types ¶
type Config ¶
type Config struct { // DatabaseName names the database the connection will be made to DatabaseName string `env:"DATABASE_NAME" envDefault:"postgres"` // ReadWriteHostname endpoint allows configuration of a endpoint that can read and write ReadWriteHostname string `env:"RW_HOSTNAME" envDefault:"localhost"` // ReadOnlyHostname endpoint allows configuration of a endpoint that can read and write ReadOnlyHostname string `env:"RO_HOSTNAME" envDefault:"localhost"` // Port for to the database connection(s) Port int `env:"PORT" envDefault:"5432"` // Username configures the username to connect to the postgres instance Username string `env:"USERNAME" envDefault:"postgres"` // Password configures the postgres password for authenticating with the instance Password string `env:"PASSWORD"` // ApplicationName allows the application to indicate its name so connections can be more easily debugged ApplicationName string `env:"APPLICATION_NAME" envDefault:"unknown"` // PgxLogLevel is provided to pgx to determine the level of logging of postgres interactions PgxLogLevel string `env:"PGX_LOG_LEVEL" envDefault:"info"` // SSLMode sets tls encryption on the database connection SSLMode string `env:"SSL_MODE" envDefault:"disable"` // IamAuth will cause the password to be set to an IAM token for authentication IamAuth bool `env:"IAM_AUTH"` // IamAuthTimeout bounds the time it takes to geht the IAM auth token IamAuthTimeout time.Duration `env:"IAM_AUTH_TIMEOUT" envDefault:"100ms"` // TemporaryDatabase can be set to cause the logic to create a random database name and initialize // it when running auto-migration. This is mostly usefull for automated tests TemporaryDatabase bool `env:"TEMPORARY_DATABASE" envDefault:"false"` // AutoMigration can be set to true to cause the logic to automatically run migrations when started. This // is mostly usefull for automated tests. AutoMigration bool `env:"AUTO_MIGRATION" envDefault:"false"` }
Config configures the code in this package.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is a pgx logger that uses a main zap logger for logging but will prefer using a context specific logger if it exists
type Migrater ¶ added in v0.4.1
type Migrater struct {
// contains filtered or unexported fields
}
Migrater allows programmatic migration of a database schema. Mostly used in testing and local development to provide fully isolated databases.
func NewMigrater ¶ added in v0.4.1
func NewMigrater( cfg Config, logs *zap.Logger, dbcfg *pgxpool.Config, dir migrate.Dir, ) (*Migrater, error)
NewMigrator inits the migrater
func (Migrater) DropDatabase ¶ added in v0.4.1
DropDatabase drops the schema