support group edges via scaffold

This commit is contained in:
Hayden 2023-03-05 21:56:53 -09:00
parent 9fa4da819f
commit 737007b156
No known key found for this signature in database
GPG key ID: 17CF79474E257545
4 changed files with 57 additions and 1 deletions

View file

@ -14,6 +14,20 @@ questions:
message: "What is the name of the model? (PascalCase)"
required: true
- name: "by_group"
prompt:
confirm: "Include a Group Edge? (group_id -> id)"
required: true
rewrites:
- from: 'templates/model.go'
to: 'backend/internal/data/ent/schema/{{ lower .Scaffold.model }}.go'
to: 'backend/internal/data/ent/schema/{{ lower .Scaffold.model }}.go'
inject:
- name: "Insert Groups Edge"
path: 'backend/internal/data/ent/schema/group.go'
at: // $scaffold_edge
template: |
{{- if .Scaffold.by_group -}}
owned("{{ lower .Scaffold.model }}s", {{ .Scaffold.model }}.Type),
{{- end -}}

View file

@ -13,6 +13,9 @@ type {{ .Scaffold.model }} struct {
func ({{ .Scaffold.model }}) Mixin() []ent.Mixin {
return []ent.Mixin{
mixins.BaseMixin{},
{{- if .Scaffold.by_group }}
GroupMixin{ref: "{{ snakecase .Scaffold.model }}s"},
{{- end }}
}
}

View file

@ -49,6 +49,7 @@ func (Group) Edges() []ent.Edge {
owned("documents", Document.Type),
owned("invitation_tokens", GroupInvitationToken.Type),
owned("notifiers", Notifier.Type),
// $scaffold_edge
}
}

View file

@ -0,0 +1,38 @@
package schema
import (
"entgo.io/ent"
"github.com/hay-kot/homebox/backend/internal/data/ent/schema/mixins"
)
type Test struct {
ent.Schema
}
func (Test) Mixin() []ent.Mixin {
return []ent.Mixin{
mixins.BaseMixin{},
GroupMixin{ref: "tests"},
}
}
// Fields of the Test.
func (Test) Fields() []ent.Field {
return []ent.Field{
// field.String("name").
}
}
// Edges of the Test.
func (Test) Edges() []ent.Edge {
return []ent.Edge{
// edge.From("group", Group.Type).
}
}
func (Test) Indexes() []ent.Index {
return []ent.Index{
// index.Fields("token"),
}
}