68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package schema
|
|
|
|
import (
|
|
"entgo.io/contrib/entgql"
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/dialect/entsql"
|
|
"entgo.io/ent/schema/edge"
|
|
"entgo.io/ent/schema/field"
|
|
"entgo.io/ent/schema/index"
|
|
)
|
|
|
|
// User holds the schema definition for the User entity.
|
|
type User struct {
|
|
ent.Schema
|
|
}
|
|
|
|
// Fields of the User.
|
|
func (User) Fields() []ent.Field {
|
|
return []ent.Field{
|
|
// field.UUID("drnid", uuid.UUID{}).
|
|
// Default(uuid.New).
|
|
// StorageKey("drnid"),
|
|
field.Int("age").
|
|
Positive(),
|
|
field.String("name").
|
|
Comment("This is the last name"). // Go comment, not DB comment
|
|
Default("unknown").
|
|
StructTag(`gqlgen:"gql_name"`),
|
|
field.String("password").
|
|
Optional().
|
|
Sensitive(),
|
|
field.Enum("size").
|
|
Comment("the helmet size").
|
|
Default("M").
|
|
Values("XS", "S", "M", "L", "XL"),
|
|
field.Int("spouse_id").
|
|
Optional(),
|
|
}
|
|
}
|
|
|
|
// Edges of the User.
|
|
func (User) Edges() []ent.Edge {
|
|
return []ent.Edge{
|
|
edge.To("cars", Car.Type),
|
|
// create an inverse-edge called "groups" of type `Group`
|
|
// and reference it to the "users" edge (in Group schema)
|
|
// explicitly using the `Ref` method.
|
|
edge.From("groups", Group.Type). // O2M two types
|
|
Ref("users"), // cf Group.Edges: "users"
|
|
edge.To("spouse", User.Type). // O2O Bidirectional
|
|
Unique().
|
|
Field("spouse_id"),
|
|
edge.To("following", User.Type). // M2M Same type
|
|
From("followers"), // Implicit Ref("following" because we're chained on it.
|
|
edge.To("friends", User.Type). // M2M bidirectional
|
|
Annotations(
|
|
entgql.Bind(),
|
|
entgql.Annotation{OrderField: "OWNER"},
|
|
),
|
|
}
|
|
}
|
|
|
|
func (User) Indexes() []ent.Index {
|
|
return []ent.Index{
|
|
index.Fields("age", "name").Unique(),
|
|
index.Fields("name").Annotations(entsql.Prefix(10)),
|
|
}
|
|
}
|