Documentation
¶
Overview ¶
Package database provides database connectivity and operations.
Package database provides database connectivity and operations.
Package database provides database connectivity and operations.
Index ¶
- func Close(db *sql.DB) error
- func Connect(cfg Config) (*sql.DB, error)
- func ConnectWithDSN(dsn string) (*sql.DB, error)
- func Ping(db *sql.DB) error
- type Config
- type Connection
- type DatabaseConfig
- type DatabaseType
- type Migration
- type Migrator
- type MongoDBConfig
- type MongoDBConnection
- type PoolStats
- type PostgresConfig
- type PostgresConnection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConnectWithDSN ¶
ConnectWithDSN connects using a full DSN string.
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 ¶
NewMigrator creates a new Migrator instance.
func (*Migrator) MigrateDown ¶
MigrateDown rolls back the last applied migration.
func (*Migrator) MigrateReset ¶
MigrateReset rolls back all migrations and re-applies them.
type MongoDBConfig ¶
MongoDBConfig holds MongoDB connection configuration.
func DefaultMongoDBConfig ¶
func DefaultMongoDBConfig() *MongoDBConfig
DefaultMongoDBConfig returns a MongoDBConfig with sensible defaults.
type MongoDBConnection ¶
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 ¶
PoolStats returns current connection pool statistics.
func GetPoolStats ¶
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 ¶
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. |