39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
package generator
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// GenerateLoadOptions generates the load options class
|
|
func GenerateLoadOptions(ctx *Context) (string, error) {
|
|
var b strings.Builder
|
|
|
|
// Imports
|
|
b.WriteString("from televend_core.databases.base_load_options import LoadOptions\n")
|
|
b.WriteString("from televend_core.databases.common.load_options import joinload\n")
|
|
b.WriteString(fmt.Sprintf("from televend_core.databases.televend_repositories.%s.model import %s\n",
|
|
ctx.ModuleName, ctx.EntityName))
|
|
b.WriteString("\n\n")
|
|
|
|
// Class definition
|
|
b.WriteString(fmt.Sprintf("class %sLoadOptions(LoadOptions):\n", ctx.EntityName))
|
|
b.WriteString(fmt.Sprintf(" model_cls = %s\n\n", ctx.EntityName))
|
|
|
|
// Generate load options for all foreign key relationships
|
|
hasRelationships := false
|
|
for _, fk := range ctx.TableInfo.ForeignKeys {
|
|
hasRelationships = true
|
|
relationName := GetRelationshipName(fk.ColumnName)
|
|
b.WriteString(fmt.Sprintf(" load_%s: bool = joinload(relations=[\"%s\"])\n",
|
|
relationName, relationName))
|
|
}
|
|
|
|
// If no relationships, add pass
|
|
if !hasRelationships {
|
|
b.WriteString(" pass\n")
|
|
}
|
|
|
|
return b.String(), nil
|
|
}
|