/
db.go
49 lines (42 loc) · 1.19 KB
/
db.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
package db
import (
"errors"
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/postgres"
"github.com/qor/i18n"
"github.com/qor/i18n/backends/database"
"github.com/qor/l10n"
"github.com/qor/media_library"
"github.com/qor/publish"
"github.com/isairz/hive/config"
"github.com/qor/sorting"
"github.com/qor/validations"
)
var (
DB *gorm.DB
Publish *publish.Publish
)
func init() {
var err error
dbConfig := config.Config.DB
if config.Config.DB.Adapter == "mysql" {
DB, err = gorm.Open("mysql", fmt.Sprintf("%v:%v@/%v?parseTime=True&loc=Local", dbConfig.User, dbConfig.Password, dbConfig.Name))
} else if config.Config.DB.Adapter == "postgres" {
DB, err = gorm.Open("postgres", fmt.Sprintf("user=%v password=%v dbname=%v sslmode=disable", dbConfig.User, dbConfig.Password, dbConfig.Name))
} else {
panic(errors.New("not supported database adapter"))
}
if err == nil {
// DB.LogMode(true)
Publish = publish.New(DB)
config.Config.I18n = i18n.New(database.New(DB))
l10n.RegisterCallbacks(DB)
sorting.RegisterCallbacks(DB)
validations.RegisterCallbacks(DB)
media_library.RegisterCallbacks(DB)
} else {
panic(err)
}
}