-
Notifications
You must be signed in to change notification settings - Fork 14
/
orm_config.go
49 lines (38 loc) · 1.1 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
package registry
import (
"errors"
"fmt"
"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)
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()
return ormConfig, err
},
Close: func(obj interface{}) error {
defferFunc()
return nil
},
}
}