Mixin

A Mixin allows you to create reusable pieces of ent.Schema code that can be injected into other schemas using composition.

The ent.Mixin interface is as follows:

  1. type Mixin interface {
  2. // Fields returns a slice of fields to add to the schema.
  3. Fields() []Field
  4. // Edges returns a slice of edges to add to the schema.
  5. Edges() []Edge
  6. // Indexes returns a slice of indexes to add to the schema.
  7. Indexes() []Index
  8. // Hooks returns a slice of hooks to add to the schema.
  9. // Note that mixin hooks are executed before schema hooks.
  10. Hooks() []Hook
  11. // Policy returns a privacy policy to add to the schema.
  12. // Note that mixin policy are executed before schema policy.
  13. Policy() Policy
  14. // Annotations returns a list of schema annotations to add
  15. // to the schema annotations.
  16. Annotations() []schema.Annotation
  17. }

Example

A common use case for Mixin is to mix-in a list of common fields to your schema.

  1. package schema
  2. import (
  3. "time"
  4. "entgo.io/ent"
  5. "entgo.io/ent/schema/field"
  6. "entgo.io/ent/schema/mixin"
  7. )
  8. // -------------------------------------------------
  9. // Mixin definition
  10. // TimeMixin implements the ent.Mixin for sharing
  11. // time fields with package schemas.
  12. type TimeMixin struct{
  13. // We embed the `mixin.Schema` to avoid
  14. // implementing the rest of the methods.
  15. mixin.Schema
  16. }
  17. func (TimeMixin) Fields() []ent.Field {
  18. return []ent.Field{
  19. field.Time("created_at").
  20. Immutable().
  21. Default(time.Now),
  22. field.Time("updated_at").
  23. Default(time.Now).
  24. UpdateDefault(time.Now),
  25. }
  26. }
  27. // DetailsMixin implements the ent.Mixin for sharing
  28. // entity details fields with package schemas.
  29. type DetailsMixin struct{
  30. // We embed the `mixin.Schema` to avoid
  31. // implementing the rest of the methods.
  32. mixin.Schema
  33. }
  34. func (DetailsMixin) Fields() []ent.Field {
  35. return []ent.Field{
  36. field.Int("age").
  37. Positive(),
  38. field.String("name").
  39. NotEmpty(),
  40. }
  41. }
  42. // -------------------------------------------------
  43. // Schema definition
  44. // User schema mixed-in the TimeMixin and DetailsMixin fields and therefore
  45. // has 5 fields: `created_at`, `updated_at`, `age`, `name` and `nickname`.
  46. type User struct {
  47. ent.Schema
  48. }
  49. func (User) Mixin() []ent.Mixin {
  50. return []ent.Mixin{
  51. TimeMixin{},
  52. DetailsMixin{},
  53. }
  54. }
  55. func (User) Fields() []ent.Field {
  56. return []ent.Field{
  57. field.String("nickname").
  58. Unique(),
  59. }
  60. }
  61. // Pet schema mixed-in the DetailsMixin fields and therefore
  62. // has 3 fields: `age`, `name` and `weight`.
  63. type Pet struct {
  64. ent.Schema
  65. }
  66. func (Pet) Mixin() []ent.Mixin {
  67. return []ent.Mixin{
  68. DetailsMixin{},
  69. }
  70. }
  71. func (Pet) Fields() []ent.Field {
  72. return []ent.Field{
  73. field.Float("weight"),
  74. }
  75. }

Builtin Mixin

Package mixin provides a few builtin mixins that can be used for adding the create_time and update_time fields to the schema.

In order to use them, add the mixin.Time mixin to your schema as follows:

  1. package schema
  2. import (
  3. "entgo.io/ent"
  4. "entgo.io/ent/schema/mixin"
  5. )
  6. type Pet struct {
  7. ent.Schema
  8. }
  9. func (Pet) Mixin() []ent.Mixin {
  10. return []ent.Mixin{
  11. mixin.Time{},
  12. // Or, mixin.CreateTime only for create_time
  13. // and mixin.UpdateTime only for update_time.
  14. }
  15. }