database

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package database provides database connectivity and operations.

Package database provides database connectivity and operations.

Package database provides database connectivity and operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close(db *sql.DB) error

Close closes the database connection.

func Connect

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

Connect establishes a connection to the PostgreSQL database.

func ConnectWithDSN

func ConnectWithDSN(dsn string) (*sql.DB, error)

ConnectWithDSN connects using a full DSN string.

func Ping

func Ping(db *sql.DB) error

Ping verifies the database connection is alive.

Types

type Config

type Config struct {
	Host     string
	Port     int
	Database string
	User     string
	Password string
	SSLMode  string
}

Config holds database connection configuration.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config with sensible defaults.

type Connection

type Connection interface {
	// Type returns the database type.
	Type() DatabaseType
	// Close closes the database connection.
	Close() error
	// Ping verifies the database connection is alive.
	Ping() error
}

Connection represents a database connection that can be either PostgreSQL or MongoDB. For connections with repository support, use the setup package instead.

func MustNewConnection

func MustNewConnection(cfg DatabaseConfig) Connection

MustNewConnection creates a new database connection and panics on error.

func MustNewConnectionWithLogger

func MustNewConnectionWithLogger(cfg DatabaseConfig, logger *slog.Logger) Connection

MustNewConnectionWithLogger creates a new database connection with a logger and panics on error.

func NewConnection

func NewConnection(cfg DatabaseConfig) (Connection, error)

NewConnection creates a new database connection based on the configuration. For PostgreSQL, it returns a PostgresConnection with an active *sql.DB. For MongoDB, it returns a MongoDBConnection with an active client. Note: For connections with repository initialization, use the setup package.

func NewConnectionWithLogger

func NewConnectionWithLogger(cfg DatabaseConfig, logger *slog.Logger) (Connection, error)

NewConnectionWithLogger creates a new database connection with a custom logger.

type DatabaseConfig

type DatabaseConfig struct {
	// Type specifies which database backend to use
	Type DatabaseType

	// PostgreSQL-specific configuration
	Postgres *PostgresConfig

	// MongoDB-specific configuration
	MongoDB *MongoDBConfig
}

DatabaseConfig holds unified database configuration for any backend.

func DatabaseConfigFromEnv

func DatabaseConfigFromEnv() DatabaseConfig

DatabaseConfigFromEnv creates a DatabaseConfig from environment variables. Environment variables:

  • DATABASE_TYPE: "postgres" or "mongodb" (default: "postgres")
  • For PostgreSQL: DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASSWORD, DB_SSL_MODE
  • For MongoDB: MONGODB_URI, MONGODB_DATABASE

func DefaultDatabaseConfig

func DefaultDatabaseConfig() DatabaseConfig

DefaultDatabaseConfig returns a DatabaseConfig with sensible defaults. Uses PostgreSQL by default for backward compatibility.

type DatabaseType

type DatabaseType string

DatabaseType represents the supported database backends.

const (
	// DatabaseTypePostgres represents PostgreSQL database.
	DatabaseTypePostgres DatabaseType = "postgres"
	// DatabaseTypeMongoDB represents MongoDB database.
	DatabaseTypeMongoDB DatabaseType = "mongodb"
)

func ParseDatabaseType

func ParseDatabaseType(s string) DatabaseType

ParseDatabaseType parses a string into a DatabaseType. Returns DatabaseTypePostgres if the input is empty or invalid.

func (DatabaseType) IsValid

func (dt DatabaseType) IsValid() bool

IsValid returns true if the database type is valid.

func (DatabaseType) String

func (dt DatabaseType) String() string

String returns the string representation of DatabaseType.

type Migration

type Migration struct {
	Version   string
	Name      string
	UpSQL     string
	DownSQL   string
	AppliedAt *time.Time
}

Migration represents a database migration.

type Migrator

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

Migrator handles database migrations.

func NewMigrator

func NewMigrator(db *sql.DB) *Migrator

NewMigrator creates a new Migrator instance.

func (*Migrator) MigrateDown

func (m *Migrator) MigrateDown() error

MigrateDown rolls back the last applied migration.

func (*Migrator) MigrateReset

func (m *Migrator) MigrateReset() error

MigrateReset rolls back all migrations and re-applies them.

func (*Migrator) MigrateUp

func (m *Migrator) MigrateUp() error

MigrateUp runs all pending migrations.

func (*Migrator) Status

func (m *Migrator) Status() ([]Migration, error)

Status returns the current migration status.

type MongoDBConfig

type MongoDBConfig struct {
	URI      string
	Database string
}

MongoDBConfig holds MongoDB connection configuration.

func DefaultMongoDBConfig

func DefaultMongoDBConfig() *MongoDBConfig

DefaultMongoDBConfig returns a MongoDBConfig with sensible defaults.

type MongoDBConnection

type MongoDBConnection struct {
	Client *mongodb.Client
}

MongoDBConnection wraps a MongoDB connection.

func (*MongoDBConnection) Close

func (c *MongoDBConnection) Close() error

Close closes the MongoDB connection.

func (*MongoDBConnection) Ping

func (c *MongoDBConnection) Ping() error

Ping verifies the MongoDB connection is alive.

func (*MongoDBConnection) Type

func (c *MongoDBConnection) Type() DatabaseType

Type returns DatabaseTypeMongoDB.

type PoolStats

type PoolStats struct {
	MaxOpenConnections int
	OpenConnections    int
	InUse              int
	Idle               int
}

PoolStats returns current connection pool statistics.

func GetPoolStats

func GetPoolStats(db *sql.DB) PoolStats

GetPoolStats returns the current connection pool statistics.

type PostgresConfig

type PostgresConfig struct {
	Host     string
	Port     int
	Database string
	User     string
	Password string
	SSLMode  string
}

PostgresConfig holds PostgreSQL connection configuration.

func DefaultPostgresConfig

func DefaultPostgresConfig() *PostgresConfig

DefaultPostgresConfig returns a PostgresConfig with sensible defaults.

type PostgresConnection

type PostgresConnection struct {
	DB *sql.DB
}

PostgresConnection wraps a PostgreSQL database connection.

func (*PostgresConnection) Close

func (c *PostgresConnection) Close() error

Close closes the PostgreSQL connection.

func (*PostgresConnection) Ping

func (c *PostgresConnection) Ping() error

Ping verifies the PostgreSQL connection is alive.

func (*PostgresConnection) Type

func (c *PostgresConnection) Type() DatabaseType

Type returns DatabaseTypePostgres.

Directories

Path Synopsis
Package models defines domain models for the database layer.
Package models defines domain models for the database layer.
Package mongodb provides MongoDB database connectivity and repository operations.
Package mongodb provides MongoDB database connectivity and repository operations.
Package repository implements the repository pattern for data access.
Package repository implements the repository pattern for data access.
Package setup provides database connection setup with repository initialization.
Package setup provides database connection setup with repository initialization.
Package testing provides test helpers for database tests.
Package testing provides test helpers for database tests.

Jump to

Keyboard shortcuts

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