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

     1  package schema
     2  
     3  import (
     4  	"entgo.io/ent"
     5  	"entgo.io/ent/dialect"
     6  	"entgo.io/ent/dialect/entsql"
     7  	"entgo.io/ent/schema"
     8  	"entgo.io/ent/schema/edge"
     9  	"entgo.io/ent/schema/field"
    10  	"entgo.io/ent/schema/index"
    11  )
    12  
    13  // File holds the schema definition for the File entity.
    14  type File struct {
    15  	ent.Schema
    16  }
    17  
    18  // Fields of the File.
    19  func (File) Fields() []ent.Field {
    20  	return []ent.Field{
    21  		field.String("disk"),
    22  		field.String("path").SchemaType(map[string]string{
    23  			dialect.MySQL: "varchar(500)",
    24  		}),
    25  		field.String("type"),
    26  		field.Int("size"),
    27  		field.Int("user_id").Optional(),
    28  	}
    29  }
    30  
    31  // Edges of the File.
    32  func (File) Edges() []ent.Edge {
    33  	return []ent.Edge{
    34  		edge.From("user", User.Type).Ref("files").Field("user_id").Unique(),
    35  
    36  		edge.To("posts", Post.Type).
    37  			Annotations(ondeleteSetNull).
    38  			StorageKey(edge.Column("featured_image_id"), edge.Symbol("post_featured_image")),
    39  
    40  		edge.To("pages", Page.Type).
    41  			Annotations(ondeleteSetNull).
    42  			StorageKey(edge.Column("featured_image_id"), edge.Symbol("page_featured_image")),
    43  
    44  		edge.To("user_avatars", User.Type).
    45  			Annotations(ondeleteSetNull).
    46  			StorageKey(edge.Column("avatar_image_id"), edge.Symbol("user_avatar_image")),
    47  	}
    48  }
    49  
    50  func (File) Indexes() []ent.Index {
    51  	return []ent.Index{
    52  		index.Fields("path").StorageKey("path_idx"),
    53  	}
    54  }
    55  
    56  func (File) Annotations() []schema.Annotation {
    57  	return []schema.Annotation{
    58  		entsql.Annotation{
    59  			Charset:   "utf8mb4",
    60  			Collation: "utf8mb4_unicode_ci",
    61  		},
    62  	}
    63  }
    64  
    65  func (File) Mixin() []ent.Mixin {
    66  	return []ent.Mixin{
    67  		TimeStamp{},
    68  	}
    69  }