-
Notifications
You must be signed in to change notification settings - Fork 57
/
mysql.go
46 lines (37 loc) · 1.21 KB
/
mysql.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
package database
import (
// just init
"log"
"os"
"strings"
"time"
"gorm.io/driver/mysql"
// "gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
)
// InitMysqlDb init mysql database
func InitDb(driver string, dsn string) (*gorm.DB, error) {
// dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
defaultLogger := logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
SlowThreshold: 200 * time.Millisecond,
LogLevel: logger.Warn,
Colorful: true,
})
var dialector gorm.Dialector
if driver == "mysql" {
dialector = mysql.Open(dsn)
}
// else if driver == "sqlite" {
// dialector = sqlite.Open(dsn)
// }
db, err := gorm.Open(dialector, &gorm.Config{
Logger: defaultLogger,
NamingStrategy: schema.NamingStrategy{
TablePrefix: "t_", // table name prefix, table for `User` would be `t_users`
SingularTable: true, // use singular table name, table for `User` would be `user` with this option enabled
NameReplacer: strings.NewReplacer("CID", "Cid"), // use name replacer to change struct/field name before convert it to db name
}})
return db, err
}