Documentation
¶
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) Close() error
- func (c *Client) IsHealthy(ctx context.Context) bool
- func (c *Client) Migrate(ctx context.Context, migrationsFS embed.FS, migrationsPath string) error
- func (c *Client) MigrationVersion() (uint, bool, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Stats() (*sql.DBStats, error)
- func (c *Client) Transaction(ctx context.Context, fn func(tx *gorm.DB) error) error
- func (c *Client) WithContext(ctx context.Context) *Client
- type Config
- func (c *Config) WithCredentials(username, password string) *Config
- func (c *Config) WithMetrics(enabled bool) *Config
- func (c *Config) WithPool(maxOpen, maxIdle int, maxLifetime, maxIdleTime time.Duration) *Config
- func (c *Config) WithPort(port int) *Config
- func (c *Config) WithSSL(mode, cert, key, rootCert string) *Config
- func (c *Config) WithTracing(enabled bool) *Config
- type Option
- func WithCredentials(username, password string) Option
- func WithMetricsOption(enabled bool) Option
- func WithPool(maxOpen, maxIdle int, maxLifetime, maxIdleTime time.Duration) Option
- func WithPort(port int) Option
- func WithSSL(mode, cert, key, rootCert string) Option
- func WithTracing(enabled bool) Option
Constants ¶
const ( Postgres = "postgres" MySQL = "mysql" MSSQL = "mssql" )
Database type constants
Variables ¶
Functions ¶
This section is empty.
Types ¶
type Client ¶
Client wraps gorm.DB with additional functionality
func (*Client) MigrationVersion ¶
MigrationVersion returns the current migration version
func (*Client) Transaction ¶
Transaction executes a function within a database transaction
type Config ¶
type Config struct {
// Database type (postgres, mysql, mssql)
Type string `json:"type" yaml:"type" validate:"required,oneof=postgres mysql mssql"`
// Connection settings
Host string `json:"host" yaml:"host" validate:"required"`
Port int `json:"port" yaml:"port" validate:"required,min=1,max=65535"`
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
Database string `json:"database" yaml:"database" validate:"required"`
// SSL settings
SSLMode string `json:"ssl_mode" yaml:"ssl_mode"` // for postgres: disable, require, verify-ca, verify-full
SSLCert string `json:"ssl_cert" yaml:"ssl_cert"` // path to cert file
SSLKey string `json:"ssl_key" yaml:"ssl_key"` // path to key file
SSLRootCert string `json:"ssl_root_cert" yaml:"ssl_root_cert"` // path to root cert file
// Connection pool settings
MaxOpenConns int `json:"max_open_conns" yaml:"max_open_conns"`
MaxIdleConns int `json:"max_idle_conns" yaml:"max_idle_conns"`
ConnMaxLifetime time.Duration `json:"conn_max_lifetime" yaml:"conn_max_lifetime"`
ConnMaxIdleTime time.Duration `json:"conn_max_idle_time" yaml:"conn_max_idle_time"`
// OpenTelemetry settings
EnableTracing bool `json:"enable_tracing" yaml:"enable_tracing"`
EnableMetrics bool `json:"enable_metrics" yaml:"enable_metrics"`
// GORM settings
SkipDefaultTransaction bool `json:"skip_default_transaction" yaml:"skip_default_transaction"`
PrepareStmt bool `json:"prepare_stmt" yaml:"prepare_stmt"`
}
Config holds database connection configuration
func (*Config) WithCredentials ¶
WithCredentials sets the username and password
func (*Config) WithMetrics ¶
WithMetrics enables or disables OpenTelemetry metrics
func (*Config) WithTracing ¶
WithTracing enables or disables OpenTelemetry tracing
type Option ¶
type Option func(*Config)
Option configures the database client
func WithCredentials ¶
WithCredentials sets the username and password (as Option for functional options pattern)
func WithMetricsOption ¶
WithMetrics enables or disables OpenTelemetry metrics (as Option for functional options pattern)
func WithTracing ¶
WithTracing enables or disables OpenTelemetry tracing (as Option for functional options pattern)