-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgxpool.go
55 lines (45 loc) · 940 Bytes
/
pgxpool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package pg
import (
"context"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
)
const (
DefaultIP = "localhost"
DefaultPort = 5432
)
func ConnString(user, pass, db, ip string, port int) string {
if port == 0 {
port = DefaultPort
}
if ip == "" {
ip = DefaultIP
}
return fmt.Sprintf("postgres://%s:%s@%v:%v/%v?sslmode=disable",
user,
pass,
ip,
port,
db,
)
}
type OptionFunc func(config *pgxpool.Config)
func ParseConfig(connString string, options ...OptionFunc) (*pgxpool.Config, error) {
config, err := pgxpool.ParseConfig(connString)
if err != nil {
return nil, err
}
if len(options) != 0 {
for _, option := range options {
option(config)
}
}
return config, nil
}
func New(ctx context.Context, connString string, options ...OptionFunc) (*pgxpool.Pool, error) {
config, err := ParseConfig(connString, options...)
if err != nil {
return nil, err
}
return pgxpool.ConnectConfig(ctx, config)
}