48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
|
package schema
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
|
||
|
"entgo.io/contrib/entgql"
|
||
|
"entgo.io/ent"
|
||
|
"entgo.io/ent/schema"
|
||
|
"entgo.io/ent/schema/edge"
|
||
|
"entgo.io/ent/schema/field"
|
||
|
"github.com/google/uuid"
|
||
|
)
|
||
|
|
||
|
// Post holds the schema definition for the Post entity.
|
||
|
type Post struct {
|
||
|
ent.Schema
|
||
|
}
|
||
|
|
||
|
// Fields of the Post.
|
||
|
func (Post) Fields() []ent.Field {
|
||
|
return []ent.Field{
|
||
|
field.UUID("id", uuid.UUID{}).Default(uuid.New).Unique().Immutable(),
|
||
|
field.Time("created_at").Default(time.Now).Immutable().Annotations(entgql.OrderField("CREATED_AT")),
|
||
|
field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now).Optional().Annotations(entgql.OrderField("UPDATED_AT")),
|
||
|
field.Bool("expires").Default(false),
|
||
|
field.Time("expire_time").Nillable().Optional().Annotations(entgql.OrderField("EXPIRE_TIME")),
|
||
|
field.String("title").NotEmpty(),
|
||
|
field.Text("body").NotEmpty(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Edges of the Post.
|
||
|
func (Post) Edges() []ent.Edge {
|
||
|
return []ent.Edge{
|
||
|
edge.From("category", Category.Type).Ref("posts").Unique(),
|
||
|
edge.From("profile", Profile.Type).Ref("posts").Unique(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Annotations of the Post.
|
||
|
func (Post) Annotations() []schema.Annotation {
|
||
|
return []schema.Annotation{
|
||
|
entgql.RelayConnection(),
|
||
|
entgql.QueryField(),
|
||
|
entgql.Mutations(entgql.MutationCreate(), entgql.MutationUpdate()),
|
||
|
}
|
||
|
}
|