From 548fce20b5d91c27ef270328c3f15979b3e9b9aa Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Sun, 25 Feb 2024 21:06:43 +0200 Subject: [PATCH] added back-relation expand limit --- daos/record_expand.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/daos/record_expand.go b/daos/record_expand.go index 4a23528a..857b5eec 100644 --- a/daos/record_expand.go +++ b/daos/record_expand.go @@ -102,24 +102,26 @@ func (dao *Dao) expandRecords(records []*models.Record, expandPath string, fetch if len(matches) == 3 { indirectRel, _ := dao.FindCollectionByNameOrId(matches[1]) if indirectRel == nil { - return fmt.Errorf("Couldn't find indirect related collection %q.", matches[1]) + return fmt.Errorf("couldn't find back-related collection %q", matches[1]) } indirectRelField := indirectRel.Schema.GetFieldByName(matches[2]) if indirectRelField == nil || indirectRelField.Type != schema.FieldTypeRelation { - return fmt.Errorf("Couldn't find indirect relation field %q in collection %q.", matches[2], mainCollection.Name) + return fmt.Errorf("couldn't find back-relation field %q in collection %q", matches[2], indirectRel.Name) } indirectRelField.InitOptions() indirectRelFieldOptions, _ := indirectRelField.Options.(*schema.RelationOptions) if indirectRelFieldOptions == nil || indirectRelFieldOptions.CollectionId != mainCollection.Id { - return fmt.Errorf("Invalid indirect relation field path %q.", parts[0]) + return fmt.Errorf("invalid back-relation field path %q", parts[0]) } // add the related id(s) as a dynamic relation field value to // allow further expand checks at later stage in a more unified manner prepErr := func() error { - q := dao.DB().Select("id").From(indirectRel.Name) + q := dao.DB().Select("id"). + From(indirectRel.Name). + Limit(1000) // the limit is arbitrary chosen and may change in the future if indirectRelFieldOptions.IsMultiple() { q.AndWhere(dbx.Exists(dbx.NewExp(fmt.Sprintf( @@ -158,9 +160,9 @@ func (dao *Dao) expandRecords(records []*models.Record, expandPath string, fetch if dbutils.HasSingleColumnUniqueIndex(indirectRelField.Name, indirectRel.Indexes) { relFieldOptions.MaxSelect = types.Pointer(1) } - // indirect relation + // indirect/back relation relField = &schema.SchemaField{ - Id: "indirect_" + security.PseudorandomString(5), + Id: "_" + parts[0] + security.PseudorandomString(3), Type: schema.FieldTypeRelation, Name: parts[0], Options: relFieldOptions,