Last active
February 29, 2020 12:56
-
-
Save victorsteven/ca4df1180d36f52759f98c16754aecf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package persistence | |
import ( | |
"fmt" | |
"food-app/domain/entity" | |
"food-app/domain/repository" | |
"github.com/jinzhu/gorm" | |
_ "github.com/jinzhu/gorm/dialects/postgres" | |
) | |
type Repositories struct { | |
User repository.UserRepository | |
Food repository.FoodRepository | |
db *gorm.DB | |
} | |
func NewRepositories(Dbdriver, DbUser, DbPassword, DbPort, DbHost, DbName string) (*Repositories, error) { | |
DBURL := fmt.Sprintf("host=%s port=%s user=%s dbname=%s sslmode=disable password=%s", DbHost, DbPort, DbUser, DbName, DbPassword) | |
db, err := gorm.Open(Dbdriver, DBURL) | |
if err != nil { | |
return nil, err | |
} | |
db.LogMode(true) | |
return &Repositories{ | |
User: NewUserRepository(db), | |
Food: NewFoodRepository(db), | |
db: db, | |
}, nil | |
} | |
//closes the database connection | |
func (s *Repositories) Close() error { | |
return s.db.Close() | |
} | |
//This migrate all tables | |
func (s *Repositories) Automigrate() error { | |
return s.db.AutoMigrate(&entity.User{}, &entity.Food{}).Error | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment