Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Component = &component.Component{ Init: component.StepFunc(func(container container.Container) error { return container.Provides( NewConfig, NewGenerator, ) }), Configuration: component.StepFunc(func(container container.Container) error { return container.Invoke(Configuration) }), }
Component is a ready-to-use Compogo component that provides a SQL query builder. It automatically:
- Registers Config and Generator in the DI container
- Applies configuration during Configuration phase
- Makes the generator available for repositories and services
Usage:
compogo.WithComponents(
db_client.Component, // database client (provides driver name)
db_sql_generator.Component, // SQL query builder
// ... driver components (postgres, mysql, etc.)
)
The driver name is automatically taken from db-client configuration and used to select the appropriate SQL dialect.
In your repository component:
type UserRepository struct {
db db_client.Client
generator *goqu.DialectWrapper
}
func NewUserRepository(container container.Container) (*UserRepository, error) {
var repo UserRepository
err := container.Invoke(func(db db_client.Client, gen *goqu.DialectWrapper) {
repo = UserRepository{db: db, generator: gen}
})
return &repo, err
}
Functions ¶
func NewGenerator ¶
NewGenerator creates a new goqu dialect wrapper for the configured database driver. It performs the following steps:
- Looks up the dialect alias for the configured driver
- Returns a goqu.DialectWrapper ready for building SQL queries
The generator should be used in repositories to construct type-safe SQL queries.
Example:
type UserRepository struct {
db db_client.Client
generator *goqu.DialectWrapper
}
func (r *UserRepository) GetUsers(ctx context.Context) ([]User, error) {
query := r.generator.From("users").Where(goqu.C("active").IsTrue())
sql, _, _ := query.ToSQL()
return r.db.QueryContext(ctx, sql)
}
func Registration ¶
Registration registers a new database driver and its goqu dialect alias. This function should be called during driver package initialization. The dialect will then be available for use via the generator component.
Example (in postgres driver):
func init() {
db_sql_generator.Registration(Postgres, "postgres")
}