first commit
This commit is contained in:
		
							
								
								
									
										44
									
								
								internal/database/database.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								internal/database/database.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
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{},
 | 
			
		||||
	// )
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user