-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect.go
More file actions
37 lines (34 loc) · 794 Bytes
/
connect.go
File metadata and controls
37 lines (34 loc) · 794 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
package sqlorm
import (
"fmt"
"time"
"github.com/tinh-tinh/tinhtinh/v2/common/color"
"gorm.io/gorm"
)
func NewConnect(config Config) *gorm.DB {
conn, err := gorm.Open(config.Dialect, config.Options...)
if err != nil {
if config.Retry != nil && config.Retry.MaxRetries > 0 {
fmt.Printf("%s %s %s %s\n",
color.Green("[SQLORM]"),
color.White("Failed to connect to database:"),
color.Red(err.Error()),
color.Yellow(fmt.Sprintf("Retrying attempt remain %d", config.Retry.MaxRetries)),
)
time.Sleep(config.Retry.Delay)
config.Retry.MaxRetries--
return NewConnect(config)
}
panic(err)
}
if config.OnInit != nil {
config.OnInit(conn)
}
if config.Sync {
err = conn.AutoMigrate(config.Models...)
if err != nil {
panic(err)
}
}
return conn
}