-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
79 lines (71 loc) · 2.02 KB
/
template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package model
const Template = `//nolint
//lint:file-ignore U1000 ignore unused code, it's generated
package {{.Package}}{{if .HasImports}}
import ({{range .Imports}}
"{{.}}"{{end}}
){{end}}
var Columns = struct { {{range .Entities}}
{{.GoName}} struct{
{{range $i, $e := .Columns}}{{if $i}}, {{end}}{{.GoName}}{{end}} string{{if .HasRelations}}
{{range $i, $e := .Relations}}{{if $i}}, {{end}}{{.GoName}}{{end}} string{{end}}
}{{end}}
}{ {{range .Entities}}
{{.GoName}}: struct {
{{range $i, $e := .Columns}}{{if $i}}, {{end}}{{.GoName}}{{end}} string{{if .HasRelations}}
{{range $i, $e := .Relations}}{{if $i}}, {{end}}{{.GoName}}{{end}} string{{end}}
}{ {{range .Columns}}
{{.GoName}}: "{{.PGName}}",{{end}}{{if .HasRelations}}
{{range .Relations}}
{{.GoName}}: "{{.GoName}}",{{end}}{{end}}
},{{end}}
}
var Tables = struct { {{range .Entities}}
{{.GoName}} struct {
Name{{if not .NoAlias }}, Alias{{end}} string
}{{end}}
}{ {{range .Entities}}
{{.GoName}}: struct {
Name{{if not .NoAlias}}, Alias{{end}} string
}{
Name: "{{.PGFullName}}",{{if not .NoAlias}}
Alias: "{{.Alias}}",{{end}}
},{{end}}
}
{{range $model := .Entities}}
type {{.GoName}} struct {
bun.BaseModel {{.Tag}}
{{range .Columns}}
{{.GoName}} {{.Type}} {{.Tag}} {{.Comment}}{{end}}{{if .HasRelations}}
{{range .Relations}}
{{.GoName}} *{{.GoType}} {{.Tag}} {{.Comment}}{{end}}{{end}}
}
{{end}}
/* Common ORM queries */
// Just a wrapper around database connection
{{- if .ORMNeeded}}
type {{ .ORMDbStruct }} struct {
*bun.DB
}
{{$dbstruct := .}}
/* 'SELECT' queries */
{{- range $model := .Entities}}
func (dbConn *{{ $dbstruct.ORMDbStruct }}) Select{{ .GoName }}() ([]*{{ .GoName }}, error) {
ctx := context.Background()
model := []*{{ .GoName }}{}
{{$parent := .}}
err := dbConn.NewSelect().
{{- range .Columns}}
{{- if $parent.NoAlias }}
Column("{{ .Column.PGName -}}").
{{- else}}
Column("{{ $parent.Alias -}}.{{ .Column.PGName -}}").
{{- end}}
{{- end}}
Model(&model).
Scan(ctx)
return model, err
}
{{end}}
{{- end}}
`