mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
schema/edge: an storage-key option for edges (#545)
* schema/edge: an storage-key option for edges * schema/edge: use Column(s) with functional options
This commit is contained in:
@@ -10,14 +10,15 @@ import (
|
||||
|
||||
// A Descriptor for edge configuration.
|
||||
type Descriptor struct {
|
||||
Tag string // struct tag.
|
||||
Type string // edge type.
|
||||
Name string // edge name.
|
||||
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.
|
||||
Tag string // struct tag.
|
||||
Type string // edge type.
|
||||
Name string // edge name.
|
||||
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.
|
||||
}
|
||||
|
||||
// To defines an association edge between two vertices.
|
||||
@@ -72,6 +73,21 @@ func (b *assocBuilder) Comment(string) *assocBuilder {
|
||||
return b
|
||||
}
|
||||
|
||||
// StorageKey sets the storage key of the edge.
|
||||
//
|
||||
// edge.To("groups").
|
||||
// StorageKey(edge.Table("user_groups"), edge.Columns("user_id", "group_id"))
|
||||
//
|
||||
func (b *assocBuilder) StorageKey(opts ...StorageOption) *assocBuilder {
|
||||
if b.desc.StorageKey == nil {
|
||||
b.desc.StorageKey = &StorageKey{}
|
||||
}
|
||||
for i := range opts {
|
||||
opts[i](b.desc.StorageKey)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// Descriptor implements the ent.Descriptor interface.
|
||||
func (b *assocBuilder) Descriptor() *Descriptor {
|
||||
return b.desc
|
||||
@@ -117,3 +133,37 @@ func (b *inverseBuilder) Comment(string) *inverseBuilder {
|
||||
func (b *inverseBuilder) Descriptor() *Descriptor {
|
||||
return b.desc
|
||||
}
|
||||
|
||||
// StorageKey holds the configuration for edge storage-key.
|
||||
type StorageKey struct {
|
||||
Table string // Table or label.
|
||||
Columns []string // Foreign-key columns.
|
||||
}
|
||||
|
||||
// StorageOption allows for setting the storage configuration using functional options.
|
||||
type StorageOption func(*StorageKey)
|
||||
|
||||
// The Table option sets the table name of M2M edges.
|
||||
func Table(name string) StorageOption {
|
||||
return func(key *StorageKey) {
|
||||
key.Table = name
|
||||
}
|
||||
}
|
||||
|
||||
// The Column option sets the foreign-key column name for O2O, O2M and M2O
|
||||
// edges. Note that, for M2M edges (2 columns), use the edge.Columns option.
|
||||
func Column(name string) StorageOption {
|
||||
return func(key *StorageKey) {
|
||||
key.Columns = []string{name}
|
||||
}
|
||||
}
|
||||
|
||||
// The Columns option sets the foreign-key column names for M2M edges.
|
||||
// The 1st column defines the name of the "To" edge, and the 2nd defines
|
||||
// the name of the "From" edge (inverse edge).
|
||||
// Note that, for O2O, O2M and M2O edges, use the edge.Column option.
|
||||
func Columns(to, from string) StorageOption {
|
||||
return func(key *StorageKey) {
|
||||
key.Columns = []string{to, from}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,9 +76,11 @@ func TestEdge(t *testing.T) {
|
||||
|
||||
from = edge.To("following", User.Type).
|
||||
StructTag("following").
|
||||
StorageKey(edge.Table("user_followers"), edge.Columns("following_id", "followers_id")).
|
||||
From("followers").
|
||||
StructTag("followers").
|
||||
Descriptor()
|
||||
assert.Equal("followers", from.Tag)
|
||||
assert.Equal("following", from.Ref.Tag)
|
||||
assert.Equal(edge.StorageKey{Table: "user_followers", Columns: []string{"following_id", "followers_id"}}, *from.Ref.StorageKey)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user