database

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 8, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package database provides database connection pooling, migrations, and health checks. It supports PostgreSQL with GORM and includes utilities for testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(db *gorm.DB) error

Close closes the database connection using the global manager.

func Connect

func Connect(cfg config.DatabaseConfig) (*gorm.DB, error)

Connect establishes a database connection using the global manager.

func HealthCheck

func HealthCheck(db *gorm.DB) error

HealthCheck performs a database health check using the global manager.

func Migrate

func Migrate(db *gorm.DB, models ...interface{}) error

Migrate runs database migrations 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 OrderBy

func OrderBy(column, direction string) func(db *gorm.DB) *gorm.DB

OrderBy applies ordering to queries.

func Paginate

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB

Paginate applies pagination to a GORM query.

func Search(columns []string, query string) func(db *gorm.DB) *gorm.DB

Search applies search filtering to queries.

func SoftDeleteScope

func SoftDeleteScope() func(db *gorm.DB) *gorm.DB

SoftDeleteScope applies soft delete scope to queries.

func TenantScope

func TenantScope(tenantID string) func(db *gorm.DB) *gorm.DB

TenantScope applies tenant filtering to queries.

func Transaction

func Transaction(db *gorm.DB, fn func(*gorm.DB) error) error

Transaction executes a function within a database transaction.

func WithTimeout

func WithTimeout(ctx context.Context, timeout time.Duration) (context.Context, context.CancelFunc)

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.

func NewManager

func NewManager() Manager

NewManager creates a new database manager.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL