clpostgres

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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"`), 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"`), 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

View Source
var Test = fx.Options(Prod,

	fx.Decorate(func(cfg Config) Config {
		var b [8]byte
		rand.Read(b[:])
		cfg.TemporarySchemaName = fmt.Sprintf(moduleName+"_test_%x", b[:])
		return cfg
	}),

	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

func New(pcfg *pgxpool.Config) (db *sql.DB)

New inits a stdlib sql connection

func NewReadOnlyConfig

func NewReadOnlyConfig(cfg Config, logs *zap.Logger) (*pgxpool.Config, error)

NewReadOnlyConfig constructs a config for a read-only database connecion. The aws config is optional and is only used when IamAuth option is set.

func NewReadWriteConfig

func NewReadWriteConfig(cfg Config, logs *zap.Logger) (*pgxpool.Config, error)

NewReadWriteConfig constructs a config for a read-write database connecion. The aws config is optional and only used when the 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:"RW_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"`
	// IamAuthTimeout bounds the time it takes to geht the IAM auth token
	IamAuthTimeout time.Duration `env:"IAM_AUTH_TIMEOUT" envDefault:"100ms"`
	// PgxLogLevel is provided to pgx to determine the level of logging of postgres interactions
	PgxLogLevel string `env:"PGX_LOG_LEVEL" envDefault:"info"`
	// SchemaName sets the schema to which the connections search_path will be set
	SchemaName string `env:"SCHEMA_NAME" envDefault:"public"`
	// TemporarySchemaName can be set to non-empty to cause schema name to indicate it is temporary schema
	TemporarySchemaName string `env:"TEMPORARY_SCHEMA_NAME"`
	// SSLMode sets tls encryption on the database connection
	SSLMode string `env:"SSL_MODE" envDefault:"disable"`
}

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

func NewLogger

func NewLogger(logs *zap.Logger) *Logger

NewLogger inits a logger for the

func (*Logger) Log

func (pl *Logger) Log(ctx context.Context, level tracelog.LogLevel, msg string, data map[string]interface{})

Jump to

Keyboard shortcuts

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