First working version
This commit is contained in:
38
internal/generator/load_options.go
Normal file
38
internal/generator/load_options.go
Normal file
@ -0,0 +1,38 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user