add custom js functions for templates

This commit is contained in:
2025-09-09 22:56:39 -06:00
parent b82e22c38d
commit a899fd6c4d
11 changed files with 558 additions and 55 deletions

View File

@ -0,0 +1,44 @@
package models
import (
"errors"
"time"
"github.com/google/uuid"
)
{{formatComment (printf "Model for %s entity\nGenerated with custom JavaScript functions" .Entity.Name)}}
type {{.Entity.Name | title}} struct {
{{- range .Entity.Fields}}
{{.Name | title}} {{goType .Type}} {{dbFieldMapping .Name .Type}}
{{- end}}
}
{{formatComment "Table name for GORM"}}
func ({{.Entity.Name | title}}) TableName() string {
return "{{pluralize (.Entity.Name | lower)}}"
}
{{formatComment "Validation function using custom JavaScript validation rules"}}
func (m *{{.Entity.Name | title}}) Validate() error {
{{- range .Entity.Fields}}
{{- if .Required}}
{{generateValidation .Type .Name}}
{{- end}}
{{- end}}
return nil
}
{{formatComment "Create a new instance with validation"}}
func New{{.Entity.Name | title}}({{range $i, $field := .Entity.Fields}}{{if $i}}, {{end}}{{$field.Name | lower}} {{goType $field.Type}}{{end}}) (*{{.Entity.Name | title}}, error) {
model := &{{.Entity.Name | title}}{
{{- range .Entity.Fields}}
{{.Name | title}}: {{.Name | lower}},
{{- end}}
}
if err := model.Validate(); err != nil {
return nil, err
}
return model, nil
}