1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2024-11-21 13:35:49 +02:00

added back-relation expand limit

This commit is contained in:
Gani Georgiev 2024-02-25 21:06:43 +02:00
parent 1014c92d86
commit 548fce20b5

View File

@ -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,