/
orm.go
36 lines (31 loc) · 914 Bytes
/
orm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package data
import (
"fmt"
"log"
"github.com/doctori/opencycledatabase/internal/pkg/config"
"github.com/doctori/opencycledatabase/internal/pkg/data/standards"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// InitDB will initialise the database connection and the scheme
func InitDB(config config.Config) *gorm.DB {
connectionString := fmt.Sprintf(
"user=%s password='%s' host=%s dbname=%s",
config.DB.Username,
config.DB.Password,
config.DB.Host,
config.DB.DBname)
log.Printf("Connecting to %s", connectionString)
db, err := gorm.Open(postgres.Open(connectionString), &gorm.Config{})
checkErr(err, "Postgres Opening Failed")
// Debug Mode
db.Debug()
db.AutoMigrate(&Image{}, &ComponentType{}, &Brand{}, &standards.BBStandard{}, &Component{}, &Bike{})
checkErr(err, "Create tables failed")
return db
}
func checkErr(err error, msg string) {
if err != nil {
log.Panicln(msg, err)
}
}