Documentation
¶
Overview ¶
Package sql provides a database/sql connection pool to PostgreSQL, MySQL or SQLite, instrumented with OpenTelemetry, with a health probe and migrations.
core.New( core.WithConfigFS(configs.FS), sql.Module, sql.AsMigrations(migrations.FS), ).Run(sql.Migrate(), httpserver.Serve())
The pool is provided as a *database/sql.DB.
Index ¶
Constants ¶
const ( DriverPostgres = "postgres" DriverMySQL = "mysql" DriverSQLite = "sqlite" )
Supported values of sql.driver.
const DefaultMaxIdleConns = 2
DefaultMaxIdleConns is the default of sql.max_idle_conns, the database/sql default.
const ModuleName = "sql"
ModuleName is the name of the sql fx module.
Variables ¶
var Module = fx.Module( ModuleName, fx.Provide(NewDriver, NewDB), health.AsProbe(NewProbe, health.Readiness, health.Startup), )
Module provides the *database/sql.DB and its Driver, and registers its readiness and startup health.Probe.
Functions ¶
func AsMigrations ¶
AsMigrations registers the migrations: the goose SQL files at the root of fsys (e.g. 00001_create_users.sql), applied by Migrate. Register them once.
-- +goose Up CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL); -- +goose Down DROP TABLE users;
See https://pressly.github.io/goose/documentation/annotations/.
func Migrate ¶
Migrate applies the pending migrations registered with AsMigrations when the application starts, before the start hooks registered after it: pass it before the servers to Run. The application fails to start if a migration fails.
func NewDB ¶
func NewDB(params NewDBParams) (*stdsql.DB, error)
NewDB opens the connection pool configured by the sql.* keys, instrumented with OpenTelemetry. Unless sql.ping_on_start is false, the application fails to start if the database is unreachable. The pool is closed when the application stops.
Types ¶
type Driver ¶
type Driver struct {
// Name is the sql.driver value: postgres, mysql or sqlite.
Name string
// contains filtered or unexported fields
}
Driver describes a supported database.
type MigrateParams ¶
type MigrateParams struct {
fx.In
Lifecycle fx.Lifecycle
Driver Driver
Logger *slog.Logger
TracerProvider trace.TracerProvider
DB *stdsql.DB
Migrations []fs.FS `group:"oryn-sql-migrations"`
}
MigrateParams are the dependencies of Migrate.
type NewDBParams ¶
type NewDBParams struct {
fx.In
Lifecycle fx.Lifecycle
Config *config.Config
Driver Driver
TracerProvider trace.TracerProvider
MeterProvider metric.MeterProvider
Propagator propagation.TextMapPropagator
}
NewDBParams are the dependencies of NewDB.
type Probe ¶
type Probe struct {
// contains filtered or unexported fields
}
Probe is a health.Probe pinging the database.