-
Notifications
You must be signed in to change notification settings - Fork 0
/
connector.go
52 lines (44 loc) · 822 Bytes
/
connector.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
package kvorm
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
)
type DbConfig struct {
Host string
Port int
User string
Password string
Name string
PollCount int32
}
type DbClient struct {
Ctx context.Context
Pool *pgxpool.Pool
}
func (cli *DbClient) Connect(ctx context.Context, cfg DbConfig) error {
connectionStr := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
cfg.Host,
cfg.Port,
cfg.User,
cfg.Password,
cfg.Name,
)
pgxConfig, err := pgxpool.ParseConfig(connectionStr)
if err != nil {
return err
}
pgxConfig.MaxConns = cfg.PollCount
db, err := pgxpool.NewWithConfig(ctx, pgxConfig)
if err != nil {
return err
}
err = db.Ping(ctx)
if err != nil {
return err
}
cli.Pool = db
cli.Ctx = ctx
return nil
}