database

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package database provides database connection management and utilities.

Package database provides database connection management and utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(cfg Config) (*sql.DB, error)

Connect establishes a database connection with the given configuration. It sets connection pool settings and verifies the connection with a ping. If ping fails, the database connection is closed to prevent leaks.

Types

type Config

type Config struct {
	Driver             string        // Database driver name (e.g., "postgres", "mysql").
	ConnectionString   string        // Connection string for the database.
	MaxOpenConnections int           // Maximum number of open connections to the database.
	MaxIdleConnections int           // Maximum number of idle connections in the pool.
	ConnMaxLifetime    time.Duration // Maximum amount of time a connection may be reused.
	ConnMaxIdleTime    time.Duration // Maximum amount of time a connection may be idle.
}

Config holds database configuration settings.

type Querier

type Querier interface {
	// ExecContext executes a query without returning any rows.
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	// QueryContext executes a query that returns rows, typically a SELECT.
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	// QueryRowContext executes a query that is expected to return at most one row.
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

Querier represents a database query executor (either *sql.DB or *sql.Tx).

func GetTx

func GetTx(ctx context.Context, db *sql.DB) Querier

GetTx retrieves a transaction from context, or returns the DB connection.

type TxManager

type TxManager interface {
	// WithTx executes the function within a database transaction.
	// If a transaction is already present in the context, it reuses it.
	WithTx(ctx context.Context, fn func(ctx context.Context) error) error
}

TxManager manages database transactions.

func NewTxManager

func NewTxManager(db *sql.DB) TxManager

NewTxManager creates a new TxManager for the given database.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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