Documentation
¶
Index ¶
Constants ¶
View Source
const (
DriverFieldName = "db.driver"
)
Variables ¶
View Source
var Component = &component.Component{ Init: component.StepFunc(func(container container.Container) error { return container.Provides( NewConfig, NewClient, ) }), BindFlags: component.BindFlags(func(flagSet flag.FlagSet, container container.Container) error { return container.Invoke(func(config *Config) { allDrivers := drivers.Keys() if len(allDrivers) == 1 { DriverDefault = allDrivers[0] } flagSet.StringVar(&config.driver, DriverFieldName, DriverDefault, fmt.Sprintf("db client driver. Available drivers: [%s]", strings.Join(allDrivers, ","))) }) }), Configuration: component.StepFunc(func(container container.Container) error { return container.Invoke(Configuration) }), }
Component is a ready-to-use Compogo component that provides a database client. It automatically:
- Registers Config and client factory in the DI container
- Adds command-line flags for driver selection
- Discovers available drivers from the registry
- Creates the appropriate client based on the selected driver
Usage:
compogo.WithComponents(
db_client.Component,
// ... driver components (postgres, mysql, etc.)
)
The actual client instance can be injected into any component that needs it:
type UserService struct {
db db_client.Client
}
View Source
var (
DriverDefault = ""
)
Functions ¶
func Registration ¶
Registration registers a new database driver and its constructor function. This function should be called during driver package initialization. Once registered, the driver becomes available for use via the --db.driver flag.
Types ¶
type Client ¶
type Client interface {
io.Closer
Query(string, ...interface{}) (*sql.Rows, error)
QueryRow(string, ...interface{}) *sql.Row
Exec(string, ...interface{}) (sql.Result, error)
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
SQL() *sql.DB
Driver() Driver
}
Client defines the interface for database operations. It mirrors the standard database/sql package while allowing for different driver implementations and decorators.
type Config ¶
type Config struct {
Driver Driver
// contains filtered or unexported fields
}
func Configuration ¶
func Configuration(config *Config, configurator configurator.Configurator) (*Config, error)
Click to show internal directories.
Click to hide internal directories.