-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from go-cinch/main
[feat]add gorm gen doc
- Loading branch information
Showing
4 changed files
with
126 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
# Gorm Gen数据库映射 | ||
|
||
项目内置gorm/gen, 数据库映射到struct, 通过工具自动生成. | ||
|
||
## 修改配置 | ||
|
||
### dsn | ||
|
||
数据库连接地址. | ||
|
||
### tables | ||
|
||
需要生成的表集合. | ||
|
||
### exclude | ||
|
||
不需要生成的表集合. | ||
|
||
### association | ||
|
||
关联关系. | ||
|
||
一般是5个字段, 通过`|`分隔, `当前表名`|`关联表名`|`结构体名称`|`关系(has_one/has_many/belongs_to/many_to_many)`|`外键` | ||
|
||
!> belongs_to不常用, 具体用法参考gorm/gen官方文档 | ||
|
||
#### 一对一 | ||
|
||
示例, `一个用户一个角色`: | ||
|
||
```yml | ||
gen: | ||
association: | ||
- 'user|role|Role|has_one|foreignKey:RoleID' | ||
``` | ||
#### 一对多 | ||
示例, `一个问题多个选项`: | ||
|
||
```yml | ||
gen: | ||
association: | ||
- 'question|option|[]Options|has_many|foreignKey:QuestionID' | ||
``` | ||
|
||
#### 多对多 | ||
|
||
示例, `一个用户组可能有多个用户, 一个用户可能存在多个用户组`: | ||
|
||
```yml | ||
gen: | ||
association: | ||
- 'user_group|user|[]Users|many_to_many|many2many:user_user_group_relation' | ||
- 'user|user_group|[]UserGroups|many_to_many|many2many:user_user_group_relation' | ||
``` | ||
|
||
#### 特别 | ||
|
||
将字段映射为指针类型: | ||
|
||
`表名`|`指针符号+字段1,指针符号+字段2,...` | ||
|
||
```yml | ||
gen: | ||
association: | ||
- 'user|*Name,*Role' | ||
``` | ||
|
||
### field-with-string-tag | ||
|
||
将字段json增加string后缀: | ||
|
||
`表名`|`字段1,字段2,...` | ||
|
||
```yml | ||
gen: | ||
field-with-string-tag: | ||
- 'user|id,role_id' | ||
``` | ||
|
||
生成的结构体, json tag会像这样: | ||
|
||
```go | ||
type User struct { | ||
ID uint64 `json:"id,string"` | ||
RoleID uint64 `json:"roleId,string"` | ||
} | ||
``` | ||
|
||
?> 这个功能主要是解决uint64在前端js会溢出, 通过映射为string来作为前端类型 | ||
|
||
## 生成映射 | ||
|
||
```bash | ||
cinch gen gorm | ||
# 🍺 Generate gorm succeeded | ||
# model path internal/data/query | ||
# query path internal/data/model | ||
``` | ||
|
||
data目录下会新增两个目录, 其中内容包含model和query, 基于gorm/gen自动生成 | ||
|
||
!> 不允许修改或新增model和query中的任何文件, 被覆盖后果自负 | ||
|
||
## 源码 | ||
|
||
参见[cinch/gorm](https://github.com/go-cinch/cinch/blob/dev/cmd/cinch/internal/gen/gorm/gorm.go), 有任何问题或者新需求, 欢迎提issue或pr~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,7 +136,7 @@ git submodule add -b master --name api/game-proto https://github.com/go-cinch/ga | |
# sudo apt install -y make | ||
make init | ||
|
||
# 5. 编译项目 | ||
# 5. 做启动前的所有检查 | ||
make all | ||
``` | ||
|
||
|
@@ -171,23 +171,23 @@ cinch run | |
```bash | ||
# 启动auth | ||
# 如果你用的是compose/single | ||
export AUTH_DATA_DATABASE_DSN='root:mysqlrootpwd@tcp(127.0.0.1:3306)/auth?parseTime=True' | ||
export AUTH_DATA_REDIS_DSN='redis://:[email protected]:6379/0' | ||
export SERVICE_DATA_DATABASE_DSN='root:mysqlrootpwd@tcp(127.0.0.1:3306)/auth?parseTime=True' | ||
export SERVICE_DATA_REDIS_DSN='redis://:[email protected]:6379/0' | ||
# 其他按需修改 | ||
export AUTH_DATA_DATABASE_DSN='root:root@tcp(127.0.0.1:3306)/auth?parseTime=True' | ||
export AUTH_DATA_REDIS_DSN='redis://127.0.0.1:6379/0' | ||
export SERVICE_DATA_DATABASE_DSN='root:root@tcp(127.0.0.1:3306)/auth?parseTime=True' | ||
export SERVICE_DATA_REDIS_DSN='redis://127.0.0.1:6379/0' | ||
cd auth | ||
cinch run | ||
|
||
# 启动game | ||
# 如果你用的是compose/single | ||
export GAME_DATA_DATABASE_DSN='root:mysqlrootpwd@tcp(127.0.0.1:3306)/game?parseTime=True' | ||
export GAME_DATA_REDIS_DSN='redis://:[email protected]:6379/0' | ||
export SERVICE_DATA_DATABASE_DSN='root:mysqlrootpwd@tcp(127.0.0.1:3306)/game?parseTime=True' | ||
export SERVICE_DATA_REDIS_DSN='redis://:[email protected]:6379/0' | ||
# 其他按需修改 | ||
export GAME_DATA_DATABASE_DSN='root:root@tcp(127.0.0.1:3306)/game?parseTime=True' | ||
export GAME_DATA_REDIS_DSN='redis://127.0.0.1:6379/0' | ||
export SERVICE_DATA_DATABASE_DSN='root:root@tcp(127.0.0.1:3306)/game?parseTime=True' | ||
export SERVICE_DATA_REDIS_DSN='redis://127.0.0.1:6379/0' | ||
# 设置auth服务 | ||
export GAME_CLIENT_AUTH='127.0.0.1:6160' | ||
export SERVICE_CLIENT_AUTH='127.0.0.1:6160' | ||
cd game | ||
cinch run | ||
``` | ||
|
@@ -208,7 +208,7 @@ curl http://127.0.0.1:6060/idempotent | |
game服务: | ||
|
||
```bash | ||
curl http://127.0.0.1:8080/hello/world | ||
curl http://127.0.0.1:8080/game/list | ||
# 输出如下说明服务通了只是没有权限, 出现其他说明配置有误 | ||
# {"code":401, "reason":"UNAUTHORIZED", "message":"token is missing", "metadata":{}} | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters