Mixin

Mixin 允许您创建可复用的 ent.Schema 代码,用与合成添加到其他 schemas

ent.Mixin 接口如下:

  1. type Mixin interface {
  2. // Fields 数组的返回值会被添加到 schema 中。
  3. Fields() []Field
  4. // Edges 数组返回值会被添加到 schema 中。
  5. Edges() []Edge
  6. // Indexes 数组的返回值会被添加到 schema 中。
  7. Indexes() []Index
  8. // Hooks 数组的返回值会被添加到 schema 中。
  9. // 请注意,mixin 的钩子会在 schema 的钩子之前被执行。
  10. Hooks() []Hook
  11. // Policy 数组的返回值会被添加到 schema 中。
  12. //请注意,mixin(混入)的 policy(策略)会在 schema 的 policy 之前被执行。
  13. Policy() Policy
  14. // Annotations 方法返回要添加到 Schema 中的注解列表。
  15. Annotations() []schema.Annotation
  16. }

示例

Mixin 多用于:将你 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. }

内置Mixin

mixin包提供了一些内置的mixin,它们可以用于在schema中添加create_timeupdate_time 字段。

若要使用它们,请将 mixin.Time mixin 添加到您的schema,如下:

  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. }