Documentation
¶
Index ¶
- type Config
- type ConnectionStats
- type Database
- type Option
- func WithConnMaxIdleTime(connMaxIdleTime time.Duration) Option
- func WithConnMaxLifetime(connMaxLifetime time.Duration) Option
- func WithConnectTimeout(connectTimeout time.Duration) Option
- func WithDatabase(database string) Option
- func WithHost(host string) Option
- func WithMaxIdleConns(maxIdleConns int) Option
- func WithMaxOpenConns(maxOpenConns int) Option
- func WithPassword(password string) Option
- func WithPort(port int) Option
- func WithQueryTimeout(queryTimeout time.Duration) Option
- func WithRLSContextVarName(varName string) Option
- func WithSSLMode(sslMode string) Option
- func WithUser(user string) Option
- type PostgreSQL
- func (p *PostgreSQL) ClearTenantContext(ctx context.Context) error
- func (p *PostgreSQL) Close() error
- func (p *PostgreSQL) Connect() error
- func (p *PostgreSQL) GetDB() *sql.DB
- func (p *PostgreSQL) GetStats() ConnectionStats
- func (p *PostgreSQL) HealthCheck() error
- func (p *PostgreSQL) SetTenantContext(ctx context.Context, tenantID string) error
- type TenantContext
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
Host string
Port int
User string
Password string
Database string
SSLMode string
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
ConnMaxIdleTime time.Duration
ConnectTimeout time.Duration
QueryTimeout time.Duration
// RLS Multitenancy configuration
RLSContextVarName string // Default: "app.current_tenant_id"
}
Config holds database configuration
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a secure default configuration
type ConnectionStats ¶
type ConnectionStats struct {
OpenConnections int
InUse int
Idle int
WaitCount int64
WaitDuration time.Duration
MaxIdleClosed int64
MaxLifetimeClosed int64
}
ConnectionStats provides information about database connections
type Database ¶
type Database interface {
// Core operations
Connect() error
Close() error
GetDB() *sql.DB
HealthCheck() error
GetStats() ConnectionStats
// RLS Multitenancy support - simple tenant context switching
SetTenantContext(ctx context.Context, tenantID string) error
ClearTenantContext(ctx context.Context) error
}
Database interface defines the contract for database operations
type Option ¶
type Option func(*Config)
Option is a functional option for configuring the database
func WithConnMaxIdleTime ¶
WithConnMaxIdleTime sets the maximum idle time of a connection
func WithConnMaxLifetime ¶
WithConnMaxLifetime sets the maximum lifetime of a connection
func WithConnectTimeout ¶
WithConnectTimeout sets the connection timeout
func WithMaxIdleConns ¶
WithMaxIdleConns sets the maximum number of idle connections
func WithMaxOpenConns ¶
WithMaxOpenConns sets the maximum number of open connections
func WithPassword ¶
WithPassword sets the database password
func WithQueryTimeout ¶
WithQueryTimeout sets the query timeout
func WithRLSContextVarName ¶
WithRLSContextVarName sets the RLS context variable name
type PostgreSQL ¶
type PostgreSQL struct {
// contains filtered or unexported fields
}
PostgreSQL implementation
func NewPostgreSQL ¶
func NewPostgreSQL(config *Config) *PostgreSQL
NewPostgreSQL creates a new PostgreSQL database instance
func NewPostgreSQLWithOptions ¶
func NewPostgreSQLWithOptions(options ...Option) *PostgreSQL
NewPostgreSQLWithOptions creates a new PostgreSQL instance with options
func (*PostgreSQL) ClearTenantContext ¶
func (p *PostgreSQL) ClearTenantContext(ctx context.Context) error
ClearTenantContext clears the tenant context
func (*PostgreSQL) Connect ¶
func (p *PostgreSQL) Connect() error
Connect establishes a connection to the PostgreSQL database
func (*PostgreSQL) GetDB ¶
func (p *PostgreSQL) GetDB() *sql.DB
GetDB returns the underlying sql.DB instance
func (*PostgreSQL) GetStats ¶
func (p *PostgreSQL) GetStats() ConnectionStats
GetStats returns connection pool statistics
func (*PostgreSQL) HealthCheck ¶
func (p *PostgreSQL) HealthCheck() error
HealthCheck verifies the database connection is healthy
func (*PostgreSQL) SetTenantContext ¶
func (p *PostgreSQL) SetTenantContext(ctx context.Context, tenantID string) error
SetTenantContext sets the tenant context for RLS
type TenantContext ¶
type TenantContext struct {
TenantID string `json:"tenantID"`
SetAt time.Time `json:"setAt,omitempty"`
}
TenantContext holds tenant-specific information for RLS multitenancy