qnc-server-tob/deploy/script/README.md

164 lines
3.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Go-Zero 代码生成工具
这是一个用于 Go-Zero 微服务项目的代码生成工具,支持生成数据库模型和 API 代码。
## 功能特性
- 数据库模型生成:支持生成 Go-Zero 风格的数据库模型代码
- API 代码生成:支持生成 API 服务相关代码
- 灵活的表名管理:支持通过命令行指定表名,无需修改配置文件
- 配置文件支持:通过 config.json 配置数据库连接和表列表
- 命令行参数:支持通过命令行参数覆盖配置文件设置
## 安装
确保你的系统已安装 Node.js10.0+)。
```bash
# 安装依赖
cd deploy/script
npm install
```
## 使用方法
### 查看配置
```bash
npm run config
```
### 生成所有配置的表模型
```bash
npm run model
```
### 生成指定表的模型
```bash
# 生成单个表
npm run model:table agent
# 生成多个表(逗号分隔,不含空格)
npm run model:tables agent,user,order
# 或者使用完整命令
node codegen.js model --tables agent,user,order
```
### 排除指定表
```bash
# 从配置的表中排除某些表
npm run model:exclude user,order
# 或者使用完整命令
node codegen.js model --exclude user,order
```
### 生成所有数据库表的模型
```bash
npm run model:all
# 生成所有表但排除某些表
node codegen.js model --all --exclude system_table,temp_table
```
### 保存表配置
```bash
# 生成指定表并保存到配置文件
npm run model:save agent,user,order -- --save
# 或者使用完整命令
node codegen.js model --tables agent,user,order --save
```
### 生成 API 代码
```bash
npm run api
```
### 生成带样式的 API 代码
```bash
npm run api:style
```
### 查看帮助信息
```bash
npm run help
```
## 配置文件
所有配置都保存在`config.json`文件中,你可以编辑此文件来修改数据库连接信息和要生成的表列表:
```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.json`的`tables`数组中列出要生成的表
2. **命令行指定单个表**:使用`--table`参数指定单个表
3. **命令行指定多个表**:使用`--tables`参数指定多个表(逗号分隔)
4. **排除特定表**:使用`--exclude`参数排除特定表
5. **生成所有表**:使用`--all`参数生成数据库中的所有表
6. **保存到配置**:使用`--save`参数将当前指定的表保存到配置文件
这种灵活的表名管理方式使你可以根据需要随时调整要生成的表,而无需频繁修改配置文件。
## 高级用法
### 通过命令行参数覆盖配置
```bash
# 使用不同的数据库连接
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
```
### 组合使用参数
```bash
# 生成所有表但排除系统表,并使用自定义模板
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` - 配置文件