first commit
This commit is contained in:
40
internal/database/database.go
Normal file
40
internal/database/database.go
Normal file
@ -0,0 +1,40 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
config "studioj/boilerplate_go/configs"
|
||||
"studioj/boilerplate_go/internal/models"
|
||||
|
||||
"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() {
|
||||
DB.AutoMigrate(
|
||||
&models.User{},
|
||||
&models.Token{},
|
||||
)
|
||||
}
|
Reference in New Issue
Block a user