database

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 23 Imported by: 0

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

func IsSupported(dbType string) bool

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.

func New

func New(dbType string) (DB, error)

New is a factory function that returns an implementation of the DB interface based on the registered database type string.

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

func (m *MongoDB) Close() error

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

func (m *MongoDB) HealthCheck(ctx context.Context, query string) error

HealthCheck executes a custom MongoDB command JSON string (e.g. `{"dbStats": 1}`) if provided, or defaults to listing database collection names.

func (*MongoDB) Ping

func (m *MongoDB) Ping(ctx context.Context) error

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 Oracle

type Oracle struct {
	SQLBase
}

func (*Oracle) Connect

func (o *Oracle) Connect(ctx context.Context, cfg config.DatabaseConfig, decryptedPassword string) error

type Postgres

type Postgres struct {
	SQLBase
}

func (*Postgres) Connect

func (p *Postgres) Connect(ctx context.Context, cfg config.DatabaseConfig, decryptedPassword string) error

type SQLBase

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

SQLBase provides shared ping, query execution, connection limits, and close logic for SQL drivers.

func (*SQLBase) Close

func (b *SQLBase) Close() error

func (*SQLBase) HealthCheck

func (b *SQLBase) HealthCheck(ctx context.Context, query string) error

func (*SQLBase) Ping

func (b *SQLBase) Ping(ctx context.Context) error

func (*SQLBase) SetDB

func (b *SQLBase) SetDB(db *sql.DB)

SetDB assigns the underlying sql.DB instance and configures optimal diagnostic connection limits.

type SQLServer

type SQLServer struct {
	SQLBase
}

func (*SQLServer) Connect

func (s *SQLServer) Connect(ctx context.Context, cfg config.DatabaseConfig, decryptedPassword string) error

type SQLite

type SQLite struct {
	SQLBase
}

func (*SQLite) Connect

func (s *SQLite) Connect(ctx context.Context, cfg config.DatabaseConfig, decryptedPassword string) error

Jump to

Keyboard shortcuts

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