Added devices and companies models

This commit is contained in:
Nedim
2023-09-06 11:58:33 +02:00
parent 8bbfa53d42
commit 40c6366608
10 changed files with 116 additions and 21 deletions

View File

@@ -2,7 +2,9 @@ package shared
import (
"fmt"
"log"
"novatech/config"
"novatech/models"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/postgres"
@@ -13,23 +15,28 @@ var db *gorm.DB
var err error
func Init() {
func Init() error{
host := config.AppConfig.Database.HostName
user := config.AppConfig.Database.UserName
port := config.AppConfig.Database.Port
// port := config.AppConfig.Database.Port
dbName := config.AppConfig.Database.DatabaseName
password := config.AppConfig.Database.Password
dbString:= fmt.Sprintf("postgres, host=%s:%s user=%s dbname=%s sslmode=disable password=%s",host,port,user,dbName,password)
//PostgreSQL
db, err = gorm.Open(dbString)
dbString:= fmt.Sprintf("host=%s user=%s dbname=%s sslmode=disable password=%s",host,user,dbName,password)
// db, err = gorm.Open("postgres", "host=localhost user=postgres dbname=postgres sslmode=disable password=root")
var err error
// //PostgreSQL
db, err = gorm.Open("postgres",dbString)
if err != nil {
fmt.Println(err)
log.Println("Error initializing the database: ", err)
return err
}
//TODO AUTOMIGRATE models once we have them
//db.AutoMigrate(&models.Person{})
db.AutoMigrate(&models.User{}, &models.Company{}, &models.Device{})
return nil
}