learnsteam-quiz-api/internal/database/database.go

46 lines
736 B
Go
Raw Normal View History

2023-10-19 22:47:12 +09:00
package database
import (
2023-11-17 01:38:01 +09:00
"fmt"
config "learnsteam/learsteam-quiz-api/configs"
"learnsteam/learsteam-quiz-api/internal/models"
2023-10-19 22:47:12 +09:00
"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() {
2023-11-17 01:38:01 +09:00
fmt.Println("AugoMigrate")
2023-10-19 22:47:12 +09:00
DB.AutoMigrate(
&models.User{},
&models.Token{},
2023-11-17 01:38:01 +09:00
&models.Program{},
&models.Quiz{},
2023-11-23 00:30:50 +09:00
&models.Exam{},
2023-10-19 22:47:12 +09:00
)
}