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, func(client client.Client) Client { return client }, ) }), 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 NewClient ¶
func NewClient(config *Config, container container.Container, logger logger.Logger) (client.Client, error)
NewClient creates a database client instance based on the configured driver. It looks up the Getter for the selected driver, invokes it with the container, and returns the created client. Returns an error if the driver or its getter is not found, or if client creation fails.
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 Config ¶
func Configuration ¶
func Configuration(config *Config, configurator configurator.Configurator) (*Config, error)
Click to show internal directories.
Click to hide internal directories.