forked from ebhomengo/niki
1
0
Fork 0
niki/adapter/storage/setup.go

34 lines
609 B
Go
Raw Normal View History

2023-12-16 06:09:50 +00:00
package storage
import (
storage "git.gocasts.ir/ebhomengo/niki/adapter/storage/models"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type DBMS struct {
db *gorm.DB
}
func GormConnect(dsn string) (DBMS, error) {
gorm, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Info),
})
if err != nil {
return DBMS{}, err
}
return DBMS{db: gorm}, nil
}
func GormAutoMigrate(db gorm.DB) error {
// Migrate the schema
err := db.AutoMigrate(
storage.PiggyBank{},
)
if err != nil {
panic(any("Storage auto migrate has error"))
}
return err
}