-
Notifications
You must be signed in to change notification settings - Fork 13
/
orm_config.go
52 lines (40 loc) · 1.23 KB
/
orm_config.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
package registry
import (
"errors"
"fmt"
"github.com/coretrix/hitrix/service/component/app"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/config"
"github.com/sarulabs/di"
"github.com/latolukasz/beeorm"
)
type ORMRegistryInitFunc func(registry *beeorm.Registry)
func ServiceDefinitionOrmRegistry(init ORMRegistryInitFunc) *service.DefinitionGlobal {
var defferFunc func()
var ormConfig beeorm.ValidatedRegistry
var err error
return &service.DefinitionGlobal{
Name: service.ORMConfigService,
Build: func(ctn di.Container) (interface{}, error) {
configService := ctn.Get(service.ConfigService).(config.IConfig)
appService := ctn.Get(service.AppService).(*app.App)
registry := beeorm.NewRegistry()
configuration, ok := configService.Get("orm")
if !ok {
return nil, errors.New("no orm config")
}
yamlConfig := map[string]interface{}{}
for k, v := range configuration.(map[interface{}]interface{}) {
yamlConfig[fmt.Sprint(k)] = v
}
registry.InitByYaml(yamlConfig)
init(registry)
ormConfig, defferFunc, err = registry.Validate(appService.GlobalContext)
return ormConfig, err
},
Close: func(obj interface{}) error {
defferFunc()
return nil
},
}
}