Documentation
¶
Overview ¶
Package db provides utilities for working with databases.
Index ¶
- Constants
- func CheckMongoHealth(ctx context.Context, client *mongo.Client) error
- func CheckPostgresHealth(ctx context.Context, pool *pgxpool.Pool) error
- func CheckSQLiteHealth(ctx context.Context, db *sql.DB) error
- func ExecutePostgresTransaction(ctx context.Context, pool *pgxpool.Pool, fn func(tx pgx.Tx) error, ...) error
- func ExecuteSQLTransaction(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error, ...) error
- func InitMongoClient(ctx context.Context, uri string, timeout time.Duration) (*mongo.Client, error)
- func InitPostgresPool(ctx context.Context, config PostgresConfig) (*pgxpool.Pool, error)
- func InitSQLiteDB(ctx context.Context, uri string, timeout, connMaxLifetime time.Duration, ...) (*sql.DB, error)
- func IsTransientError(err error) bool
- func LogDatabaseConnection(ctx context.Context, logger *logging.ContextLogger, dbType string)
- type MongoClientInterface
- type PgxPoolInterface
- type PgxTxInterface
- type PostgresConfig
- type RetryConfig
- type SQLDBInterface
- type SQLTxInterface
Constants ¶
const DefaultTimeout = 30 * time.Second
DefaultTimeout is the default timeout for database operations
Variables ¶
This section is empty.
Functions ¶
func CheckMongoHealth ¶
CheckMongoHealth checks if a MongoDB connection is healthy. Parameters:
- ctx: The context for the operation
- client: The MongoDB client
Returns:
- error: An error if the health check fails
func CheckPostgresHealth ¶
CheckPostgresHealth checks if a PostgreSQL connection is healthy. Parameters:
- ctx: The context for the operation
- pool: The PostgreSQL connection pool
Returns:
- error: An error if the health check fails
func CheckSQLiteHealth ¶
CheckSQLiteHealth checks if a SQLite connection is healthy. Parameters:
- ctx: The context for the operation
- db: The SQLite database connection
Returns:
- error: An error if the health check fails
func ExecutePostgresTransaction ¶
func ExecutePostgresTransaction(ctx context.Context, pool *pgxpool.Pool, fn func(tx pgx.Tx) error, retryConfig ...RetryConfig) error
ExecutePostgresTransaction executes a function within a PostgreSQL transaction. Parameters:
- ctx: The context for the operation
- pool: The PostgreSQL connection pool
- fn: The function to execute within the transaction
- retryConfig: Optional retry configuration for transient errors
Returns:
- error: An error if the transaction fails
func ExecuteSQLTransaction ¶
func ExecuteSQLTransaction(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error, retryConfig ...RetryConfig) error
ExecuteSQLTransaction executes a function within a SQL transaction. Parameters:
- ctx: The context for the operation
- db: The SQL database connection
- fn: The function to execute within the transaction
- retryConfig: Optional retry configuration for transient errors
Returns:
- error: An error if the transaction fails
func InitMongoClient ¶
InitMongoClient initializes a MongoDB client. Parameters:
- ctx: The context for the operation
- uri: The MongoDB connection URI
- timeout: The timeout for the connection operation
Returns:
- *mongo.Client: The initialized MongoDB client
- error: An error if initialization fails
func InitPostgresPool ¶
InitPostgresPool initializes a PostgreSQL connection pool. Parameters:
- ctx: The context for the operation
- config: The PostgreSQL connection configuration
Returns:
- *pgxpool.Pool: The initialized PostgreSQL connection pool
- error: An error if initialization fails
func InitSQLiteDB ¶
func InitSQLiteDB(ctx context.Context, uri string, timeout, connMaxLifetime time.Duration, maxOpenConns, maxIdleConns int) (*sql.DB, error)
InitSQLiteDB initializes a SQLite database connection. Parameters:
- ctx: The context for the operation
- uri: The SQLite connection URI
- timeout: The timeout for the connection operation
- maxOpenConns: The maximum number of open connections
- maxIdleConns: The maximum number of idle connections
- connMaxLifetime: The maximum lifetime of a connection
Returns:
- *sql.DB: The initialized SQLite database connection
- error: An error if initialization fails
func IsTransientError ¶ added in v1.2.0
IsTransientError checks if an error is a transient database error that can be retried.
func LogDatabaseConnection ¶
func LogDatabaseConnection(ctx context.Context, logger *logging.ContextLogger, dbType string)
LogDatabaseConnection logs a successful database connection. Parameters:
- ctx: The context for the operation
- logger: The logger to use
- dbType: The type of database (e.g., "MongoDB", "PostgreSQL", "SQLite")
Types ¶
type MongoClientInterface ¶
type MongoClientInterface interface {
Ping(ctx context.Context, rp *readpref.ReadPref) error
Connect(ctx context.Context) error
Database(name string, opts ...*options.DatabaseOptions) *mongo.Database
}
MongoClientInterface defines the interface for mongo.Client operations
type PgxPoolInterface ¶
type PgxPoolInterface interface {
Ping(ctx context.Context) error
Begin(ctx context.Context) (pgx.Tx, error)
Close()
}
PgxPoolInterface defines the interface for pgxpool.Pool operations
type PgxTxInterface ¶
type PgxTxInterface interface {
Commit(ctx context.Context) error
Rollback(ctx context.Context) error
}
PgxTxInterface defines the interface for pgx.Tx operations
type PostgresConfig ¶ added in v1.2.0
type PostgresConfig struct {
// URI is the PostgreSQL connection URI
URI string
// Timeout is the timeout for connection operations
Timeout time.Duration
// MaxOpenConns is the maximum number of open connections
MaxConns int32
// MinOpenConns is the minimum number of open connections
MinConns int32
// MaxConnLifetime is the maximum lifetime of a connection
MaxConnLifetime time.Duration
// MaxConnIdleTime is the maximum idle time of a connection
MaxConnIdleTime time.Duration
// HealthCheckPeriod is the period between health checks
HealthCheckPeriod time.Duration
}
PostgresConfig holds configuration for a PostgreSQL connection pool.
type RetryConfig ¶ added in v1.2.0
type RetryConfig struct {
// MaxRetries is the maximum number of retry attempts
MaxRetries int
// InitialBackoff is the initial backoff duration
InitialBackoff time.Duration
// MaxBackoff is the maximum backoff duration
MaxBackoff time.Duration
// BackoffFactor is the factor by which the backoff increases
BackoffFactor float64
// Logger is the logger to use for logging retries
Logger *logging.ContextLogger
}
RetryConfig holds configuration for retry operations.
func DefaultRetryConfig ¶ added in v1.2.0
func DefaultRetryConfig() RetryConfig
DefaultRetryConfig returns the default retry configuration.
type SQLDBInterface ¶
type SQLDBInterface interface {
PingContext(ctx context.Context) error
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
SetMaxOpenConns(n int)
SetMaxIdleConns(n int)
SetConnMaxLifetime(d time.Duration)
}
SQLDBInterface defines the interface for sql.DB operations
type SQLTxInterface ¶
SQLTxInterface defines the interface for sql.Tx operations