github.com/ngocphuongnb/tetua@v0.0.7-alpha/packages/entrepository/ent/schema/topic.go (about)

     1  package schema
     2  
     3  import (
     4  	"entgo.io/ent"
     5  	"entgo.io/ent/dialect/entsql"
     6  	"entgo.io/ent/schema"
     7  	"entgo.io/ent/schema/edge"
     8  	"entgo.io/ent/schema/field"
     9  )
    10  
    11  // Topic holds the schema definition for the Topic entity.
    12  type Topic struct {
    13  	ent.Schema
    14  }
    15  
    16  // Fields of the Topic.
    17  func (Topic) Fields() []ent.Field {
    18  	return []ent.Field{
    19  		field.String("name").StructTag(`validate:"max=255"`).Unique(),
    20  		field.String("slug").StructTag(`validate:"max=255"`).Unique(),
    21  		field.String("description").Optional().StructTag(`validate:"max=255"`),
    22  		field.Text("content").StructTag(`validate:"required"`),
    23  		field.Text("content_html").StructTag(`validate:"required"`),
    24  		field.Int("parent_id").Optional(),
    25  	}
    26  }
    27  
    28  // Edges of the Topic.
    29  func (Topic) Edges() []ent.Edge {
    30  	return []ent.Edge{
    31  		edge.To("posts", Post.Type).
    32  			Annotations(ondeleteSetNull),
    33  		edge.To("children", Topic.Type).
    34  			Annotations(ondeleteSetNull).
    35  			StorageKey(edge.Column("parent_id"), edge.Symbol("topic_parent")),
    36  		edge.From("parent", Topic.Type).
    37  			Field("parent_id").
    38  			Ref("children").
    39  			Unique(),
    40  	}
    41  }
    42  
    43  func (Topic) Annotations() []schema.Annotation {
    44  	return []schema.Annotation{
    45  		entsql.Annotation{
    46  			Charset:   "utf8mb4",
    47  			Collation: "utf8mb4_unicode_ci",
    48  		},
    49  	}
    50  }
    51  
    52  func (Topic) Mixin() []ent.Mixin {
    53  	return []ent.Mixin{
    54  		TimeStamp{},
    55  	}
    56  }