Make relationships only return query objects.
This commit is contained in:
parent
79520d27fb
commit
c04e0d003a
4 changed files with 10 additions and 50 deletions
|
@ -1,31 +1,12 @@
|
|||
{{- define "relationship_to_one_helper"}}
|
||||
{{- $varNameSingular := .ForeignKey.ForeignTable | singular | camelCase -}}
|
||||
// {{.Function.Name}}G pointed to by the foreign key.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}G(mods ...qm.QueryMod) (*{{.ForeignTable.NameGo}}, error) {
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query {
|
||||
return {{.Function.Receiver}}.{{.Function.Name}}(boil.GetDB(), mods...)
|
||||
}
|
||||
|
||||
// {{.Function.Name}}GP pointed to by the foreign key. Panics on error.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}GP(mods ...qm.QueryMod) *{{.ForeignTable.NameGo}} {
|
||||
slice, err := {{.Function.Receiver}}.{{.Function.Name}}(boil.GetDB(), mods...)
|
||||
if err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
|
||||
return slice
|
||||
}
|
||||
|
||||
// {{.Function.Name}}P pointed to by the foreign key with exeuctor. Panics on error.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}P(exec boil.Executor, mods ...qm.QueryMod) *{{.ForeignTable.NameGo}} {
|
||||
slice, err := {{.Function.Receiver}}.{{.Function.Name}}(exec, mods...)
|
||||
if err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
|
||||
return slice
|
||||
}
|
||||
|
||||
// {{.Function.Name}} pointed to by the foreign key.
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) (*{{.ForeignTable.NameGo}}, error) {
|
||||
func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) ({{$varNameSingular}}Query) {
|
||||
queryMods := []qm.QueryMod{
|
||||
qm.Where("{{.ForeignTable.ColumnName}}=$1", {{.Function.Receiver}}.{{.LocalTable.ColumnNameGo}}),
|
||||
}
|
||||
|
@ -35,7 +16,7 @@ func ({{.Function.Receiver}} *{{.LocalTable.NameGo}}) {{.Function.Name}}(exec bo
|
|||
query := {{.ForeignTable.NamePluralGo}}(exec, queryMods...)
|
||||
boil.SetFrom(query.Query, "{{.ForeignTable.Name}}")
|
||||
|
||||
return query.One()
|
||||
return query
|
||||
}
|
||||
|
||||
{{end -}}
|
||||
|
|
|
@ -3,41 +3,20 @@
|
|||
{{- $dot := . -}}
|
||||
{{- $table := .Table -}}
|
||||
{{- range .Table.ToManyRelationships -}}
|
||||
{{- $varNameSingular := .ForeignTable | singular | camelCase -}}
|
||||
{{- if (and .ForeignColumnUnique (not .ToJoinTable)) -}}
|
||||
{{- template "relationship_to_one_helper" (textsFromOneToOneRelationship $dot.PkgName $dot.Tables $table .) -}}
|
||||
{{- else -}}
|
||||
{{- $rel := textsFromRelationship $dot.Tables $table . -}}
|
||||
// {{$rel.Function.Name}}G retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}}
|
||||
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
|
||||
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}G(mods ...qm.QueryMod) ({{$rel.ForeignTable.Slice}}, error) {
|
||||
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}G(mods ...qm.QueryMod) {{$varNameSingular}}Query {
|
||||
return {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(boil.GetDB(), mods...)
|
||||
}
|
||||
|
||||
// {{$rel.Function.Name}}GP panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}}
|
||||
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
|
||||
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}GP(mods ...qm.QueryMod) {{$rel.ForeignTable.Slice}} {
|
||||
slice, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(boil.GetDB(), mods...)
|
||||
if err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
|
||||
return slice
|
||||
}
|
||||
|
||||
// {{$rel.Function.Name}}P panics on error. Retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
|
||||
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
|
||||
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}P(exec boil.Executor, mods ...qm.QueryMod) {{$rel.ForeignTable.Slice}} {
|
||||
slice, err := {{$rel.Function.Receiver}}.{{$rel.Function.Name}}(exec, mods...)
|
||||
if err != nil {
|
||||
panic(boil.WrapErr(err))
|
||||
}
|
||||
|
||||
return slice
|
||||
}
|
||||
|
||||
// {{$rel.Function.Name}} retrieves all the {{$rel.LocalTable.NameSingular}}'s {{$rel.ForeignTable.NameHumanReadable}} with an executor
|
||||
{{- if not (eq $rel.Function.Name $rel.ForeignTable.NamePluralGo)}} via {{.ForeignColumn}} column{{- end}}.
|
||||
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) ({{$rel.ForeignTable.Slice}}, error) {
|
||||
func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Name}}(exec boil.Executor, mods ...qm.QueryMod) {{$varNameSingular}}Query {
|
||||
queryMods := []qm.QueryMod{
|
||||
qm.Select(`"{{id 0}}".*`),
|
||||
}
|
||||
|
@ -59,7 +38,7 @@ func ({{$rel.Function.Receiver}} *{{$rel.LocalTable.NameGo}}) {{$rel.Function.Na
|
|||
|
||||
query := {{$rel.ForeignTable.NamePluralGo}}(exec, queryMods...)
|
||||
boil.SetFrom(query.Query, `"{{.ForeignTable}}" as "{{id 0}}"`)
|
||||
return query.All()
|
||||
return query
|
||||
}
|
||||
|
||||
{{end -}}{{- /* if unique foreign key */ -}}
|
||||
|
|
|
@ -52,7 +52,7 @@ func test{{$rel.LocalTable.NameGo}}ToMany{{$rel.Function.Name}}(t *testing.T) {
|
|||
{{end}}
|
||||
|
||||
{{$varname := .ForeignTable | singular | camelCase -}}
|
||||
{{$varname}}, err := a.{{$rel.Function.Name}}(tx)
|
||||
{{$varname}}, err := a.{{$rel.Function.Name}}(tx).All()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ func test{{.LocalTable.NameGo}}ToOne{{.ForeignTable.NameGo}}_{{.Function.Name}}(
|
|||
}
|
||||
{{end -}}
|
||||
|
||||
check, err := local.{{.Function.Name}}(tx)
|
||||
check, err := local.{{.Function.Name}}(tx).One()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue