mirror of
https://github.com/bitechdev/ResolveSpec.git
synced 2026-01-09 13:04:24 +00:00
Better preloads
This commit is contained in:
@@ -560,11 +560,33 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
|
||||
|
||||
// applyPreloadWithRecursion applies a preload with support for ComputedQL and recursive preloading
|
||||
func (h *Handler) applyPreloadWithRecursion(query common.SelectQuery, preload common.PreloadOption, model interface{}, depth int) common.SelectQuery {
|
||||
// Log relationship keys if they're specified (from XFiles)
|
||||
if preload.RelatedKey != "" || preload.ForeignKey != "" || preload.PrimaryKey != "" {
|
||||
logger.Debug("Preload %s has relationship keys - PK: %s, RelatedKey: %s, ForeignKey: %s",
|
||||
preload.Relation, preload.PrimaryKey, preload.RelatedKey, preload.ForeignKey)
|
||||
|
||||
// Build a WHERE clause using the relationship keys if needed
|
||||
// Note: Bun's PreloadRelation typically handles the relationship join automatically via struct tags
|
||||
// However, if the relationship keys are explicitly provided from XFiles, we can use them
|
||||
// to add additional filtering or validation
|
||||
if preload.RelatedKey != "" && preload.Where == "" {
|
||||
// For child tables: ensure the child's relatedkey column will be matched
|
||||
// The actual parent value is dynamic and handled by Bun's preload mechanism
|
||||
// We just log this for visibility
|
||||
logger.Debug("Child table %s will be filtered by %s matching parent's primary key",
|
||||
preload.Relation, preload.RelatedKey)
|
||||
}
|
||||
if preload.ForeignKey != "" && preload.Where == "" {
|
||||
// For parent tables: ensure the parent's primary key matches the current table's foreign key
|
||||
logger.Debug("Parent table %s will be filtered by primary key matching current table's %s",
|
||||
preload.Relation, preload.ForeignKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the preload
|
||||
query = query.PreloadRelation(preload.Relation, func(sq common.SelectQuery) common.SelectQuery {
|
||||
// Get the related model for column operations
|
||||
relationParts := strings.Split(preload.Relation, ",")
|
||||
relatedModel := reflection.GetRelationModel(model, relationParts[0])
|
||||
relatedModel := reflection.GetRelationModel(model, preload.Relation)
|
||||
if relatedModel == nil {
|
||||
logger.Warn("Could not get related model for preload: %s", preload.Relation)
|
||||
// relatedModel = model // fallback to parent model
|
||||
|
||||
@@ -919,6 +919,20 @@ func (h *Handler) addXFilesPreload(xfile *XFiles, options *ExtendedRequestOption
|
||||
// Set recursive flag
|
||||
preloadOpt.Recursive = xfile.Recursive
|
||||
|
||||
// Extract relationship keys for proper foreign key filtering
|
||||
if xfile.PrimaryKey != "" {
|
||||
preloadOpt.PrimaryKey = xfile.PrimaryKey
|
||||
logger.Debug("X-Files: Set primary key for %s: %s", relationPath, xfile.PrimaryKey)
|
||||
}
|
||||
if xfile.RelatedKey != "" {
|
||||
preloadOpt.RelatedKey = xfile.RelatedKey
|
||||
logger.Debug("X-Files: Set related key for %s: %s", relationPath, xfile.RelatedKey)
|
||||
}
|
||||
if xfile.ForeignKey != "" {
|
||||
preloadOpt.ForeignKey = xfile.ForeignKey
|
||||
logger.Debug("X-Files: Set foreign key for %s: %s", relationPath, xfile.ForeignKey)
|
||||
}
|
||||
|
||||
// Add the preload option
|
||||
options.Preload = append(options.Preload, preloadOpt)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user