-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
72 lines (60 loc) · 1.46 KB
/
repo.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package server
import (
"fmt"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
// Config property names
const (
ConfigDBURI = "db_uri"
ConfigDBMaxConn = "db_max_connections"
)
// Repository provides access to the DB and prepared statements
type Repository struct {
DB *gorm.DB
dbConfig DBconfig
}
// DBconfig holds all relevant db configurations
type DBconfig struct {
DbURI string
DbMaxDBConnections int
}
// CreateRepository intizializes a repository
func CreateRepository(c *Config) *Repository {
dbConf := readDBConfig(c)
repo := Repository{
dbConfig: dbConf,
}
err := repo.init()
if err != nil {
panic(err)
}
return &repo
}
func readDBConfig(c *Config) DBconfig {
dbConfig := DBconfig{
DbURI: c.Get(ConfigDBURI),
DbMaxDBConnections: c.GetInt(ConfigDBMaxConn),
}
return dbConfig
}
// Init initializes the repository incl. DB connection
func (r *Repository) init() error {
gdb, err := gorm.Open(mysql.Open(r.dbConfig.DbURI), &gorm.Config{})
if err != nil {
return fmt.Errorf("failed to open DB connection: %w", err)
}
r.DB = gdb
db, err := gdb.DB()
if err != nil {
return fmt.Errorf("failed to retrieve underlying sqldb object: %w", err)
}
db.SetMaxOpenConns(r.dbConfig.DbMaxDBConnections)
return nil
}
// RunMigrations migrates all provided entities which must be gorm compatible entities
func (r *Repository) RunMigrations(entities []interface{}) {
for _, e := range entities {
r.DB.AutoMigrate(e)
}
}