Skip to content

Commit

Permalink
add db(gormx) config fn
Browse files Browse the repository at this point in the history
  • Loading branch information
yinheli committed Aug 10, 2021
1 parent b5efbc7 commit 5919289
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions core/xservice/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/xinpianchang/xservice/core"
"github.com/xinpianchang/xservice/pkg/config"
"github.com/xinpianchang/xservice/pkg/gormx"
"github.com/xinpianchang/xservice/pkg/netx"
)

Expand All @@ -26,6 +27,7 @@ type Options struct {
Build string
Description string
Config *viper.Viper
DbConfigureFn gormx.ConfigureFn
GrpcServerOptions []grpc.ServerOption
GrpcClientDialOptions []grpc.DialOption
GrpcClientDialTimeout time.Duration
Expand Down Expand Up @@ -74,6 +76,13 @@ func Config(config *viper.Viper) Option {
}
}

// WithDbConfigureFn set db configure function
func WithDbConfigureFn(fn gormx.ConfigureFn) Option {
return func(o *Options) {
o.DbConfigureFn = fn
}
}

// WithGrpcServerOptions add additional grpc server options
func WithGrpcServerOptions(options ...grpc.ServerOption) Option {
return func(o *Options) {
Expand Down
2 changes: 1 addition & 1 deletion core/xservice/xservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (t *serviceImpl) init() {
}

if t.options.Config.IsSet("database") {
gormx.Config(t.options.Config)
gormx.Config(t.options.Config, t.options.DbConfigureFn)
}

if t.options.Config.IsSet("mq") {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gormx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Config(v *viper.Viper, configureFn ...ConfigureFn) {
}

var db *gorm.DB
if len(configureFn) > 0 {
if len(configureFn) > 0 && configureFn[0] != nil {
db = configureFn[0](c)
} else {
db = MySQLDbConfig(c)
Expand Down

0 comments on commit 5919289

Please sign in to comment.