database

package
v0.1.0-20260312-120723... Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDSN

func BuildDSN(
	dbType types.DBType,
	host string,
	dbName,
	user,
	password string,
	options map[string]string,
) string

BuildDSN constructs a DSN (Data Source Name) string based on input parameters. host should be in the format "host:port" or the domain if it is available BuildDSN safely builds a DSN string for PostgreSQL, MySQL, and MongoDB.

func ExtractDBNameFromDSN

func ExtractDBNameFromDSN(dbType types.DBType, dsn string) (string, error)

ExtractDBNameFromDSN extracts the database name from a DSN string.

func GetColumns

func GetColumns(rows Rows) ([]string, error)

Helper function to check if the rows implement the ExtendedRows interface This interface is required to access column names from the result set

func MonitorDB

func MonitorDB[T Database](ctx context.Context, dbInstance T)

MonitorDB continuously monitors the database connection.

func NewDBOptions

func NewDBOptions[T DBConfig](opts ...DBOption) T

NewDBOptions creates a new DBConfig with the given options.

func RowsToSlice

func RowsToSlice(rows Rows) ([][]string, error)

RowsToSlice converts database.Rows object into a 2D slice of strings.

Each row in the database.Rows object is converted into a slice of strings, where each string represents a column value in that row.

Returns a 2D slice of strings representing the data from the database.Rows object, and an error if any occurs during the conversion process.

Types

type DBConfig

type DBConfig interface {
	GetDSN() string
	GetMaxConns() int
	IsDebugMode() bool
	GetQueryProvider() string
	GetLogger() *log.Log
	GetCheckAliveInterval() time.Duration
	GetStartMonitor() bool

	GetMinConns() int
	GetMaxConnIdleTime() time.Duration
	GetMaxConnLifetime() time.Duration
	GetHealthCheckPeriod() time.Duration
	// contains filtered or unexported methods
}

DBConfig interface defines the common configuration for any database.

type DBOption

type DBOption func(c DBConfig)

DBOption defines a function that modifies a DBConfig.

func WithCheckAliveInterval

func WithCheckAliveInterval(interval time.Duration) DBOption

WithCheckAliveInterval sets the interval for checking the health of the database connection.

func WithDSN

func WithDSN(dsn string) DBOption

WithDSN sets the DSN for any database.

func WithDebugMode

func WithDebugMode(debug bool) DBOption

WithDebugMode enables or disables debug mode for any database.

func WithHealthCheckPeriod

func WithHealthCheckPeriod(d time.Duration) DBOption

WithHealthCheckPeriod sets the health check period for connections.

func WithLogger

func WithLogger(logger *log.Log) DBOption

WithLogger sets the logger for any database.

func WithMaxConnIdleTime

func WithMaxConnIdleTime(d time.Duration) DBOption

WithMaxConnIdleTime sets the max time a connection can be idle before being closed. Recommended: 30s-5min for serverless databases like Neon.

func WithMaxConnLifetime

func WithMaxConnLifetime(d time.Duration) DBOption

WithMaxConnLifetime sets the max lifetime for a connection before it's closed. Recommended: 1h for serverless databases.

func WithMaxConns

func WithMaxConns(maxConns int) DBOption

WithMaxConns sets the maximum number of connections for any database.

func WithMinConns

func WithMinConns(minConns int) DBOption

WithMinConns sets the minimum number of connections in the pool. Set to 0 for serverless databases like Neon to allow pool to be fully idle.

func WithNeonDefaults

func WithNeonDefaults() DBOption

WithNeonDefaults applies recommended settings for Neon PostgreSQL serverless. MinConns=0, MaxConnIdleTime=30s, MaxConnLifetime=1h, HealthCheckPeriod=0

func WithQueryProvider

func WithQueryProvider(queryProvider types.DBType) DBOption

WithQueryProvider sets the name of the provider for retrieving SQL queries.

func WithStartMonitor

func WithStartMonitor(start bool) DBOption

WithStartMonitor toggles whether health monitoring starts automatically.

type DBOptions

type DBOptions any

DBOptions defines a generic interface for database-specific options.

type Database

type Database interface {
	Connect(ctx context.Context) error
	Close() error
	Query(ctx context.Context, query string, args ...any) (Rows, error)
	QueryRow(ctx context.Context, query string, args ...any) Row
	Exec(ctx context.Context, query string, args ...any) (ExecResult, error)
	BeginTransaction(ctx context.Context) (Transaction, error)
	IsDebugQuery(query string, args ...any)
	RowsToSlice(rows Rows) ([][]string, error)
	Ping() error
	GetProviderDB() any // GetDB should return the underlying query or database object (e.g., *sqlc.Queries)
	IsQueryProviderAvailable() bool
	FetchStopChannel() <-chan struct{}
	FetchCheckAliveInterval() time.Duration
	StartMonitorEnabled() bool
	StartMonitor()
	StopMonitor()
	GetLogger() *log.Log
}

Database defines a common interface for different database backends.

func NewDatabase

func NewDatabase[T Database, O DBOptions, Q any](
	dbFactory func(O, GenericQueriesFactory[Q]) T,
	queriesFactory GenericQueriesFactory[Q],
	options O,
) (Database, error)

NewDatabase creates and initializes a new database instance using generics.

type ExecResult

type ExecResult interface {
	// RowsAffected returns the number of rows affected by the executed statement.
	RowsAffected() int64
	// LastInsertId returns the ID of the last inserted row, if applicable.
	LastInsertId() int64
}

ExecResult interface defines methods for retrieving information about the result of an executed SQL statement.

type ExtendedRows

type ExtendedRows interface {
	Rows
	Columns() ([]string, error)
}

ExtendedRows extends the Rows interface to include a method for retrieving column names.

type GenericQueriesFactory

type GenericQueriesFactory[Q any] func(db any) Q

GenericQueriesFactory defines a generic interface for database queries.

type MySQLDBOptions

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

MySQLDBOptions struct for MySQL configuration.

func (*MySQLDBOptions) GetCheckAliveInterval

func (m *MySQLDBOptions) GetCheckAliveInterval() time.Duration

GetCheckAliveInterval returns the interval for checking the health of the database connection.

func (*MySQLDBOptions) GetDSN

func (m *MySQLDBOptions) GetDSN() string

GetDSN returns the DSN for MySQL.

func (*MySQLDBOptions) GetLogger

func (m *MySQLDBOptions) GetLogger() *log.Log

GetLogger returns the Logger for MySQL.

func (*MySQLDBOptions) GetMaxConns

func (m *MySQLDBOptions) GetMaxConns() int

GetMaxConns returns the maximum number of connections for MySQL.

func (*MySQLDBOptions) GetQueryProvider

func (m *MySQLDBOptions) GetQueryProvider() string

GetQueryProvider returns the QueryProvider for MySQL.

func (*MySQLDBOptions) GetStartMonitor

func (m *MySQLDBOptions) GetStartMonitor() bool

GetStartMonitor returns whether health monitoring should start automatically.

func (*MySQLDBOptions) IsDebugMode

func (m *MySQLDBOptions) IsDebugMode() bool

IsDebugMode returns whether debug mode is enabled for MySQL.

type MySqlDBTX

type MySqlDBTX interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
	PrepareContext(context.Context, string) (*sql.Stmt, error)
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...interface{}) *sql.Row
}

MySqlDBTX is the common interface for executing database queries using SQLC.

type MySqlQueriesFactory

type MySqlQueriesFactory[T any] func(db MySqlDBTX) T

MySqlQueriesFactory is a generic factory function for creating `Queries` instances.

type PostgresDBOptions

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

PostgresDBOptions struct for PostgreSQL configuration.

func (*PostgresDBOptions) GetCheckAliveInterval

func (p *PostgresDBOptions) GetCheckAliveInterval() time.Duration

GetCheckAliveInterval returns the interval for checking the health of the database connection.

func (*PostgresDBOptions) GetDSN

func (p *PostgresDBOptions) GetDSN() string

GetDSN returns the DSN for PostgreSQL.

func (*PostgresDBOptions) GetHealthCheckPeriod

func (p *PostgresDBOptions) GetHealthCheckPeriod() time.Duration

GetHealthCheckPeriod returns the health check period for connections.

func (*PostgresDBOptions) GetLogger

func (p *PostgresDBOptions) GetLogger() *log.Log

GetLogger returns the Logger for PostgreSQL.

func (*PostgresDBOptions) GetMaxConnIdleTime

func (p *PostgresDBOptions) GetMaxConnIdleTime() time.Duration

GetMaxConnIdleTime returns the max idle time for connections.

func (*PostgresDBOptions) GetMaxConnLifetime

func (p *PostgresDBOptions) GetMaxConnLifetime() time.Duration

GetMaxConnLifetime returns the max lifetime for connections.

func (*PostgresDBOptions) GetMaxConns

func (p *PostgresDBOptions) GetMaxConns() int

GetMaxConns returns the maximum number of connections for PostgreSQL.

func (*PostgresDBOptions) GetMinConns

func (p *PostgresDBOptions) GetMinConns() int

GetMinConns returns the minimum number of connections for PostgreSQL.

func (*PostgresDBOptions) GetQueryProvider

func (p *PostgresDBOptions) GetQueryProvider() string

GetQueryProvider returns the QueryProvider for PostgreSQL.

func (*PostgresDBOptions) GetStartMonitor

func (p *PostgresDBOptions) GetStartMonitor() bool

GetStartMonitor returns whether health monitoring should start automatically.

func (*PostgresDBOptions) IsDebugMode

func (p *PostgresDBOptions) IsDebugMode() bool

IsDebugMode returns whether debug mode is enabled for PostgreSQL.

type PostgresDBTX

type PostgresDBTX interface {
	Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
	Query(context.Context, string, ...interface{}) (pgx.Rows, error)
	QueryRow(context.Context, string, ...interface{}) pgx.Row
}

PostgresDBTX is the common interface for executing database queries using SQLC.

type PostgresQueriesFactory

type PostgresQueriesFactory[T any] func(db PostgresDBTX) T

PostgresQueriesFactory is a generic factory function for creating `Queries` instances.

type Row

type Row interface {
	// Scan reads the columns in the row into the provided values.
	Scan(dest ...any) error
}

Row defines a common interface for a single row result from a database query.

type Rows

type Rows interface {
	// Close closes the resource associated with the set of rows.
	Close() error
	// Next advances to the next row in the result set. Returns true if there is a next row, false otherwise.
	Next() bool
	// Scan reads the columns in the current row into the provided values.
	Scan(dest ...any) error
	// Err returns any error encountered during the iteration over the rows.
	Err() error
}

Rows defines a common interface for query results, providing methods for interacting with a set of rows returned from a database query.

type Transaction

type Transaction interface {
	Query(ctx context.Context, query string, args ...any) (Rows, error)
	QueryRow(ctx context.Context, query string, args ...any) Row
	Exec(ctx context.Context, query string, args ...any) (ExecResult, error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
	IsDebugQuery(query string, args ...any)
	RowsToSlice(rows Rows) ([][]string, error)
	GetTx() any
}

Transaction defines a common interface for database transactions.

Jump to

Keyboard shortcuts

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