database

package
v0.0.0-...-0d7d7e8 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReplaceGlobals

func ReplaceGlobals(databases Databases) func()

ReplaceGlobals replaces the global Databases struct with the provided one

func StartHealthMonitoring

func StartHealthMonitoring(name string, interval time.Duration, checker HealthCheckable, recover func()) func()

StartHealthMonitoring is a convenience function to create a manager, add a target, and start monitoring

Types

type Databases

type Databases struct {
	// contains filtered or unexported fields
}

Databases holds all the database connections

func DB

func DB() *Databases

DB returns the global Databases struct

func (*Databases) CloseAll

func (db *Databases) CloseAll()

CloseAll closes all database connections

func (*Databases) Postgres

func (db *Databases) Postgres() PostgresDB

Postgres returns the postgres database connection

func (*Databases) Redis

func (db *Databases) Redis() RedisDB

Redis returns the redis database connection

func (*Databases) SetPostgres

func (db *Databases) SetPostgres(postgres PostgresDB)

SetPostgres sets the postgres database connection while preserving other connections

func (*Databases) SetRedis

func (db *Databases) SetRedis(redis RedisDB)

SetRedis sets the redis database connection while preserving other connections

type HealthCheckable

type HealthCheckable interface {
	IsHealthy() bool
}

HealthCheckable represents any database connection that can be health-checked

type HealthMonitor

type HealthMonitor struct {
	// contains filtered or unexported fields
}

HealthMonitor manages multiple health targets with a single goroutine

func NewHealthMonitor

func NewHealthMonitor(interval time.Duration) *HealthMonitor

NewHealthMonitor creates a new health check manager with the given interval

func (*HealthMonitor) AddTarget

func (m *HealthMonitor) AddTarget(name string, checker HealthCheckable, recover func())

AddTarget registers a new health target with the monitor

func (*HealthMonitor) Start

func (m *HealthMonitor) Start() func()

Start begins the health check monitoring for all registered checkers

type HealthTarget

type HealthTarget struct {
	Name    string
	Checker HealthCheckable
	Recover func()
}

HealthTarget represents a registered health target with its settings

type PostgresDB

type PostgresDB struct {
	DB *sqlx.DB
}

func NewPostgresDB

func NewPostgresDB(db SqlDatabase) PostgresDB

NewPostgresDB create a new Postgres DB.

func (PostgresDB) Close

func (p PostgresDB) Close()

Close closes the database connection.

func (PostgresDB) IsHealthy

func (p PostgresDB) IsHealthy() bool

IsHealthy checks if the database connection is healthy by running a simple query.

type RedisDB

type RedisDB struct {
	Client *redis.Client
}

func NewRedisDB

func NewRedisDB() RedisDB

NewRedisDB creates a new Redis client.

func (RedisDB) Close

func (r RedisDB) Close()

Close closes the Redis connection.

func (RedisDB) IsHealthy

func (r RedisDB) IsHealthy() bool

IsHealthy checks if the Redis connection is healthy by running a ping command.

type Sql

type Sql struct {
	SqlCredentials
}

Sql is a struct that holds the credentials for a SQL database.

func (Sql) Connect

func (s Sql) Connect() (*sqlx.DB, error)

Connect creates a new SQL connection.

type SqlCredentials

type SqlCredentials struct {
	Host     string `json:"url,omitempty"`
	Port     string `json:"port,omitempty"`
	User     string `json:"user,omitempty"`
	Password string `json:"password,omitempty"`
	DbName   string `json:"dbname,omitempty"`
}

SqlCredentials holds the credentials for a SQL database.

type SqlDatabase

type SqlDatabase interface {
	Connect() (*sqlx.DB, error)
}

SqlDatabase is an interface for a SQL database.

func NewSqlDatabase

func NewSqlDatabase(credentials SqlCredentials) SqlDatabase

NewSqlDatabase creates a new SQL database.

Jump to

Keyboard shortcuts

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