doc: update documentation to include naming convention (#1144)

* Update documentation to include naming convention

This change relates to my question in #1107 around naming convention for fields / edges. As a bonus, I added documentation around the StructTag method for edges, as seemed to be omitted.

* s/camel/Pascal/g
This commit is contained in:
Andy Day
2021-01-07 13:58:38 -08:00
committed by GitHub
parent cfb8f5c4a9
commit 8eda87dbc3
2 changed files with 27 additions and 0 deletions

View File

@@ -898,6 +898,23 @@ func (User) Edges() []ent.Edge {
}
```
## Struct Tags
Custom struct tags can be added to the generated entities using the `StructTag`
method. Note that if this option was not provided, or provided and did not
contain the `json` tag, the default `json` tag will be added with the field name.
```go
// Edges of the User.
func (User) Edges() []ent.Edge {
return []ent.Edge{
edge.To("pets", Pet.Type).
// Override the default json tag "pets" with "owner" for O2M relationship.
StructTag(`json:"owner"`),
}
}
```
## Indexes
Indexes can be defined on multi fields and some types of edges as well.
@@ -932,3 +949,8 @@ func (Pet) Edges() []ent.Edge {
```
Read more about annotations and their usage in templates in the [template doc](templates.md#annotations).
## Naming Convention
By convention edge names should use `snake_case`. The corresponding struct fields generated by `ent` will follow the Go convention
of using `PascalCase`. In cases where `PascalCase` is desired, you can do so with the `StorageKey` or `StructTag` methods.

View File

@@ -554,3 +554,8 @@ func (User) Fields() []ent.Field {
```
Read more about annotations and their usage in templates in the [template doc](templates.md#annotations).
## Naming Convention
By convention field names should use `snake_case`. The corresponding struct fields generated by `ent` will follow the Go convention
of using `PascalCase`. In cases where `PascalCase` is desired, you can do so with the `StorageKey` or `StructTag` methods.