MCPcopy Create free account
hub / github.com/go-dev-frame/sponge / InitPostgresql

Function InitPostgresql

internal/database/postgresql.go:15–44  ·  view source on GitHub ↗

InitPostgresql connect postgresql

()

Source from the content-addressed store, hash-verified

13
14// InitPostgresql connect postgresql
15func InitPostgresql() *sgorm.DB {
16 postgresqlCfg := config.Get().Database.Postgresql
17 opts := []postgresql.Option{
18 postgresql.WithMaxIdleConns(postgresqlCfg.MaxIdleConns),
19 postgresql.WithMaxOpenConns(postgresqlCfg.MaxOpenConns),
20 postgresql.WithConnMaxLifetime(time.Duration(postgresqlCfg.ConnMaxLifetime) * time.Minute),
21 }
22 if postgresqlCfg.EnableLog {
23 opts = append(opts,
24 postgresql.WithLogging(logger.Get()),
25 postgresql.WithLogRequestIDKey("request_id"),
26 )
27 }
28
29 if config.Get().App.EnableTrace {
30 opts = append(opts, postgresql.WithEnableTrace())
31 }
32
33 // add custom gorm plugin
34 //opts = append(opts, postgresql.WithGormPlugin(yourPlugin))
35
36 dsn := utils.AdaptivePostgresqlDsn(postgresqlCfg.Dsn)
37 db, err := postgresql.Init(dsn, opts...)
38 if err != nil {
39 panic("init postgresql error: " + err.Error())
40 }
41
42 sgorm.SetDriver("postgresql")
43 return db
44}

Callers 2

InitDBFunction · 0.85
TestInitPostgresqlErrorFunction · 0.85

Calls 12

GetFunction · 0.92
WithMaxIdleConnsFunction · 0.92
WithMaxOpenConnsFunction · 0.92
WithConnMaxLifetimeFunction · 0.92
WithLoggingFunction · 0.92
GetFunction · 0.92
WithLogRequestIDKeyFunction · 0.92
WithEnableTraceFunction · 0.92
AdaptivePostgresqlDsnFunction · 0.92
InitFunction · 0.92
SetDriverFunction · 0.92
ErrorMethod · 0.65

Tested by 1

TestInitPostgresqlErrorFunction · 0.68