mirror of
https://github.com/ent/ent.git
synced 2026-04-28 05:30:56 +03:00
ent: initial support for edge schemas (#2560)
This commit is contained in:
@@ -12,17 +12,18 @@ import (
|
||||
|
||||
// A Descriptor for edge configuration.
|
||||
type Descriptor struct {
|
||||
Tag string // struct tag.
|
||||
Type string // edge type.
|
||||
Name string // edge name.
|
||||
Field string // edge field name (e.g. foreign-key).
|
||||
RefName string // ref name; inverse only.
|
||||
Ref *Descriptor // edge reference; to/from of the same type.
|
||||
Unique bool // unique edge.
|
||||
Inverse bool // inverse edge.
|
||||
Required bool // required on creation.
|
||||
StorageKey *StorageKey // optional storage-key configuration.
|
||||
Annotations []schema.Annotation // edge annotations.
|
||||
Tag string // struct tag.
|
||||
Type string // edge type.
|
||||
Name string // edge name.
|
||||
Field string // edge field name (e.g. foreign-key).
|
||||
RefName string // ref name; inverse only.
|
||||
Ref *Descriptor // edge reference; to/from of the same type.
|
||||
Through *struct{ N, T string } // through type and name.
|
||||
Unique bool // unique edge.
|
||||
Inverse bool // inverse edge.
|
||||
Required bool // required on creation.
|
||||
StorageKey *StorageKey // optional storage-key configuration.
|
||||
Annotations []schema.Annotation // edge annotations.
|
||||
}
|
||||
|
||||
// To defines an association edge between two vertices.
|
||||
@@ -86,6 +87,16 @@ func (b *assocBuilder) Field(f string) *assocBuilder {
|
||||
return b
|
||||
}
|
||||
|
||||
// Through allows setting an "edge schema" to interact explicitly with M2M edges.
|
||||
//
|
||||
// edge.To("friends", User.Type).
|
||||
// Through("friendships", Friendship.Type)
|
||||
//
|
||||
func (b *assocBuilder) Through(name string, t interface{}) *assocBuilder {
|
||||
b.desc.Through = &struct{ N, T string }{N: name, T: typ(t)}
|
||||
return b
|
||||
}
|
||||
|
||||
// Comment used to put annotations on the schema.
|
||||
func (b *assocBuilder) Comment(string) *assocBuilder {
|
||||
return b
|
||||
@@ -173,6 +184,17 @@ func (b *inverseBuilder) Field(f string) *inverseBuilder {
|
||||
return b
|
||||
}
|
||||
|
||||
// Through allows setting an "edge schema" to interact explicitly with M2M edges.
|
||||
//
|
||||
// edge.From("liked_users", User.Type).
|
||||
// Ref("liked_tweets").
|
||||
// Through("likes", TweetLike.Type)
|
||||
//
|
||||
func (b *inverseBuilder) Through(name string, t interface{}) *inverseBuilder {
|
||||
b.desc.Through = &struct{ N, T string }{N: name, T: typ(t)}
|
||||
return b
|
||||
}
|
||||
|
||||
// Annotations adds a list of annotations to the edge object to be used by
|
||||
// codegen extensions.
|
||||
//
|
||||
|
||||
@@ -19,6 +19,30 @@ type Annotation struct {
|
||||
// }
|
||||
//
|
||||
StructTag map[string]string
|
||||
|
||||
// ID defines a multi-field schema identifier. Note,
|
||||
// the annotation is valid only for edge schemas.
|
||||
//
|
||||
// func (TweetLike) Annotations() []schema.Annotation {
|
||||
// return []schema.Annotation{
|
||||
// field.ID("user_id", "tweet_id"),
|
||||
// }
|
||||
// }
|
||||
//
|
||||
ID []string
|
||||
}
|
||||
|
||||
// ID defines a multi-field schema identifier. Note, the
|
||||
// annotation is valid only for edge schemas.
|
||||
//
|
||||
// func (TweetLike) Annotations() []schema.Annotation {
|
||||
// return []schema.Annotation{
|
||||
// field.ID("user_id", "tweet_id"),
|
||||
// }
|
||||
// }
|
||||
//
|
||||
func ID(first, second string, fields ...string) *Annotation {
|
||||
return &Annotation{ID: append([]string{first, second}, fields...)}
|
||||
}
|
||||
|
||||
// Name describes the annotation name.
|
||||
@@ -45,10 +69,13 @@ func (a Annotation) Merge(other schema.Annotation) schema.Annotation {
|
||||
}
|
||||
a.StructTag[k] = v
|
||||
}
|
||||
if len(ant.ID) > 0 {
|
||||
a.ID = ant.ID
|
||||
}
|
||||
return a
|
||||
}
|
||||
|
||||
var (
|
||||
_ schema.Annotation = (*Annotation)(nil)
|
||||
_ schema.Merger = (*Annotation)(nil)
|
||||
)
|
||||
var _ interface {
|
||||
schema.Annotation
|
||||
schema.Merger
|
||||
} = (*Annotation)(nil)
|
||||
|
||||
Reference in New Issue
Block a user