mirror of
https://github.com/ent/ent.git
synced 2026-05-22 09:31:45 +03:00
doc: add schema annotation to entgo.io (#880)
This commit is contained in:
45
doc/md/schema-annotations.md
Executable file
45
doc/md/schema-annotations.md
Executable file
@@ -0,0 +1,45 @@
|
||||
---
|
||||
id: schema-annotations
|
||||
title: Annotations
|
||||
---
|
||||
|
||||
Schema annotations allow attaching metadata to schema objects like fields and edges and inject them to external templates.
|
||||
An annotation must be a Go type that is serializable to JSON raw value (e.g. struct, map or slice)
|
||||
and implement the [Annotation](https://pkg.go.dev/github.com/facebook/ent/schema/field?tab=doc#Annotation) interface.
|
||||
|
||||
The builtin annotations allow configuring the different storage drivers (like SQL) and control the code generation output.
|
||||
|
||||
## Custom Table Name
|
||||
|
||||
A custom table name can be provided for types using the `entsql` annotation as follows:
|
||||
|
||||
```go
|
||||
package schema
|
||||
|
||||
import (
|
||||
"github.com/facebook/ent"
|
||||
"github.com/facebook/ent/dialect/entsql"
|
||||
"github.com/facebook/ent/schema"
|
||||
"github.com/facebook/ent/schema/field"
|
||||
)
|
||||
|
||||
// User holds the schema definition for the User entity.
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
// Annotations of the User.
|
||||
func (User) Annotations() []schema.Annotation {
|
||||
return []schema.Annotation{
|
||||
entsql.Annotation{Table: "Users"},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the User.
|
||||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("age"),
|
||||
field.String("name"),
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -1,34 +0,0 @@
|
||||
---
|
||||
id: schema-config
|
||||
title: Config
|
||||
---
|
||||
|
||||
## Custom Table Name
|
||||
|
||||
A custom table name can be provided for types using the `Table` option as follows:
|
||||
|
||||
```go
|
||||
package schema
|
||||
|
||||
import (
|
||||
"github.com/facebook/ent"
|
||||
"github.com/facebook/ent/schema/field"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ent.Schema
|
||||
}
|
||||
|
||||
func (User) Config() ent.Config {
|
||||
return ent.Config{
|
||||
Table: "Users",
|
||||
}
|
||||
}
|
||||
|
||||
func (User) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.Int("age"),
|
||||
field.String("name"),
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -69,7 +69,7 @@ In order to override an existing template, use its name. For example:
|
||||
```
|
||||
|
||||
## Annotations
|
||||
Schema annotations allow to attach metadata to fields and edges and inject them to external templates.
|
||||
Schema annotations allow attaching metadata to fields and edges and inject them to external templates.
|
||||
An annotation must be a Go type that is serializable to JSON raw value (e.g. struct, map or slice)
|
||||
and implement the [Annotation](https://pkg.go.dev/github.com/facebook/ent/schema/field?tab=doc#Annotation) interface.
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@
|
||||
"schema-fields",
|
||||
"schema-edges",
|
||||
"schema-indexes",
|
||||
"schema-config",
|
||||
"schema-mixin"
|
||||
"schema-mixin",
|
||||
"schema-annotations"
|
||||
],
|
||||
"Code Generation": [
|
||||
"code-gen",
|
||||
|
||||
Reference in New Issue
Block a user