mirror of
https://github.com/hanzo-fi/go-libs.git
synced 2026-10-30 20:53:44 +00:00
43 lines
1.3 KiB
Go
43 lines
1.3 KiB
Go
package storagefx
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/uptrace/bun"
|
|
"go.uber.org/fx"
|
|
|
|
logging "github.com/hanzo-fi/go-libs/v5/pkg/observe/log"
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/storage/bun/connect"
|
|
"github.com/hanzo-fi/go-libs/v5/pkg/storage/bun/debug"
|
|
)
|
|
|
|
func BunConnectModule(connectionOptions connect.ConnectionOptions, debugMode bool) fx.Option {
|
|
return fx.Options(
|
|
fx.Provide(func(logger logging.Logger) (*bun.DB, error) {
|
|
hooks := make([]bun.QueryHook, 0)
|
|
debugHook := debug.NewQueryHook()
|
|
debugHook.Debug = debugMode
|
|
hooks = append(hooks, debugHook)
|
|
|
|
logger.
|
|
WithFields(map[string]any{
|
|
"max-idle-conns": connectionOptions.MaxIdleConns,
|
|
"max-open-conns": connectionOptions.MaxOpenConns,
|
|
"max-conn-max-idle-time": connectionOptions.ConnMaxIdleTime,
|
|
"conn-max-lifetime": connectionOptions.ConnMaxLifetime,
|
|
}).
|
|
Infof("opening database connection")
|
|
|
|
return connect.OpenSQLDB(logging.ContextWithLogger(context.Background(), logger), connectionOptions, hooks...)
|
|
}),
|
|
fx.Invoke(func(lc fx.Lifecycle, db *bun.DB, logger logging.Logger) {
|
|
lc.Append(fx.Hook{
|
|
OnStop: func(ctx context.Context) error {
|
|
logger.Info("closing database connection...")
|
|
return db.Close()
|
|
},
|
|
})
|
|
}),
|
|
)
|
|
}
|