-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
27 lines (22 loc) · 647 Bytes
/
db.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
package config
import (
"fmt"
"log"
"tokobelanja-kelompok7/model/entity"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func InitDB(username, password, host, port, dbName string) *gorm.DB {
dsnString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", username, password, host, port, dbName)
db, err := gorm.Open(mysql.Open(dsnString), &gorm.Config{})
if err != nil {
fmt.Println(dsnString)
panic(err.Error())
}
if err != nil {
log.Fatal("DB Konek Eror")
}
fmt.Println("DB Berhasil Konek")
db.AutoMigrate(&entity.User{}, &entity.Category{}, &entity.Product{}, &entity.TransactionHistory{})
return db
}