generated from resonatecoop/id-server-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.go
49 lines (36 loc) · 924 Bytes
/
database.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
package database
import (
"github.com/jackc/pgx/v4"
stdlib "github.com/jackc/pgx/v4/stdlib"
"github.com/resonatecoop/id/config"
bun "github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/extra/bundebug"
// Drivers
_ "github.com/lib/pq"
//_ "github.com/uptrace/bun/dialects/mysql"
)
func init() {
// sql.NowFunc = func() time.Time {
// return time.Now().UTC()
// }
}
// NewDatabase returns a bun.DB struct
func NewDatabase(cnf *config.Config) (*bun.DB, error) {
// Postgres
dbconfig, err := pgx.ParseConfig(cnf.Database.PSN)
if err != nil {
return nil, err
}
dbconfig.PreferSimpleProtocol = true
sqldb := stdlib.OpenDB(*dbconfig)
db := bun.NewDB(sqldb, pgdialect.New())
if cnf.IsDevelopment {
db.AddQueryHook(bundebug.NewQueryHook(bundebug.WithVerbose(true)))
}
_, err = db.Exec("SELECT 1=1")
if err != nil {
return db, err
}
return db, err
}