DB Table To Protobuf - 图1tip

This feature is available starting from version v2.4.

Introduction

This command is used to read the configured database and generate the corresponding proto data structure files based on the data tables.

Command Usage

  1. $ gf gen pbentity -h
  2. USAGE
  3. gf gen pbentity [OPTION]
  4. OPTION
  5. -p, --path directory path for generated files storing
  6. -k, --package package path for all entity proto files
  7. -l, --link database configuration, the same as the ORM configuration of GoFrame
  8. -t, --tables generate models only for given tables, multiple table names separated with ','
  9. -f, --prefix add specified prefix for all entity names and entity proto files
  10. -r, --removePrefix remove specified prefix of the table, multiple prefix separated with ','
  11. -rf, --removeFieldPrefix remove specified prefix of the field, multiple prefix separated with ','
  12. -n, --nameCase case for message attribute names, default is "Camel":
  13. | Case | Example |
  14. |---------------- |--------------------|
  15. | Camel | AnyKindOfString |
  16. | CamelLower | anyKindOfString | default
  17. | Snake | any_kind_of_string |
  18. | SnakeScreaming | ANY_KIND_OF_STRING |
  19. | SnakeFirstUpper | rgb_code_md5 |
  20. | Kebab | any-kind-of-string |
  21. | KebabScreaming | ANY-KIND-OF-STRING |
  22. -j, --jsonCase case for message json tag, cases are the same as "nameCase", default "CamelLower".
  23. set it to "none" to ignore json tag generating.
  24. -o, --option extra protobuf options
  25. -h, --help more information about this command
  26. EXAMPLE
  27. gf gen pbentity
  28. gf gen pbentity -l "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  29. gf gen pbentity -p ./protocol/demos/entity -t user,user_detail,user_login
  30. gf gen pbentity -r user_ -k github.com/gogf/gf/example/protobuf
  31. gf gen pbentity -r user_
  32. CONFIGURATION SUPPORT
  33. Options are also supported by configuration file.
  34. It's suggested using configuration file instead of command line arguments making producing.
  35. The configuration node name is "gf.gen.pbentity", which also supports multiple databases, for example(config.yaml):
  36. gfcli:
  37. gen:
  38. - pbentity:
  39. link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
  40. path: "protocol/demos/entity"
  41. tables: "order,products"
  42. package: "demos"
  43. - pbentity:
  44. link: "mysql:root:12345678@tcp(127.0.0.1:3306)/primary"
  45. path: "protocol/demos/entity"
  46. prefix: "primary_"
  47. tables: "user, userDetail"
  48. package: "demos"
  49. option: |
  50. option go_package = "protobuf/demos";
  51. option java_package = "protobuf/demos";
  52. option php_namespace = "protobuf/demos";

DB Table To Protobuf - 图2tip

If you are using the framework’s recommended project scaffolding and have make installed, you can also use the make pbentity shortcut command.

Parameter Description:

NameDefault ValueMeaningExample
gfcli.gen.pbentityCode generation configuration items, which can contain multiple configuration items as an array, supporting multiple databases. Different databases can set different generation rules, such as generating to different locations or files.-
pathmanifest/protobuf/pbentityThe directory address for storing the generated proto files.protobuf/pbentity
packageAutomatically recognize go.modThe gopackage path in the generated proto file, and automatically recognizes the package name-
linkConsists of two parts, the first indicating the type of database you are connecting to such as mysql, postgresql, etc., and the second being the dsn information for connecting to the database. Refer to the chapter ORM - Configuration for details.-
prefixThe prefix for generating database objects and files, to distinguish between different databases or identical table names in different databases, preventing table name conflicts.order
user
removePrefixRemoves the specified prefix from the table names. Multiple prefixes are separated by commas.gf
removeFieldPrefixRemoves the specified prefix from field names. Multiple prefixes are separated by commas.f_
tablesDesignates tables in the current database for code generation. If empty, all tables in the database will be generated.user, user_detail
nameCaseCamelLowerThe name format for generated message attribute fields. Optional parameters are: Camel, CamelLower, Snake, SnakeScreaming, SnakeFirstUpper, Kebab, KebabScreaming. Refer to the command line help example for details.Snake
optionAdditional proto option configuration list.

Differences from gen dao‘s entity

Similarities

  • Both generate entity content, i.e., creating corresponding Golang entity objects from data collections (database tables) for easier program use. Both are one-way generation, meaning they can only generate entity object code from data collections, ensuring synchronization of entity object data structures.
  • The entity data entity objects generated by gen dao are primarily used for HTTP protocol services, although they are generic for the Golang language. In HTTP services, the entity generated in gen dao, although under the internal directory, will ultimately be part of the HTTP API response serving the client.

Differences

  • In GRPC services, the entity data structures generated by gen dao cannot be used by GRPC interfaces because GRPC data structures need to be defined using proto files. Therefore, in GRPC services, the pbentity proto files generated by gen pbentity are needed. Moreover, in GRPC microservice development, the entity generated by gen dao no longer has specific functions.
  • The name pbentity is used instead of entity to avoid conflicts with the entity in gen dao.