Documentation
¶
Index ¶
- func BuildDSN(dbType types.DBType, host string, dbName, user, password string, ...) string
- func ExtractDBNameFromDSN(dbType types.DBType, dsn string) (string, error)
- func GetColumns(rows Rows) ([]string, error)
- func MonitorDB[T Database](ctx context.Context, dbInstance T)
- func NewDBOptions[T DBConfig](opts ...DBOption) T
- func RowsToSlice(rows Rows) ([][]string, error)
- type DBConfig
- type DBOption
- func WithCheckAliveInterval(interval time.Duration) DBOption
- func WithDSN(dsn string) DBOption
- func WithDebugMode(debug bool) DBOption
- func WithHealthCheckPeriod(d time.Duration) DBOption
- func WithLogger(logger *log.Log) DBOption
- func WithMaxConnIdleTime(d time.Duration) DBOption
- func WithMaxConnLifetime(d time.Duration) DBOption
- func WithMaxConns(maxConns int) DBOption
- func WithMinConns(minConns int) DBOption
- func WithNeonDefaults() DBOption
- func WithQueryProvider(queryProvider types.DBType) DBOption
- func WithStartMonitor(start bool) DBOption
- type DBOptions
- type Database
- type ExecResult
- type ExtendedRows
- type GenericQueriesFactory
- type MySQLDBOptions
- func (m *MySQLDBOptions) GetCheckAliveInterval() time.Duration
- func (m *MySQLDBOptions) GetDSN() string
- func (m *MySQLDBOptions) GetLogger() *log.Log
- func (m *MySQLDBOptions) GetMaxConns() int
- func (m *MySQLDBOptions) GetQueryProvider() string
- func (m *MySQLDBOptions) GetStartMonitor() bool
- func (m *MySQLDBOptions) IsDebugMode() bool
- type MySqlDBTX
- type MySqlQueriesFactory
- type PostgresDBOptions
- func (p *PostgresDBOptions) GetCheckAliveInterval() time.Duration
- func (p *PostgresDBOptions) GetDSN() string
- func (p *PostgresDBOptions) GetHealthCheckPeriod() time.Duration
- func (p *PostgresDBOptions) GetLogger() *log.Log
- func (p *PostgresDBOptions) GetMaxConnIdleTime() time.Duration
- func (p *PostgresDBOptions) GetMaxConnLifetime() time.Duration
- func (p *PostgresDBOptions) GetMaxConns() int
- func (p *PostgresDBOptions) GetMinConns() int
- func (p *PostgresDBOptions) GetQueryProvider() string
- func (p *PostgresDBOptions) GetStartMonitor() bool
- func (p *PostgresDBOptions) IsDebugMode() bool
- type PostgresDBTX
- type PostgresQueriesFactory
- type Row
- type Rows
- type Transaction
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 ¶
ExtractDBNameFromDSN extracts the database name from a DSN string.
func GetColumns ¶
Helper function to check if the rows implement the ExtendedRows interface This interface is required to access column names from the result set
func NewDBOptions ¶
NewDBOptions creates a new DBConfig with the given options.
func RowsToSlice ¶
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 ¶
WithCheckAliveInterval sets the interval for checking the health of the database connection.
func WithDebugMode ¶
WithDebugMode enables or disables debug mode for any database.
func WithHealthCheckPeriod ¶
WithHealthCheckPeriod sets the health check period for connections.
func WithLogger ¶
WithLogger sets the logger for any database.
func WithMaxConnIdleTime ¶
WithMaxConnIdleTime sets the max time a connection can be idle before being closed. Recommended: 30s-5min for serverless databases like Neon.
func WithMaxConnLifetime ¶
WithMaxConnLifetime sets the max lifetime for a connection before it's closed. Recommended: 1h for serverless databases.
func WithMaxConns ¶
WithMaxConns sets the maximum number of connections for any database.
func WithMinConns ¶
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 ¶
WithQueryProvider sets the name of the provider for retrieving SQL queries.
func WithStartMonitor ¶
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 ¶
ExtendedRows extends the Rows interface to include a method for retrieving column names.
type GenericQueriesFactory ¶
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 ¶
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.