Documentation
¶
Overview ¶
Package database provides database connection pooling, migrations, and health checks. It supports PostgreSQL with GORM and includes utilities for testing.
Index ¶
- func Close(db *gorm.DB) error
- func Connect(cfg config.DatabaseConfig) (*gorm.DB, error)
- func HealthCheck(db *gorm.DB) error
- func Migrate(db *gorm.DB, models ...interface{}) error
- func NewPostgresDB(cfg config.DatabaseConfig) (*gorm.DB, error)
- func OrderBy(column, direction string) func(db *gorm.DB) *gorm.DB
- func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB
- func Search(columns []string, query string) func(db *gorm.DB) *gorm.DB
- func SoftDeleteScope() func(db *gorm.DB) *gorm.DB
- func TenantScope(tenantID string) func(db *gorm.DB) *gorm.DB
- func Transaction(db *gorm.DB, fn func(*gorm.DB) error) error
- func WithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)
- type ConnectionStats
- type Manager
- type PaginationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
func Connect(cfg config.DatabaseConfig) (*gorm.DB, error)
Connect establishes a database connection using the global manager.
func HealthCheck ¶
HealthCheck performs a database health check using the global manager.
func NewPostgresDB ¶ added in v0.1.4
func NewPostgresDB(cfg config.DatabaseConfig) (*gorm.DB, error)
NewPostgresDB establishes a new PostgreSQL database connection. This is an alias for Connect to maintain compatibility.
func SoftDeleteScope ¶
SoftDeleteScope applies soft delete scope to queries.
func TenantScope ¶
TenantScope applies tenant filtering to queries.
func Transaction ¶
Transaction executes a function within a database transaction.
func WithTimeout ¶
WithTimeout creates a context with timeout for database operations.
Types ¶
type ConnectionStats ¶
type ConnectionStats struct {
OpenConnections int `json:"open_connections"`
InUse int `json:"in_use"`
Idle int `json:"idle"`
WaitCount int `json:"wait_count"`
WaitDuration int `json:"wait_duration_ms"`
MaxIdleClosed int `json:"max_idle_closed"`
MaxLifetime int `json:"max_lifetime_closed"`
}
ConnectionStats represents database connection statistics.
func GetConnectionStats ¶
func GetConnectionStats(db *gorm.DB) (*ConnectionStats, error)
GetConnectionStats returns database connection statistics.
type Manager ¶
type Manager interface {
// Connect establishes a database connection
Connect(cfg config.DatabaseConfig) (*gorm.DB, error)
// HealthCheck performs a database health check
HealthCheck(db *gorm.DB) error
// Close closes the database connection
Close(db *gorm.DB) error
// Migrate runs database migrations
Migrate(db *gorm.DB, models ...interface{}) error
}
Manager provides database management functionality.
type PaginationResult ¶
type PaginationResult struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
Total int64 `json:"total"`
TotalPages int `json:"total_pages"`
Data interface{} `json:"data"`
}
PaginationResult represents paginated query results.
func PaginateQuery ¶
func PaginateQuery(db *gorm.DB, page, pageSize int, result interface{}) (*PaginationResult, error)
PaginateQuery executes a paginated query.