45 lines
699 B
Go
45 lines
699 B
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
config "learnsteam/cslms-api/configs"
|
|
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/schema"
|
|
)
|
|
|
|
type Database struct {
|
|
*gorm.DB
|
|
}
|
|
|
|
var DB *gorm.DB
|
|
|
|
func Init() {
|
|
DB = ConnectDB((config.DATABASE_URL))
|
|
}
|
|
|
|
func ConnectDB(url string) *gorm.DB {
|
|
gorm_config := gorm.Config{NamingStrategy: schema.NamingStrategy{SingularTable: true}}
|
|
db, err := gorm.Open(mysql.Open(url), &gorm_config)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
func GetDB() *gorm.DB {
|
|
return DB
|
|
}
|
|
|
|
func AutoMigrate() {
|
|
fmt.Println("AugoMigrate")
|
|
// DB.AutoMigrate(
|
|
// &models.User{},
|
|
// &models.Token{},
|
|
// &models.Program{},
|
|
// &models.Quiz{},
|
|
// &models.Exam{},
|
|
// )
|
|
}
|