-
Notifications
You must be signed in to change notification settings - Fork 14
/
orm_engine.go
57 lines (44 loc) · 1.34 KB
/
orm_engine.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
package registry
import (
"github.com/gin-gonic/gin"
"github.com/latolukasz/beeorm"
"github.com/sarulabs/di"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/config"
)
func ServiceProviderOrmEngine() *service.DefinitionGlobal {
return &service.DefinitionGlobal{
Name: "orm_engine_global",
Build: func(ctn di.Container) (interface{}, error) {
ormConfigService, err := ctn.SafeGet(service.ORMConfigService)
if err != nil {
return nil, err
}
ormEngine := ormConfigService.(beeorm.ValidatedRegistry).CreateEngine()
configService := ctn.Get(service.ConfigService).(config.IConfig)
ormDebug, ok := configService.Bool("orm_debug")
if ok && ormDebug {
ormEngine.EnableQueryDebug()
}
return ormEngine, nil
},
}
}
func ServiceProviderOrmEngineForContext(enableGraphQLDataLoader bool) *service.DefinitionRequest {
return &service.DefinitionRequest{
Name: "orm_engine_request",
Build: func(c *gin.Context) (interface{}, error) {
ormConfigService := service.DI().OrmConfig()
ormEngine := ormConfigService.CreateEngine()
if enableGraphQLDataLoader {
ormEngine.EnableRequestCache()
}
configService := service.DI().Config()
ormDebug, ok := configService.Bool("orm_debug")
if ok && ormDebug {
ormEngine.EnableQueryDebug()
}
return ormEngine, nil
},
}
}