Documentation
¶
Overview ¶
Package setup provides database connection setup with repository initialization. This package bridges the database and repository packages to avoid import cycles.
Index ¶
- type Connection
- func MustNewConnection(cfg database.DatabaseConfig) Connection
- func MustNewConnectionWithLogger(cfg database.DatabaseConfig, logger *slog.Logger) Connection
- func NewConnection(cfg database.DatabaseConfig) (Connection, error)
- func NewConnectionWithLogger(cfg database.DatabaseConfig, logger *slog.Logger) (Connection, error)
- type MongoDBConnection
- func (c *MongoDBConnection) Close() error
- func (c *MongoDBConnection) DB() *sql.DB
- func (c *MongoDBConnection) MongoClient() *mongodb.Client
- func (c *MongoDBConnection) Ping() error
- func (c *MongoDBConnection) Repositories() *repository.Repositories
- func (c *MongoDBConnection) Type() database.DatabaseType
- type PostgresConnection
- func (c *PostgresConnection) Close() error
- func (c *PostgresConnection) DB() *sql.DB
- func (c *PostgresConnection) MongoClient() *mongodb.Client
- func (c *PostgresConnection) Ping() error
- func (c *PostgresConnection) Repositories() *repository.Repositories
- func (c *PostgresConnection) Type() database.DatabaseType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Connection ¶
type Connection interface {
// Type returns the database type.
Type() database.DatabaseType
// Close closes the database connection.
Close() error
// Ping verifies the database connection is alive.
Ping() error
// Repositories returns the repository interfaces for this connection.
Repositories() *repository.Repositories
// DB returns the raw sql.DB for PostgreSQL connections (nil for MongoDB).
DB() *sql.DB
// MongoClient returns the MongoDB client for MongoDB connections (nil for PostgreSQL).
MongoClient() *mongodb.Client
}
Connection represents a database connection with repositories.
func MustNewConnection ¶
func MustNewConnection(cfg database.DatabaseConfig) Connection
MustNewConnection creates a new database connection and panics on error.
func MustNewConnectionWithLogger ¶
func MustNewConnectionWithLogger(cfg database.DatabaseConfig, logger *slog.Logger) Connection
MustNewConnectionWithLogger creates a new database connection with a logger and panics on error.
func NewConnection ¶
func NewConnection(cfg database.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.
func NewConnectionWithLogger ¶
func NewConnectionWithLogger(cfg database.DatabaseConfig, logger *slog.Logger) (Connection, error)
NewConnectionWithLogger creates a new database connection with a custom logger.
type MongoDBConnection ¶
type MongoDBConnection struct {
// contains filtered or unexported fields
}
MongoDBConnection wraps a MongoDB connection with repository adapters.
func (*MongoDBConnection) Close ¶
func (c *MongoDBConnection) Close() error
Close closes the MongoDB connection.
func (*MongoDBConnection) DB ¶
func (c *MongoDBConnection) DB() *sql.DB
DB returns nil for MongoDB connections.
func (*MongoDBConnection) MongoClient ¶
func (c *MongoDBConnection) MongoClient() *mongodb.Client
MongoClient returns the MongoDB client.
func (*MongoDBConnection) Ping ¶
func (c *MongoDBConnection) Ping() error
Ping verifies the MongoDB connection is alive.
func (*MongoDBConnection) Repositories ¶
func (c *MongoDBConnection) Repositories() *repository.Repositories
Repositories returns the repository interfaces for MongoDB.
func (*MongoDBConnection) Type ¶
func (c *MongoDBConnection) Type() database.DatabaseType
Type returns DatabaseTypeMongoDB.
type PostgresConnection ¶
type PostgresConnection struct {
// contains filtered or unexported fields
}
PostgresConnection wraps a PostgreSQL database connection with repositories.
func (*PostgresConnection) Close ¶
func (c *PostgresConnection) Close() error
Close closes the PostgreSQL connection.
func (*PostgresConnection) DB ¶
func (c *PostgresConnection) DB() *sql.DB
DB returns the raw sql.DB connection.
func (*PostgresConnection) MongoClient ¶
func (c *PostgresConnection) MongoClient() *mongodb.Client
MongoClient returns nil for PostgreSQL connections.
func (*PostgresConnection) Ping ¶
func (c *PostgresConnection) Ping() error
Ping verifies the PostgreSQL connection is alive.
func (*PostgresConnection) Repositories ¶
func (c *PostgresConnection) Repositories() *repository.Repositories
Repositories returns the repository interfaces for PostgreSQL.
func (*PostgresConnection) Type ¶
func (c *PostgresConnection) Type() database.DatabaseType
Type returns DatabaseTypePostgres.