mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
feat(goctl): generate mongo model with cache prefix (#4907)
This commit is contained in:
@@ -206,6 +206,7 @@
|
||||
"short": "Generate mongo model",
|
||||
"type": "Specified model type name",
|
||||
"cache": "Generate code with cache [optional]",
|
||||
"prefix": "Generate code with cache prefix [optional]",
|
||||
"easy": "Generate code with auto generated CollectionName for easy declare [optional]",
|
||||
"dir": "{{.goctl.model.dir}}",
|
||||
"style": "{{.global.style}}",
|
||||
|
||||
@@ -62,6 +62,7 @@ func init() {
|
||||
|
||||
mongoCmdFlags.StringSliceVarP(&mongo.VarStringSliceType, "type", "t")
|
||||
mongoCmdFlags.BoolVarP(&mongo.VarBoolCache, "cache", "c")
|
||||
mongoCmdFlags.StringVarP(&mongo.VarStringPrefix, "prefix", "p")
|
||||
mongoCmdFlags.BoolVarP(&mongo.VarBoolEasy, "easy", "e")
|
||||
mongoCmdFlags.StringVarP(&mongo.VarStringDir, "dir", "d")
|
||||
mongoCmdFlags.StringVar(&mongo.VarStringStyle, "style")
|
||||
|
||||
@@ -17,6 +17,7 @@ import (
|
||||
type Context struct {
|
||||
Types []string
|
||||
Cache bool
|
||||
Prefix string
|
||||
Easy bool
|
||||
Output string
|
||||
Cfg *config.Config
|
||||
@@ -60,6 +61,7 @@ func generateModel(ctx *Context) error {
|
||||
"Type": stringx.From(t).Title(),
|
||||
"lowerType": stringx.From(t).Untitle(),
|
||||
"Cache": ctx.Cache,
|
||||
"Prefix": ctx.Prefix,
|
||||
"version": version.BuildVersion,
|
||||
}, output, true); err != nil {
|
||||
return err
|
||||
|
||||
@@ -19,6 +19,8 @@ var (
|
||||
VarStringDir string
|
||||
// VarBoolCache describes whether cache is enabled.
|
||||
VarBoolCache bool
|
||||
// VarStringPrefix string describes the prefix for the cache key.
|
||||
VarStringPrefix string
|
||||
// VarBoolEasy describes whether to generate Collection Name in the code for easy declare.
|
||||
VarBoolEasy bool
|
||||
// VarStringStyle describes the style.
|
||||
@@ -35,6 +37,7 @@ var (
|
||||
func Action(_ *cobra.Command, _ []string) error {
|
||||
tp := VarStringSliceType
|
||||
c := VarBoolCache
|
||||
p := VarStringPrefix
|
||||
easy := VarBoolEasy
|
||||
o := strings.TrimSpace(VarStringDir)
|
||||
s := VarStringStyle
|
||||
@@ -74,6 +77,7 @@ func Action(_ *cobra.Command, _ []string) error {
|
||||
return generate.Do(&generate.Context{
|
||||
Types: tp,
|
||||
Cache: c,
|
||||
Prefix: p,
|
||||
Easy: easy,
|
||||
Output: a,
|
||||
Cfg: cfg,
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
)
|
||||
|
||||
{{if .Cache}}var prefix{{.Type}}CacheKey = "cache:{{.lowerType}}:"{{end}}
|
||||
{{if .Cache}}var prefix{{.Type}}CacheKey = "{{if .Prefix}}{{.Prefix}}:{{end}}cache:{{.lowerType}}:"{{end}}
|
||||
|
||||
type {{.lowerType}}Model interface{
|
||||
Insert(ctx context.Context,data *{{.Type}}) error
|
||||
|
||||
Reference in New Issue
Block a user