component

package
v0.1.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 29, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package component provides PostgreSQL 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 connection pool.

This component only establishes the connection pool. Schema migration should be registered as a separate bootstrap.Func step immediately after:

AddParallel(component.Component()).
Add(bootstrap.Func("migrate", func() error {
    db.AutoMigrate(...)
    return nil
}, nil))
Example

ExampleComponent shows how Component() is used in a bootstrap registration chain. It reads PostgreSQL connection parameters from env during Init():

db.host, db.port, db.name, db.schema, db.username, db.password

NewPostgres performs an eager connectivity check — if the database is unreachable Init() returns an error immediately so bootstrap aborts startup.

Schema migration should be registered as a separate step immediately after:

bootstrap.New(envComp.Component(...), logComp.Component()).
    Add(dbComp.Component()).
    Add(bootstrap.Func("migrate", func() error {
        db.AutoMigrate(...)
        return nil
    }, nil)).
    Run()

postgres.InitDefault uses sync.Once — only the first Init call per process takes effect. In this example the database is unreachable (testdata points to 127.0.0.1:1) so Init returns an error.

package main

import (
	"fmt"

	dbComp "github.com/phcp-tech/common-library-golang/dbsqlc/postgres/component"
)

func main() {
	c := dbComp.Component()
	fmt.Println(c != nil)
}
Output:
true

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL