Documentation
¶
Index ¶
- Constants
- Variables
- type Config
- type ConnectionConfig
- type DatabaseService
- type Module
- func (m *Module) Dependencies() []string
- func (m *Module) GetConnection(name string) (*sql.DB, bool)
- func (m *Module) GetConnections() []string
- func (m *Module) GetDefaultConnection() *sql.DB
- func (m *Module) GetDefaultService() DatabaseService
- func (m *Module) GetService(name string) (DatabaseService, bool)
- func (m *Module) Init(app modular.Application) error
- func (m *Module) Name() string
- func (m *Module) ProvidesServices() []modular.ServiceProvider
- func (m *Module) RegisterConfig(app modular.Application) error
- func (m *Module) RequiresServices() []modular.ServiceDependency
- func (m *Module) Start(ctx context.Context) error
- func (m *Module) Stop(ctx context.Context) error
Constants ¶
const Name = "database"
Module name constant
Variables ¶
var ( ErrEmptyDriver = errors.New("database driver cannot be empty") ErrEmptyDSN = errors.New("database connection string (DSN) cannot be empty") ErrDatabaseNotConnected = errors.New("database not connected") )
Define static errors
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Connections contains all defined database connections
Connections map[string]ConnectionConfig `json:"connections" yaml:"connections"`
// Default specifies the name of the default connection
Default string `json:"default" yaml:"default"`
}
Config represents database module configuration
type ConnectionConfig ¶
type ConnectionConfig struct {
// Driver specifies the database driver to use (e.g., "postgres", "mysql")
Driver string `json:"driver" yaml:"driver"`
// DSN is the database connection string
DSN string `json:"dsn" yaml:"dsn"`
// MaxOpenConnections sets the maximum number of open connections to the database
MaxOpenConnections int `json:"max_open_connections" yaml:"max_open_connections"`
// MaxIdleConnections sets the maximum number of idle connections in the pool
MaxIdleConnections int `json:"max_idle_connections" yaml:"max_idle_connections"`
// ConnectionMaxLifetime sets the maximum amount of time a connection may be reused (in seconds)
ConnectionMaxLifetime int `json:"connection_max_lifetime" yaml:"connection_max_lifetime"`
// ConnectionMaxIdleTime sets the maximum amount of time a connection may be idle (in seconds)
ConnectionMaxIdleTime int `json:"connection_max_idle_time" yaml:"connection_max_idle_time"`
}
ConnectionConfig represents configuration for a single database connection
type DatabaseService ¶
type DatabaseService interface {
// Connect establishes the database connection
Connect() error
// Close closes the database connection
Close() error
// DB returns the underlying database connection
DB() *sql.DB
// Ping verifies the database connection is still alive
Ping(ctx context.Context) error
// Stats returns database statistics
Stats() sql.DBStats
// ExecuteContext executes a query without returning any rows
ExecuteContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
// Execute executes a query without returning any rows (using default context)
Execute(query string, args ...interface{}) (sql.Result, error)
// QueryContext executes a query that returns rows
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
// Query executes a query that returns rows (using default context)
Query(query string, args ...interface{}) (*sql.Rows, error)
// QueryRowContext executes a query that returns a single row
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
// QueryRow executes a query that returns a single row (using default context)
QueryRow(query string, args ...interface{}) *sql.Row
// BeginTx starts a transaction
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
// Begin starts a transaction with default options
Begin() (*sql.Tx, error)
}
DatabaseService defines the operations that can be performed with a database
func NewDatabaseService ¶
func NewDatabaseService(config ConnectionConfig) (DatabaseService, error)
NewDatabaseService creates a new database service from configuration
type Module ¶
type Module struct {
// contains filtered or unexported fields
}
Module represents the database module
func (*Module) Dependencies ¶ added in v1.0.8
Dependencies returns the names of modules this module depends on
func (*Module) GetConnection ¶
GetConnection returns a database connection by name
func (*Module) GetConnections ¶
GetConnections returns a list of all available connection names
func (*Module) GetDefaultConnection ¶
GetDefaultConnection returns the default database connection
func (*Module) GetDefaultService ¶ added in v1.0.8
func (m *Module) GetDefaultService() DatabaseService
GetDefaultService returns the default database service
func (*Module) GetService ¶ added in v1.0.8
func (m *Module) GetService(name string) (DatabaseService, bool)
GetService returns a database service by name
func (*Module) Init ¶
func (m *Module) Init(app modular.Application) error
Init initializes the database module
func (*Module) ProvidesServices ¶
func (m *Module) ProvidesServices() []modular.ServiceProvider
ProvidesServices declares services provided by this module
func (*Module) RegisterConfig ¶
func (m *Module) RegisterConfig(app modular.Application) error
RegisterConfig registers the module's configuration structure
func (*Module) RequiresServices ¶
func (m *Module) RequiresServices() []modular.ServiceDependency
RequiresServices declares services required by this module