Skip to content

Commit

Permalink
Next (#10)
Browse files Browse the repository at this point in the history
* feat(box): support server hooks

* feat(pkg/client/kafka): new alias; add default meta config

* feat(pkg/client/gopg): support new alias

* build(mod): upgrade pg,redis

* feat(pkg/client/gopg): support debug logger; replace default logger, add `Error` alias.

* feat(pkg/client/redis): support script and add tests

* feat(pkg/schedule): support handler args and add example

* docs(readme): update readme

* feat(pkg/client/rabbitmq): support rabbitmq client

* fix(pkg/client): fix type alias

* feat(pkg/schedule): support ExecOnce and ExecTiming method

* feat(pkg/server/ginserver): support ginlog

* feat(pkg/metric): alias vec

* feat(pkg/server/ginserver): support ginprom

* feat(pkg/server/ginserver): alias gin.Context

* feat(pkg/server/ginserver): support ginsession

* feat(pkg/server/ginserver): ginlog support skip urls

* feat(pkg/server/ginserver): ginlog support trace reqid and bizid

* fix(pkg/client/wukong): fix formdata missing error

* feat(pkg/client/wukong): support empty base url client

* feat(pkg/cache): support Prefix option, export Default instance and export cache missing error

BREAKING CHANGE: Now cache missing is an ErrCacheMiss error, not nil.

* feat(pkg/locker): redislocker support prefix

* feat(pkg/client/mongodb): more alias, eg: ObjectID,Null,Undefined etc

* feat(pkg/server/ginserver): ginlog request body convert to string

* feat(pkg/client/mongodb): add options alias

* feat(pkg/client/wukong): support logger and metric switch

* feat(pkg/client/kafka): alias Encoder

* feat(pkg/client/wukong): support more logger level

* feat(pkg/client/kafka): support more client methods and alias

* fix(pkg/client/mongodb): fix multi instance metric conflict.

BREAKING CHANGE: change Monitor to interface

fix #4

* feat(pkg/client/gopg): new alias types

* feat(pkg/client/mongodb): alias OptionXXX to options.XXX for quick new options

* refactor(pkg/config): watcher of mognodb and redis reuse Read function

* feat(pkg/client/gopg): support config fields. more alias

* feat(pkg/client/gorm): implement basic function

* style(pkg/config): format comments

* refactor(pkg/client/gorm): rename to pkg/client/gormx

* feat(pkg/server/grpcserver): add `WithWrap` option, remove `RawServer` function

BREAKING CHANGE: remove `RawServer` function

* feat(pkg/server/grpcgateway): init

* feat(pkg/insight): show insight by html, `/config` support format parameter: json,table

* feat(pkg/metric): fix config load error

* refactor(pkg/metric): rename job name to service name and service version

* feat(box): support box info metric

* feat(pkg/server/ginserver): more metric

* feat(metric): remove 0.25 summary quantile

* feat(box): support tags, config and metric

* feat(pkg/server/ginserver): replace 404,405 response text; fix prom response size error;

* feat(pkg/util/strutil): add String2Bytes and Bytes2String function

* feat(pkg/pubsub): support lightweight pubsub

* feat(pkg/logger): add mask core; remove dpanic level and some unsed log func; add watch interval

* build(mod): support go version 1.15+

* ci(lint): update lint timeout 2m

* Create codeql-analysis.yml

* feat(pkg/codec): support json,proto,toml,xml and yaml

* refactor(pkg/config): change encoder to codec and delete encoder

* feat(gopg): upgrade go-pg from v10.7.7 to v10.10.6 and add more error log

* docs(rediscache): update commenets

* build(mod): tidy

* fix(pkg/client/wukong): ignore nil query field

* feat(pkg/client/gormx): 增加gorm.Config配置支持
  • Loading branch information
amazing-gao authored Dec 10, 2021
1 parent 1429284 commit 6027351
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 10 deletions.
72 changes: 64 additions & 8 deletions pkg/client/gormx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,39 @@ import (
"github.com/boxgo/box/pkg/config"
"github.com/boxgo/box/pkg/logger"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
)

type (
// Config 配置
Config struct {
path string
dial gorm.Dialector
Driver string `config:"driver" desc:"SQLDrivers: https://github.com/golang/go/wiki/SQLDrivers"`
DSN string `config:"dsn" desc:"Data Source Name"`
MaxIdleTime time.Duration `config:"maxIdleTime" desc:"SetConnMaxIdleTime sets the maximum amount of time a connection may be idle. If d <= 0, connections are not closed due to a connection's idle time."`
MaxLifeTime time.Duration `config:"maxLifeTime" desc:"SetConnMaxLifetime sets the maximum amount of time a connection may be reused. If d <= 0, connections are not closed due to a connection's age."`
MaxIdleConns int `config:"maxIdleConns" desc:"MaxIdleConns sets the maximum number of connections in the idle connection pool. If MaxOpenConns is greater than 0 but less than the new MaxIdleConns, then the new MaxIdleConns will be reduced to match the MaxOpenConns limit. If n <= 0, no idle connections are retained."`
MaxOpenConns int `config:"maxOpenConns" desc:"MaxOpenConns sets the maximum number of open connections to the database. If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit. If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited)."`
path string
dial gorm.Dialector
Driver string `config:"driver" desc:"SQLDrivers: https://github.com/golang/go/wiki/SQLDrivers"`
DSN string `config:"dsn" desc:"Data Source Name"`
MaxIdleTime time.Duration `config:"maxIdleTime" desc:"SetConnMaxIdleTime sets the maximum amount of time a connection may be idle. If d <= 0, connections are not closed due to a connection's idle time."`
MaxLifeTime time.Duration `config:"maxLifeTime" desc:"SetConnMaxLifetime sets the maximum amount of time a connection may be reused. If d <= 0, connections are not closed due to a connection's age."`
MaxIdleConns int `config:"maxIdleConns" desc:"MaxIdleConns sets the maximum number of connections in the idle connection pool. If MaxOpenConns is greater than 0 but less than the new MaxIdleConns, then the new MaxIdleConns will be reduced to match the MaxOpenConns limit. If n <= 0, no idle connections are retained."`
MaxOpenConns int `config:"maxOpenConns" desc:"MaxOpenConns sets the maximum number of open connections to the database. If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit. If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited)."`
SkipDefaultTransaction bool `config:"skipDefaultTransaction"`
DryRun bool `config:"dryRun"`
PrepareStmt bool `config:"prepareStmt"`
DisableNestedTransaction bool `config:"disableNestedTransaction"`
AllowGlobalUpdate bool `config:"allowGlobalUpdate"`
DisableAutomaticPing bool `config:"disableAutomaticPing"`
DisableForeignKeyConstraintWhenMigrating bool `config:"disableForeignKeyConstraintWhenMigrating"`
QueryFields bool `config:"queryFields"`
CreateBatchSize int `config:"createBatchSize"`
NamingStrategy schema.Namer `config:"-"`
NamingStrategyTablePrefix string `config:"namingStrategyTablePrefix"`
NamingStrategySingularTable bool `config:"namingStrategySingularTable"`
NamingStrategyNoLowerCase bool `config:"namingStrategyNoLowerCase"`
NamingStrategyNameReplacer schema.Replacer `config:"-"`
NowFunc func() time.Time `config:"-"`
ConnPool gorm.ConnPool `config:"-"`
ClauseBuilders map[string]clause.ClauseBuilder `config:"-"`
Plugins map[string]gorm.Plugin `config:"-"`
}

// OptionFunc 选项信息
Expand All @@ -31,6 +51,42 @@ func WithDialector(dial gorm.Dialector) OptionFunc {
}
}

func WithNamingStrategy(namer schema.Namer) OptionFunc {
return func(c *Config) {
c.NamingStrategy = namer
}
}

func WithNamingStrategyNameReplacer(replacer schema.Replacer) OptionFunc {
return func(c *Config) {
c.NamingStrategyNameReplacer = replacer
}
}

func WithNowFunc(fn func() time.Time) OptionFunc {
return func(c *Config) {
c.NowFunc = fn
}
}

func WithConnPool(pool gorm.ConnPool) OptionFunc {
return func(c *Config) {
c.ConnPool = pool
}
}

func WithClauseBuilders(builders map[string]clause.ClauseBuilder) OptionFunc {
return func(c *Config) {
c.ClauseBuilders = builders
}
}

func WithPlugins(plugins map[string]gorm.Plugin) OptionFunc {
return func(c *Config) {
c.Plugins = plugins
}
}

// StdConfig 标准配置
func StdConfig(key string, optionFunc ...OptionFunc) *Config {
cfg := DefaultConfig(key)
Expand Down
29 changes: 27 additions & 2 deletions pkg/client/gormx/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

type (
Expand All @@ -34,9 +35,33 @@ func newGorm(c *Config) *Gorm {
}
}

var namer schema.Namer
if c.NamingStrategy != nil {
namer = c.NamingStrategy
} else {
namer = schema.NamingStrategy{
TablePrefix: c.NamingStrategyTablePrefix,
SingularTable: c.NamingStrategySingularTable,
NameReplacer: c.NamingStrategyNameReplacer,
NoLowerCase: c.NamingStrategyNoLowerCase,
}
}

db, err := gorm.Open(dial, &gorm.Config{
SkipDefaultTransaction: true,
Logger: &Logger{},
SkipDefaultTransaction: c.SkipDefaultTransaction,
DryRun: c.DryRun,
PrepareStmt: c.PrepareStmt,
DisableNestedTransaction: c.DisableNestedTransaction,
AllowGlobalUpdate: c.AllowGlobalUpdate,
DisableForeignKeyConstraintWhenMigrating: c.DisableForeignKeyConstraintWhenMigrating,
QueryFields: c.QueryFields,
CreateBatchSize: c.CreateBatchSize,
NamingStrategy: namer,
NowFunc: c.NowFunc,
ConnPool: c.ConnPool,
ClauseBuilders: c.ClauseBuilders,
Plugins: c.Plugins,
Logger: &Logger{},
})
if err != nil {
logger.Panicf("Gorm open error %s %s: %s", c.Driver, c.DSN, err)
Expand Down

0 comments on commit 6027351

Please sign in to comment.