Documentation
¶
Overview ¶
Package database provides a common interface, driver registry, and specific implementations for database drivers. It abstracts connection management, pinging, and health check execution across SQL and NoSQL engines.
Supported Drivers (registered via init()):
- postgres: PostgreSQL via lib/pq
- mysql: MySQL via go-sql-driver/mysql
- sqlite: SQLite via mattn/go-sqlite3
- sqlserver: SQL Server via microsoft/go-mssqldb
- oracle: Oracle via sijms/go-ora
- mongodb: MongoDB via mongo-driver/v2
Architecture:
- DB interface: Connect, Ping, HealthCheck, Close
- SQLBase: Shared implementation for SQL drivers (SetDB, Ping, HealthCheck, Close)
- Driver registry: Thread-safe map with init()-time registration
TLS configuration builder for database connections.
TLS Modes (from config.SupportedTLSModes):
- ""/disable: No TLS, plaintext connection
- require: TLS enabled, skip server certificate verification
- verify-ca: TLS with CA verification (custom or system CA)
- verify-full: TLS with CA + hostname verification
Security:
- Minimum TLS 1.2 enforced
- Certificate files read via os.OpenRoot to prevent directory traversal
- mTLS requires both client cert and key paths
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsSupported ¶
IsSupported returns true if the specified database type has been registered.
func RegisterDriver ¶
func RegisterDriver(name string, factory DriverFactory)
RegisterDriver registers a database driver factory for a given database type name. Driver implementations call this in their init() functions.
Types ¶
type DB ¶
type DB interface {
Connect(ctx context.Context, cfg config.DatabaseConfig, decryptedPassword string) error
Ping(ctx context.Context) error
HealthCheck(ctx context.Context, query string) error
Close() error
}
DB defines the standard operations required for any supported database type. Note: The ctx parameter in Connect is reserved for future use with context-aware database connectors (e.g., sql.OpenDB with pgx). Currently, sql.Open() does not support context cancellation - the actual connection is established lazily on first query. Callers should use ctx in Ping/HealthCheck to enforce timeouts.
type DriverFactory ¶
type DriverFactory func() DB
DriverFactory is a constructor function that returns a new DB instance.
type MongoDB ¶
type MongoDB struct {
// contains filtered or unexported fields
}
MongoDB implements the DB interface for MongoDB using the v2 driver.
func (*MongoDB) Close ¶
Close disconnects from MongoDB with a 5-second timeout. Uses bounded timeout instead of context.Background() to prevent indefinite blocking.
func (*MongoDB) Connect ¶
func (m *MongoDB) Connect(ctx context.Context, cfg config.DatabaseConfig, decryptedPassword string) error
Connect establishes a connection to MongoDB using the provided configuration. It uses the v2 driver pattern where mongo.Connect takes only options.
func (*MongoDB) HealthCheck ¶
HealthCheck executes a custom MongoDB command JSON string (e.g. `{"dbStats": 1}`) if provided, or defaults to listing database collection names.
type MySQL ¶
type MySQL struct {
SQLBase
}
func (*MySQL) Connect ¶
func (m *MySQL) Connect(ctx context.Context, cfg config.DatabaseConfig, decryptedPassword string) error
Connect establishes a MySQL connection with TLS support. TLS config registration uses a SHA256 hash of addr+cert paths as the key to avoid conflicts when the same host:port uses different certificates.
type SQLBase ¶
type SQLBase struct {
// contains filtered or unexported fields
}
SQLBase provides shared ping, query execution, connection limits, and close logic for SQL drivers.