user.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package schema
  2. import (
  3. "entgo.io/contrib/entgql"
  4. "entgo.io/ent"
  5. "entgo.io/ent/dialect/entsql"
  6. "entgo.io/ent/schema/edge"
  7. "entgo.io/ent/schema/field"
  8. "entgo.io/ent/schema/index"
  9. )
  10. // User holds the schema definition for the User entity.
  11. type User struct {
  12. ent.Schema
  13. }
  14. // Fields of the User.
  15. func (User) Fields() []ent.Field {
  16. return []ent.Field{
  17. // field.UUID("drnid", uuid.UUID{}).
  18. // Default(uuid.New).
  19. // StorageKey("drnid"),
  20. field.Int("age").
  21. Positive(),
  22. field.String("name").
  23. Comment("This is the last name"). // Go comment, not DB comment
  24. Default("unknown").
  25. StructTag(`gqlgen:"gql_name"`),
  26. field.String("password").
  27. Optional().
  28. Sensitive(),
  29. field.Enum("size").
  30. Comment("the helmet size").
  31. Default("M").
  32. Values("XS", "S", "M", "L", "XL"),
  33. field.Int("spouse_id").
  34. Optional(),
  35. }
  36. }
  37. // Edges of the User.
  38. func (User) Edges() []ent.Edge {
  39. return []ent.Edge{
  40. edge.To("cars", Car.Type),
  41. // create an inverse-edge called "groups" of type `Group`
  42. // and reference it to the "users" edge (in Group schema)
  43. // explicitly using the `Ref` method.
  44. edge.From("groups", Group.Type). // O2M two types
  45. Ref("users"), // cf Group.Edges: "users"
  46. edge.To("spouse", User.Type). // O2O Bidirectional
  47. Unique().
  48. Field("spouse_id"),
  49. edge.To("following", User.Type). // M2M Same type
  50. From("followers"), // Implicit Ref("following" because we're chained on it.
  51. edge.To("friends", User.Type). // M2M bidirectional
  52. Annotations(
  53. entgql.Bind(),
  54. entgql.Annotation{OrderField: "OWNER"},
  55. ),
  56. }
  57. }
  58. func (User) Indexes() []ent.Index {
  59. return []ent.Index{
  60. index.Fields("age", "name").Unique(),
  61. index.Fields("name").Annotations(entsql.Prefix(10)),
  62. }
  63. }