-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbhelper.go
More file actions
44 lines (36 loc) · 716 Bytes
/
dbhelper.go
File metadata and controls
44 lines (36 loc) · 716 Bytes
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
package dbhelper
import (
"context"
_ "embed"
"os"
"bufo.zone/dbufo"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
type DbHelpers struct {
Pool *pgxpool.Pool
Queries *dbufo.Queries
}
func GetDb(ctx context.Context) *DbHelpers {
dbUrl := os.Getenv("DB_URL")
db, err := pgxpool.New(ctx, dbUrl)
if err != nil {
panic(err)
}
return &DbHelpers{
Pool: db,
Queries: dbufo.New(db),
}
}
func (helpers *DbHelpers) Close() {
helpers.Pool.Close()
}
func GetConn(ctx context.Context) *pgx.Conn {
// dbPath := os.Getenv("DB_PATH")
dbUrl := "postgres://postgres:password@localhost:5432/bufozone"
db, err := pgx.Connect(ctx, dbUrl)
if err != nil {
panic(err)
}
return db
}