Database (pgx)
pkg/db/drivers/pgx manages a PostgreSQL connection pool using jackc/pgx.
lakta.NewRuntime( config.NewModule( config.WithConfigDirs(".", "./config"), config.WithArgs(os.Args[1:]), ), tint.NewModule(), slog.NewModule(), pgx.NewModule(), myapp.NewModule(),)Consuming the connection
Section titled “Consuming the connection”func (m *MyModule) Init(ctx context.Context) error { pool := do.MustInvoke[*pgxpool.Pool](lakta.GetInjector(ctx)) m.pool = pool return nil}Multi-instance
Section titled “Multi-instance”pgx.NewModule(pgx.WithName("primary")),pgx.NewModule(pgx.WithName("replica")),modules: db: pgx: primary: dsn: "postgres://user:pass@primary:5432/mydb" replica: dsn: "postgres://user:pass@replica:5432/mydb"Configuration Reference
Section titled “Configuration Reference”Config path: modules.db.pgx.<name>
dsn required dSN is the database connection string used to configure the database connection
LAKTA_MODULES__DB__PGX__<NAME>__DSN max_open_conns maxOpenConns specifies the maximum number of open connections to the database. It maps to the "max_open_conns" configuration
LAKTA_MODULES__DB__PGX__<NAME>__MAX_OPEN_CONNS log_level logLevel specifies the logging level for database operations, supporting values like trace, debug, info, warn, error, none
LAKTA_MODULES__DB__PGX__<NAME>__LOG_LEVEL health_check healthCheck enables or disables the database health check mechanism
LAKTA_MODULES__DB__PGX__<NAME>__HEALTH_CHECK min_conns minConns is the minimum number of idle connections kept in the pool
LAKTA_MODULES__DB__PGX__<NAME>__MIN_CONNS max_conn_lifetime maxConnLifetime is the maximum age of a connection before it is closed
LAKTA_MODULES__DB__PGX__<NAME>__MAX_CONN_LIFETIME max_conn_idle_time maxConnIdleTime is the maximum idle time before a connection is closed
LAKTA_MODULES__DB__PGX__<NAME>__MAX_CONN_IDLE_TIME health_check_period healthCheckPeriod is how often the pool checks idle connection health
LAKTA_MODULES__DB__PGX__<NAME>__HEALTH_CHECK_PERIOD statement_timeout statementTimeout sets the per-statement timeout (Postgres statement_timeout). Zero disables it
LAKTA_MODULES__DB__PGX__<NAME>__STATEMENT_TIMEOUT