1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
---
id: schema-mixin
title: Mixin
---
A `Mixin` allows you to create reusable pieces of `ent.Schema` code.
The `ent.Mixin` interface is as follows:
```go
type Mixin interface {
// Fields returns a slice of fields to add to the schema.
Fields() []Field
// Edges returns a slice of edges to add to the schema.
Edges() []Edge
// Indexes returns a slice of indexes to add to the schema.
Indexes() []Index
// Hooks returns a slice of hooks to add to the schema.
// Note that mixin hooks are executed before schema hooks.
Hooks() []Hook
// Policy returns a privacy policy to add to the schema.
// Note that mixin policy are executed before schema policy.
Policy() Policy
// Annotations returns a list of schema annotations to add
// to the schema annotations.
Annotations() []schema.Annotation
}
```
## Example
A common use case for `Mixin` is to mix-in a list of common fields to your schema.
```go
package schema
import (
"time"
"github.com/facebook/ent"
"github.com/facebook/ent/schema/field"
"github.com/facebook/ent/schema/mixin"
)
// -------------------------------------------------
// Mixin definition
// TimeMixin implements the ent.Mixin for sharing
// time fields with package schemas.
type TimeMixin struct{
// We embed the `mixin.Schema` to avoid
// implementing the rest of the methods.
mixin.Schema
}
func (TimeMixin) Fields() []ent.Field {
return []ent.Field{
field.Time("created_at").
Immutable().
Default(time.Now),
field.Time("updated_at").
Default(time.Now).
UpdateDefault(time.Now),
}
}
// DetailsMixin implements the ent.Mixin for sharing
// entity details fields with package schemas.
type DetailsMixin struct{
// We embed the `mixin.Schema` to avoid
// implementing the rest of the methods.
mixin.Schema
}
func (DetailsMixin) Fields() []ent.Field {
return []ent.Field{
field.Int("age").
Positive(),
field.String("name").
NotEmpty(),
}
}
// -------------------------------------------------
// Schema definition
// User schema mixed-in the TimeMixin and DetailsMixin fields and therefore
// has 5 fields: `created_at`, `updated_at`, `age`, `name` and `nickname`.
type User struct {
ent.Schema
}
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
TimeMixin{},
DetailsMixin{},
}
}
func (User) Fields() []ent.Field {
return []ent.Field{
field.String("nickname").
Unique(),
}
}
// Pet schema mixed-in the DetailsMixin fields and therefore
// has 3 fields: `age`, `name` and `weight`.
type Pet struct {
ent.Schema
}
func (Pet) Mixin() []ent.Mixin {
return []ent.Mixin{
DetailsMixin{},
}
}
func (Pet) Fields() []ent.Field {
return []ent.Field{
field.Float("weight"),
}
}
```
## 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:
```go
package schema
import (
"github.com/facebook/ent"
"github.com/facebook/ent/schema/mixin"
)
type Pet struct {
ent.Schema
}
func (Pet) Mixin() []ent.Mixin {
return []ent.Mixin{
mixin.Time{},
// Or, mixin.CreateTime only for create_time
// and mixin.UpdateTime only for update_time.
}
}
```
|