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