Skip to content

Commit

Permalink
docs: optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
yikakia authored and tr1v3r committed Apr 30, 2022
1 parent 986a4f1 commit 0048646
Showing 2 changed files with 28 additions and 12 deletions.
18 changes: 12 additions & 6 deletions README.ZH_CN.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@

- 自动生成 CRUD 和 DIY 方法
- 自动根据表结构生成模型(model)代码
- 事务、嵌套事务、保存点、回滚事务点
- 完全兼容 GORM
- 更安全、更友好
- 多种生成代码模式
@@ -94,6 +95,7 @@
- [预加载](#preloading)
- [预加载(Preload)](#preload)
- [预加载全部数据(Preload All)](#preload-all)
- [预加载指定列](#preload-with-select)
- [根据条件预加载](#preload-with-conditions)
- [嵌套预加载](#nested-preloading)
- [更新](#update)
@@ -216,6 +218,7 @@ demo
│ └── query # generated code's directory
| ├── user.gen.go # generated code for user
│ └── gen.go # generated code
| └── user.gen_test.go # generated unit test
├── biz
│ └── query.go # call function in dal/gorm_generated.go and query databases
├── config
@@ -257,6 +260,7 @@ FieldType // specify field type
FieldTypeReg // specify field type (match with regexp)
FieldTag // specify gorm and json tag
FieldJSONTag // specify json tag
FieldJSONTagWithNS // specify new tag with name strategy
FieldGORMTag // specify gorm tag
FieldNewTag // append new tag
FieldNewTagWithNS // specify new tag with name strategy
@@ -729,6 +733,7 @@ c := q.CreditCard
type Result struct {
Name string
Email string
ID int64
}

var result Result
@@ -1328,6 +1333,8 @@ u.WithContext(ctx).Omit(field.AssociationFields).Create(&user)

###### <span id="find-associations">查询关联</span>

查询匹配的关联

```go
u := query.Use(db).User

@@ -1412,6 +1419,8 @@ db.Select(field.AssociationsFields).Delete(&user)

##### <span id="preloading">预加载</span>

此功能目前仅支持现有模型

###### <span id="preload">预加载(Preload)</span>

GEN 允许使用 `Preload` 在其他 SQL 中预先加载关系,例如:
@@ -1586,7 +1595,7 @@ u.WithContext(ctx).Where(u.Activate.Is(true)).UpdateSimple(u.Age.Value(17), u.Nu
// UPDATE users SET age=17, number=0, birthday=NULL, updated_at='2013-11-17 21:34:10' WHERE active=true;
```

> **NOTE** When update with struct, GEN will only update non-zero fields, you might want to use `map` to update attributes or use `Select` to specify fields to update
> **注意** 当通过 struct 更新的时候,GEN 将只会更新其非零值的字段,你可能需要用 `map` 去更新属性,或者用 `select` 去明确指定哪些字段是需要被更新的
##### <span id="update-selected-fields">更新指定字段</span>

@@ -1869,7 +1878,6 @@ select * from @@table where

##### <span id="method-interface-example">方法接口示例</span>


```go
type Method interface {
// Where("name=@name and age=@age")
@@ -1909,11 +1917,10 @@ type Method interface {
// {{for _,user:=range users}}
// {{if user.Age >18}
// OR [email protected]
// {{end}}
// {{end}}
// {{end}}
// {{end}}
FindByOrList(cond bool, id int, key, value string) ([]gen.T, error)

FindByOrList(users []gen.T) ([]gen.T, error)
}
```

@@ -1925,7 +1932,6 @@ type Method interface {

一个测试用例包含输入和期望结果,输入应和对应的方法参数匹配,期望应和对应的方法返回值相匹配。这将在测试中被断言为 “**Equal(相等)**”。


```go
package query

22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1758,8 +1758,11 @@ Logical operations must be wrapped in `{{}}`,and end must used `{{end}}`, All te
Use case in raw SQL:

```go
// select * from users where {{if name !=""}} name=@name{{end}}
methond(name string) (gen.T,error)
// select * from users where
// {{if name !=""}}
// name=@name
// {{end}}
methond(name string) (gen.T,error)
```

Use case in raw SQL template:
@@ -1792,7 +1795,10 @@ select * from @@table where
Use case in raw SQL

```go
// select * from {{where}}id=@id{{end}}
// select * from
// {{where}}
// id=@id
// {{end}}
methond(id int) error
```

@@ -1817,8 +1823,12 @@ select * from @@table
Use case in raw SQL

```go
// update users {{set}}name=@name{{end}}
methond() error
// update users
// {{set}}
// name=@name
// {{end}}
// where id=@id
methond(name string,id int) error
```

Use case in raw SQL template
@@ -1906,7 +1916,7 @@ type Method interface {
// {{end}}
// {{end}}
FindByOrList(users []gen.T) ([]gen.T, error)
}
}
```

#### Unit Test

0 comments on commit 0048646

Please sign in to comment.