mirror of
https://github.com/hay-kot/homebox.git
synced 2025-07-26 20:40:26 +00:00
refactor schema edges
This commit is contained in:
parent
ccd40ffcac
commit
883468e04c
12 changed files with 499 additions and 460 deletions
|
@ -6,6 +6,7 @@ import (
|
|||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/ent/schema/mixins"
|
||||
)
|
||||
|
||||
|
@ -56,15 +57,31 @@ func (Group) Edges() []ent.Edge {
|
|||
// GroupMixin when embedded in an ent.Schema, adds a reference to
|
||||
// the Group entity.
|
||||
type GroupMixin struct {
|
||||
ref string
|
||||
ref string
|
||||
field string
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (g GroupMixin) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("group", Group.Type).
|
||||
Ref(g.ref).
|
||||
Unique().
|
||||
Required(),
|
||||
func (g GroupMixin) Fields() []ent.Field {
|
||||
if g.field != "" {
|
||||
return []ent.Field{
|
||||
field.UUID(g.field, uuid.UUID{}),
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (g GroupMixin) Edges() []ent.Edge {
|
||||
edge := edge.From("group", Group.Type).
|
||||
Ref(g.ref).
|
||||
Unique().
|
||||
Required()
|
||||
|
||||
if g.field != "" {
|
||||
edge = edge.Field(g.field)
|
||||
}
|
||||
|
||||
return []ent.Edge{edge}
|
||||
}
|
||||
|
|
|
@ -2,11 +2,9 @@ package schema
|
|||
|
||||
import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/index"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/ent/schema/mixins"
|
||||
)
|
||||
|
||||
|
@ -17,14 +15,20 @@ type Notifier struct {
|
|||
func (Notifier) Mixin() []ent.Mixin {
|
||||
return []ent.Mixin{
|
||||
mixins.BaseMixin{},
|
||||
GroupMixin{
|
||||
ref: "notifiers",
|
||||
field: "group_id",
|
||||
},
|
||||
UserMixin{
|
||||
ref: "notifiers",
|
||||
field: "user_id",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Fields of the Notifier.
|
||||
func (Notifier) Fields() []ent.Field {
|
||||
return []ent.Field{
|
||||
field.UUID("user_id", uuid.UUID{}),
|
||||
field.UUID("group_id", uuid.UUID{}),
|
||||
field.String("name").
|
||||
MaxLen(255).
|
||||
NotEmpty(),
|
||||
|
@ -37,22 +41,6 @@ func (Notifier) Fields() []ent.Field {
|
|||
}
|
||||
}
|
||||
|
||||
// Edges of the Notifier.
|
||||
func (Notifier) Edges() []ent.Edge {
|
||||
return []ent.Edge{
|
||||
edge.From("user", User.Type).
|
||||
Field("user_id").
|
||||
Ref("notifiers").
|
||||
Required().
|
||||
Unique(),
|
||||
edge.From("group", Group.Type).
|
||||
Field("group_id").
|
||||
Ref("notifiers").
|
||||
Required().
|
||||
Unique(),
|
||||
}
|
||||
}
|
||||
|
||||
func (Notifier) Indexes() []ent.Index {
|
||||
return []ent.Index{
|
||||
index.Fields("user_id"),
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
"entgo.io/ent/dialect/entsql"
|
||||
"entgo.io/ent/schema/edge"
|
||||
"entgo.io/ent/schema/field"
|
||||
"entgo.io/ent/schema/mixin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/hay-kot/homebox/backend/internal/data/ent/schema/mixins"
|
||||
)
|
||||
|
||||
|
@ -59,3 +61,35 @@ func (User) Edges() []ent.Edge {
|
|||
}),
|
||||
}
|
||||
}
|
||||
|
||||
// UserMixin when embedded in an ent.Schema, adds a reference to
|
||||
// the Group entity.
|
||||
type UserMixin struct {
|
||||
ref string
|
||||
field string
|
||||
mixin.Schema
|
||||
}
|
||||
|
||||
func (g UserMixin) Fields() []ent.Field {
|
||||
if g.field != "" {
|
||||
return []ent.Field{
|
||||
field.UUID(g.field, uuid.UUID{}),
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (g UserMixin) Edges() []ent.Edge {
|
||||
edge := edge.From("user", User.Type).
|
||||
Ref(g.ref).
|
||||
Unique().
|
||||
Required()
|
||||
|
||||
if g.field != "" {
|
||||
edge = edge.Field(g.field)
|
||||
}
|
||||
|
||||
return []ent.Edge{edge}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue