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, uri string, timeout time.Duration) (*pgxpool.Pool, error)
- func InitSQLiteDB(ctx context.Context, uri string, timeout, connMaxLifetime time.Duration, ...) (*sql.DB, error)
- func LogDatabaseConnection(ctx context.Context, logger *logging.ContextLogger, dbType string)
- type MongoClientInterface
- type PgxPoolInterface
- type PgxTxInterface
- 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) 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
Returns:
- error: An error if the transaction fails
func ExecuteSQLTransaction ¶
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
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 ¶
func InitPostgresPool(ctx context.Context, uri string, timeout time.Duration) (*pgxpool.Pool, error)
InitPostgresPool initializes a PostgreSQL connection pool. Parameters:
- ctx: The context for the operation
- uri: The PostgreSQL connection URI
- timeout: The timeout for the connection operation
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 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 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