diff --git a/cmd/init.go b/cmd/init.go index a14d525d..6a604e53 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -16,6 +16,7 @@ var dbDsn string var dbOrm string var dbSoft string var dbGrpc bool +var dbService bool var dbTablePrefix string var dbTableGlob string var module bool @@ -43,6 +44,7 @@ var initCmd = &cobra.Command{ Orm: dbOrm, Soft: dbSoft, Grpc: dbGrpc, + Service: dbService, } if stringutils.IsNotEmpty(dbConf.Driver) && stringutils.IsNotEmpty(dbConf.Dsn) { options = append(options, svc.WithDbConfig(&dbConf)) @@ -69,6 +71,7 @@ func init() { initCmd.Flags().StringVar(&dbDsn, "db_dsn", "", `Specify database connection url`) initCmd.Flags().StringVar(&dbSoft, "db_soft", "deleted_at", `Specify database soft delete column name`) initCmd.Flags().BoolVar(&dbGrpc, "db_grpc", false, `If true, grpc code will also be generated`) + initCmd.Flags().BoolVar(&dbService, "db_service", true, `If false, service will not be generated, and db_grpc will be ignored. Only dao layer code will be generated.`) initCmd.Flags().StringVar(&dbTablePrefix, "db_table_prefix", "", `table prefix or schema name for pg`) initCmd.Flags().StringVar(&dbTableGlob, "db_table_glob", "", `used to filter glob-matched tables`) initCmd.Flags().StringVar(&naming, "case", "lowerCamel", `protobuf message field and json tag case, only support "lowerCamel" and "snake"`) diff --git a/cmd/internal/svc/codegen/database/common.go b/cmd/internal/svc/codegen/database/common.go index d8a28030..33892336 100644 --- a/cmd/internal/svc/codegen/database/common.go +++ b/cmd/internal/svc/codegen/database/common.go @@ -52,6 +52,7 @@ type IOrmGenerator interface { fix() Initialize(conf OrmGeneratorConfig) GenService() + GenDao() } var _ IOrmGenerator = (*AbstractBaseGenerator)(nil) @@ -74,6 +75,12 @@ type AbstractBaseGenerator struct { ConfigPackage string } +func (b *AbstractBaseGenerator) GenDao() { + b.dto() + b.orm() + b.goModTidy() +} + func (b *AbstractBaseGenerator) fix() { b.impl.fix() } diff --git a/cmd/internal/svc/svc.go b/cmd/internal/svc/svc.go index 3b9cfd85..b547d2f3 100644 --- a/cmd/internal/svc/svc.go +++ b/cmd/internal/svc/svc.go @@ -106,6 +106,7 @@ type DbConfig struct { Orm string Soft string Grpc bool + Service bool } func (receiver *Svc) SetWatcher(w *watcher.Watcher) { @@ -192,7 +193,11 @@ func (receiver *Svc) Init() { Soft: receiver.DbConfig.Soft, Grpc: receiver.DbConfig.Grpc, }) - gen.GenService() + if receiver.DbConfig.Service { + gen.GenService() + } else { + gen.GenDao() + } } else if !receiver.module { if stringutils.IsEmpty(receiver.DocPath) { matches, _ := filepath.Glob(filepath.Join(receiver.dir, "*_openapi3.json"))