28 lines
525 B
Go
28 lines
525 B
Go
package gorm
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"src.dualinventive.com/go/authentication-service/internal/domain"
|
|
|
|
"github.com/jinzhu/gorm"
|
|
)
|
|
|
|
func openDB(host, port, name, user, pass string) (*gorm.DB, error) {
|
|
dbConfig := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8&parseTime=True&loc=Local",
|
|
user,
|
|
pass,
|
|
host,
|
|
port,
|
|
name,
|
|
)
|
|
|
|
db, err := gorm.Open("mysql", dbConfig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to connect to database: %v", err)
|
|
}
|
|
|
|
db.Model(domain.User{}).Related(domain.Company{})
|
|
return db, nil
|
|
}
|