Documentation
¶
Overview ¶
Package component provides MySQL lifecycle integration for bootstrap.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Component ¶
func Component() bootstrap.IComponent
Component wraps loadFromEnv as a bootstrap.IComponent. Init calls loadFromEnv; Close closes the default MySQL connection.
Because sql.Open is lazy, Init() always returns nil — the actual network connection is deferred to the first query. Use a PreReady step to ping the database if an eager connectivity check is required:
AddParallel(component.Component()).
PreReady(func() error {
return mysql.Default().PingContext(ctx)
}).
Run()
Example ¶
ExampleComponent shows how Component() is used in a bootstrap registration chain. It reads MySQL connection parameters from env during Init():
db.host, db.port, db.name, db.username, db.password
Unlike PostgreSQL, sql.Open is lazy — Init() always returns nil regardless of whether the server is reachable. Use a PreReady step for an eager ping if needed:
bootstrap.New().
Add(envComp.Component(...)).
Add(logComp.Component()).
Add(dbComp.Component()).
PreReady(func() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
return mysql.Default().PingContext(ctx)
}).
Run()
mysql.InitDefault uses sync.Once — only the first Init call per process takes effect.
package main
import (
"fmt"
dbComp "github.com/phcp-tech/common-library-golang/dbsqlc/mysql/component"
)
func main() {
c := dbComp.Component()
fmt.Println(c != nil)
}
Output: true
Types ¶
This section is empty.