refactor schema folder

This commit is contained in:
Hayden 2023-03-05 21:47:31 -09:00
parent 77fb37ebf4
commit 9fa4da819f
No known key found for this signature in database
GPG key ID: 17CF79474E257545
22 changed files with 723 additions and 735 deletions

View file

@ -16,6 +16,7 @@ type Document struct {
func (Document) Mixin() []ent.Mixin {
return []ent.Mixin{
mixins.BaseMixin{},
GroupMixin{ref: "documents"},
}
}
@ -34,10 +35,6 @@ func (Document) Fields() []ent.Field {
// Edges of the Document.
func (Document) Edges() []ent.Edge {
return []ent.Edge{
edge.From("group", Group.Type).
Ref("documents").
Required().
Unique(),
edge.To("attachments", Attachment.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,

View file

@ -5,6 +5,7 @@ import (
"entgo.io/ent/dialect/entsql"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
"github.com/hay-kot/homebox/backend/internal/data/ent/schema/mixins"
)
@ -33,34 +34,36 @@ func (Group) Fields() []ent.Field {
// Edges of the Home.
func (Group) Edges() []ent.Edge {
owned := func(name string, t any) ent.Edge {
return edge.To(name, t).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
})
}
return []ent.Edge{
edge.To("users", User.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
}),
edge.To("locations", Location.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
}),
edge.To("items", Item.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
}),
edge.To("labels", Label.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
}),
edge.To("documents", Document.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
}),
edge.To("invitation_tokens", GroupInvitationToken.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
}),
edge.To("notifiers", Notifier.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
}),
owned("users", User.Type),
owned("locations", Location.Type),
owned("items", Item.Type),
owned("labels", Label.Type),
owned("documents", Document.Type),
owned("invitation_tokens", GroupInvitationToken.Type),
owned("notifiers", Notifier.Type),
}
}
// GroupMixin when embedded in an ent.Schema, adds a reference to
// the Group entity.
type GroupMixin struct {
ref string
mixin.Schema
}
func (g GroupMixin) Edges() []ent.Edge {
return []ent.Edge{
edge.From("group", Group.Type).
Ref(g.ref).
Unique().
Required(),
}
}

View file

@ -0,0 +1 @@
package schema

View file

@ -18,6 +18,7 @@ func (Item) Mixin() []ent.Mixin {
return []ent.Mixin{
mixins.BaseMixin{},
mixins.DetailsMixin{},
GroupMixin{ref: "items"},
}
}
@ -102,10 +103,6 @@ func (Item) Edges() []ent.Edge {
edge.To("children", Item.Type).
From("parent").
Unique(),
edge.From("group", Group.Type).
Ref("items").
Required().
Unique(),
edge.From("label", Label.Type).
Ref("items"),
edge.From("location", Location.Type).

View file

@ -16,6 +16,7 @@ func (Label) Mixin() []ent.Mixin {
return []ent.Mixin{
mixins.BaseMixin{},
mixins.DetailsMixin{},
GroupMixin{ref: "labels"},
}
}
@ -31,10 +32,6 @@ func (Label) Fields() []ent.Field {
// Edges of the Label.
func (Label) Edges() []ent.Edge {
return []ent.Edge{
edge.From("group", Group.Type).
Ref("labels").
Required().
Unique(),
edge.To("items", Item.Type),
}
}

View file

@ -16,6 +16,7 @@ func (Location) Mixin() []ent.Mixin {
return []ent.Mixin{
mixins.BaseMixin{},
mixins.DetailsMixin{},
GroupMixin{ref: "locations"},
}
}
@ -30,10 +31,6 @@ func (Location) Edges() []ent.Edge {
edge.To("children", Location.Type).
From("parent").
Unique(),
edge.From("group", Group.Type).
Ref("locations").
Unique().
Required(),
edge.To("items", Item.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,

View file

@ -57,7 +57,6 @@ func (Notifier) Indexes() []ent.Index {
return []ent.Index{
index.Fields("user_id"),
index.Fields("user_id", "is_active"),
index.Fields("group_id"),
index.Fields("group_id", "is_active"),
}

View file

@ -16,6 +16,7 @@ type User struct {
func (User) Mixin() []ent.Mixin {
return []ent.Mixin{
mixins.BaseMixin{},
GroupMixin{ref: "users"},
}
}
@ -48,10 +49,6 @@ func (User) Fields() []ent.Field {
// Edges of the User.
func (User) Edges() []ent.Edge {
return []ent.Edge{
edge.From("group", Group.Type).
Ref("users").
Required().
Unique(),
edge.To("auth_tokens", AuthTokens.Type).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,