qnc-server-tob/deploy/script
2025-06-03 12:24:15 +08:00
..
model new version qnc 2025-04-11 13:10:17 +08:00
codegen.js 新增代理实名认证和授权 2025-05-24 14:26:20 +08:00
config.json 新增代理实名认证和授权 2025-05-24 14:26:20 +08:00
gen_api.js 新增代理实名认证和授权 2025-05-24 14:26:20 +08:00
gen_models.js add sms authorization 2025-06-03 12:24:15 +08:00
gen_models.ps1 add sms authorization 2025-06-03 12:24:15 +08:00
package.json 新增代理实名认证和授权 2025-05-24 14:26:20 +08:00
README.md 新增代理实名认证和授权 2025-05-24 14:26:20 +08:00

Go-Zero 代码生成工具

这是一个用于 Go-Zero 微服务项目的代码生成工具,支持生成数据库模型和 API 代码。

功能特性

  • 数据库模型生成:支持生成 Go-Zero 风格的数据库模型代码
  • API 代码生成:支持生成 API 服务相关代码
  • 灵活的表名管理:支持通过命令行指定表名,无需修改配置文件
  • 配置文件支持:通过 config.json 配置数据库连接和表列表
  • 命令行参数:支持通过命令行参数覆盖配置文件设置

安装

确保你的系统已安装 Node.js10.0+)。

# 安装依赖
cd deploy/script
npm install

使用方法

查看配置

npm run config

生成所有配置的表模型

npm run model

生成指定表的模型

# 生成单个表
npm run model:table agent

# 生成多个表(逗号分隔,不含空格)
npm run model:tables agent,user,order

# 或者使用完整命令
node codegen.js model --tables agent,user,order

排除指定表

# 从配置的表中排除某些表
npm run model:exclude user,order

# 或者使用完整命令
node codegen.js model --exclude user,order

生成所有数据库表的模型

npm run model:all

# 生成所有表但排除某些表
node codegen.js model --all --exclude system_table,temp_table

保存表配置

# 生成指定表并保存到配置文件
npm run model:save agent,user,order -- --save

# 或者使用完整命令
node codegen.js model --tables agent,user,order --save

生成 API 代码

npm run api

生成带样式的 API 代码

npm run api:style

查看帮助信息

npm run help

配置文件

所有配置都保存在config.json文件中,你可以编辑此文件来修改数据库连接信息和要生成的表列表:

{
    "model": {
        "dbUrl": "user:password@tcp(host:port)/database",
        "outputDir": "./model",
        "templateDir": "../template",
        "targetDir": "../../app/user/model",
        "tables": ["table1", "table2"],
        "disabledTables": ["disabled_table1"]
    },
    "api": {
        "apiFile": "./app/user/cmd/api/desc/main.api",
        "outputDir": "./app/user/cmd/api",
        "templateDir": "./deploy/template",
        "useStyle": false
    }
}

表名管理

本工具提供了多种方式来指定需要生成的表:

  1. 配置文件指定:在config.jsontables数组中列出要生成的表
  2. 命令行指定单个表:使用--table参数指定单个表
  3. 命令行指定多个表:使用--tables参数指定多个表(逗号分隔)
  4. 排除特定表:使用--exclude参数排除特定表
  5. 生成所有表:使用--all参数生成数据库中的所有表
  6. 保存到配置:使用--save参数将当前指定的表保存到配置文件

这种灵活的表名管理方式使你可以根据需要随时调整要生成的表,而无需频繁修改配置文件。

高级用法

通过命令行参数覆盖配置

# 使用不同的数据库连接
node codegen.js model --db-url "user:pass@tcp(localhost:3306)/otherdb"

# 使用不同的API文件
node codegen.js api --api ./other_api.api

# 指定输出目录
node codegen.js api --dir ./output

组合使用参数

# 生成所有表但排除系统表,并使用自定义模板
node codegen.js model --all --exclude system_log,system_user --template ./my-templates

# 生成指定表并保存到配置
node codegen.js model --tables user,order,product --save

脚本文件说明

  • codegen.js - 统一入口脚本
  • gen_models.js - 模型生成脚本
  • gen_api.js - API 生成脚本
  • config.json - 配置文件