-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
gorm.go
54 lines (50 loc) · 960 Bytes
/
gorm.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
package database
import (
"database/sql"
gorm2 "github.com/bitmagnet-io/bitmagnet/internal/database/gorm"
"go.uber.org/fx"
"go.uber.org/zap"
"gorm.io/gorm"
gormlogger "gorm.io/gorm/logger"
"time"
)
type Params struct {
fx.In
Logger *zap.SugaredLogger
Lifecycle fx.Lifecycle
Dialector gorm.Dialector
}
type Result struct {
fx.Out
SqlDb *sql.DB
GormDb *gorm.DB
}
func New(p Params) (r Result, err error) {
loggerResult, loggerErr := gorm2.New(gorm2.Params{
ZapLogger: p.Logger,
Config: gorm2.Config{
LogLevel: gormlogger.Info,
SlowThreshold: time.Second * 3,
},
})
if loggerErr != nil {
err = loggerErr
return
}
gDb, dbErr := gorm.Open(p.Dialector, &gorm.Config{
Logger: loggerResult.GormLogger,
DisableAutomaticPing: true,
})
if dbErr != nil {
err = dbErr
return
}
sqlDb, sqlDbErr := gDb.DB()
if sqlDbErr != nil {
err = sqlDbErr
return
}
r.GormDb = gDb
r.SqlDb = sqlDb
return
}