schema/edge: add support for configuring foreign-key symbols

Fixed #1423
This commit is contained in:
Ariel Mashraki
2021-04-04 14:42:21 +03:00
committed by Ariel Mashraki
parent 745afde770
commit f3f03e1edd
8 changed files with 79 additions and 14 deletions

View File

@@ -194,6 +194,7 @@ func (b *inverseBuilder) Descriptor() *Descriptor {
// StorageKey holds the configuration for edge storage-key.
type StorageKey struct {
Table string // Table or label.
Symbols []string // Symbols/names of the foreign-key constraints.
Columns []string // Foreign-key columns.
}
@@ -207,6 +208,24 @@ func Table(name string) StorageOption {
}
}
// Symbol sets the symbol/name of the foreign-key constraint for O2O, O2M and M2O edges.
// Note that, for M2M edges (2 columns and 2 constraints), use the edge.Symbols option.
func Symbol(symbol string) StorageOption {
return func(key *StorageKey) {
key.Symbols = []string{symbol}
}
}
// Symbols sets the symbol/name of the foreign-key constraints 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.Symbol option.
func Symbols(to, from string) StorageOption {
return func(key *StorageKey) {
key.Symbols = []string{to, from}
}
}
// Column sets the foreign-key column name option for O2O, O2M and M2O edges.
// Note that, for M2M edges (2 columns), use the edge.Columns option.
func Column(name string) StorageOption {

View File

@@ -86,13 +86,13 @@ func TestEdge(t *testing.T) {
from = edge.To("following", User.Type).
StructTag("following").
StorageKey(edge.Table("user_followers"), edge.Columns("following_id", "followers_id")).
StorageKey(edge.Table("user_followers"), edge.Columns("following_id", "followers_id"), edge.Symbol("users_followers")).
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)
assert.Equal(edge.StorageKey{Table: "user_followers", Symbols: []string{"users_followers"}, Columns: []string{"following_id", "followers_id"}}, *from.Ref.StorageKey)
}
type GQL struct {