1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-19 22:19:23 +02:00
pocketbase/core/record_query_expand.go

283 lines
7.6 KiB
Go
Raw Normal View History

2024-09-29 19:23:19 +03:00
package core
2022-07-07 00:19:05 +03:00
import (
"errors"
2022-07-07 00:19:05 +03:00
"fmt"
"log"
2022-10-30 10:28:14 +02:00
"regexp"
2022-07-07 00:19:05 +03:00
"strings"
2022-10-30 10:28:14 +02:00
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/tools/dbutils"
2022-07-07 00:19:05 +03:00
"github.com/pocketbase/pocketbase/tools/list"
)
// ExpandFetchFunc defines the function that is used to fetch the expanded relation records.
2024-09-29 19:23:19 +03:00
type ExpandFetchFunc func(relCollection *Collection, relIds []string) ([]*Record, error)
2022-07-07 00:19:05 +03:00
// ExpandRecord expands the relations of a single Record model.
//
// If optFetchFunc is not set, then a default function will be used
// that returns all relation records.
//
// Returns a map with the failed expand parameters and their errors.
2024-09-29 19:23:19 +03:00
func (app *BaseApp) ExpandRecord(record *Record, expands []string, optFetchFunc ExpandFetchFunc) map[string]error {
return app.ExpandRecords([]*Record{record}, expands, optFetchFunc)
2022-07-07 00:19:05 +03:00
}
// ExpandRecords expands the relations of the provided Record models list.
//
// If optFetchFunc is not set, then a default function will be used
// that returns all relation records.
//
// Returns a map with the failed expand parameters and their errors.
2024-09-29 19:23:19 +03:00
func (app *BaseApp) ExpandRecords(records []*Record, expands []string, optFetchFunc ExpandFetchFunc) map[string]error {
2022-07-07 00:19:05 +03:00
normalized := normalizeExpands(expands)
failed := map[string]error{}
2022-07-07 00:19:05 +03:00
for _, expand := range normalized {
2024-09-29 19:23:19 +03:00
if err := app.expandRecords(records, expand, optFetchFunc, 1); err != nil {
failed[expand] = err
2022-07-07 00:19:05 +03:00
}
}
return failed
2022-07-07 00:19:05 +03:00
}
// Deprecated
var indirectExpandRegexOld = regexp.MustCompile(`^(\w+)\((\w+)\)$`)
var indirectExpandRegex = regexp.MustCompile(`^(\w+)_via_(\w+)$`)
2022-10-30 10:28:14 +02:00
2022-07-07 00:19:05 +03:00
// notes:
// - if fetchFunc is nil, dao.FindRecordsByIds will be used
2022-07-07 00:19:05 +03:00
// - all records are expected to be from the same collection
2024-09-29 19:23:19 +03:00
// - if maxNestedRels(6) is reached, the function returns nil ignoring the remaining expand path
func (app *BaseApp) expandRecords(records []*Record, expandPath string, fetchFunc ExpandFetchFunc, recursionLevel int) error {
2022-07-07 00:19:05 +03:00
if fetchFunc == nil {
// load a default fetchFunc
2024-09-29 19:23:19 +03:00
fetchFunc = func(relCollection *Collection, relIds []string) ([]*Record, error) {
return app.FindRecordsByIds(relCollection.Id, relIds)
}
2022-07-07 00:19:05 +03:00
}
2024-09-29 19:23:19 +03:00
if expandPath == "" || recursionLevel > maxNestedRels || len(records) == 0 {
2022-07-07 00:19:05 +03:00
return nil
}
2022-10-30 10:28:14 +02:00
mainCollection := records[0].Collection()
2024-09-29 19:23:19 +03:00
var relField *RelationField
var relCollection *Collection
2022-10-30 10:28:14 +02:00
2022-07-07 00:19:05 +03:00
parts := strings.SplitN(expandPath, ".", 2)
var matches []string
// @todo remove the old syntax support
if strings.Contains(parts[0], "(") {
matches = indirectExpandRegexOld.FindStringSubmatch(parts[0])
if len(matches) == 3 {
log.Printf(
"%s expand format is deprecated and will be removed in the future. Consider replacing it with %s_via_%s.\n",
matches[0],
matches[1],
matches[2],
)
}
} else {
matches = indirectExpandRegex.FindStringSubmatch(parts[0])
}
2022-07-07 00:19:05 +03:00
2022-10-30 10:28:14 +02:00
if len(matches) == 3 {
2024-09-29 19:23:19 +03:00
indirectRel, _ := getCollectionByModelOrIdentifier(app, matches[1])
2022-10-30 10:28:14 +02:00
if indirectRel == nil {
2024-02-25 21:06:43 +02:00
return fmt.Errorf("couldn't find back-related collection %q", matches[1])
2022-10-30 10:28:14 +02:00
}
2024-09-29 19:23:19 +03:00
indirectRelField, _ := indirectRel.Fields.GetByName(matches[2]).(*RelationField)
if indirectRelField == nil || indirectRelField.CollectionId != mainCollection.Id {
2024-02-25 21:06:43 +02:00
return fmt.Errorf("couldn't find back-relation field %q in collection %q", matches[2], indirectRel.Name)
2022-10-30 10:28:14 +02:00
}
// 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 {
2024-09-29 19:23:19 +03:00
q := app.DB().Select("id").
2024-02-25 21:06:43 +02:00
From(indirectRel.Name).
Limit(1000) // the limit is arbitrary chosen and may change in the future
2024-09-29 19:23:19 +03:00
if indirectRelField.IsMultiple() {
q.AndWhere(dbx.Exists(dbx.NewExp(fmt.Sprintf(
"SELECT 1 FROM %s je WHERE je.value = {:id}",
2024-09-29 19:23:19 +03:00
dbutils.JSONEach(indirectRelField.Name),
))))
} else {
q.AndWhere(dbx.NewExp("[[" + indirectRelField.Name + "]] = {:id}"))
2022-10-30 10:28:14 +02:00
}
pq := q.Build().Prepare()
for _, record := range records {
var relIds []string
err := pq.Bind(dbx.Params{"id": record.Id}).Column(&relIds)
if err != nil {
return errors.Join(err, pq.Close())
}
if len(relIds) > 0 {
record.Set(parts[0], relIds)
}
2022-10-30 10:28:14 +02:00
}
return pq.Close()
}()
if prepErr != nil {
return prepErr
2022-10-30 10:28:14 +02:00
}
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
// indirect/back relation
relField = &RelationField{
Name: parts[0],
MaxSelect: 2147483647,
2022-10-30 10:28:14 +02:00
CollectionId: indirectRel.Id,
}
2024-09-29 19:23:19 +03:00
if dbutils.HasSingleColumnUniqueIndex(indirectRelField.GetName(), indirectRel.Indexes) {
relField.MaxSelect = 1
2022-10-30 10:28:14 +02:00
}
relCollection = indirectRel
} else {
// direct relation
2024-09-29 19:23:19 +03:00
relField, _ = mainCollection.Fields.GetByName(parts[0]).(*RelationField)
if relField == nil {
return fmt.Errorf("couldn't find relation field %q in collection %q", parts[0], mainCollection.Name)
2022-10-30 10:28:14 +02:00
}
2024-09-29 19:23:19 +03:00
relCollection, _ = getCollectionByModelOrIdentifier(app, relField.CollectionId)
2022-10-30 10:28:14 +02:00
if relCollection == nil {
2024-09-29 19:23:19 +03:00
return fmt.Errorf("couldn't find related collection %q", relField.CollectionId)
2022-10-30 10:28:14 +02:00
}
2022-07-07 00:19:05 +03:00
}
2022-10-30 10:28:14 +02:00
// ---------------------------------------------------------------
2022-07-07 00:19:05 +03:00
// extract the id of the relations to expand
relIds := make([]string, 0, len(records))
2022-07-07 00:19:05 +03:00
for _, record := range records {
2022-10-30 10:28:14 +02:00
relIds = append(relIds, record.GetStringSlice(relField.Name)...)
2022-07-07 00:19:05 +03:00
}
// fetch rels
rels, relsErr := fetchFunc(relCollection, relIds)
if relsErr != nil {
return relsErr
}
// expand nested fields
if len(parts) > 1 {
2024-09-29 19:23:19 +03:00
err := app.expandRecords(rels, parts[1], fetchFunc, recursionLevel+1)
2022-07-07 00:19:05 +03:00
if err != nil {
return err
}
}
// reindex with the rel id
2024-09-29 19:23:19 +03:00
indexedRels := make(map[string]*Record, len(rels))
2022-07-07 00:19:05 +03:00
for _, rel := range rels {
2024-09-29 19:23:19 +03:00
indexedRels[rel.Id] = rel
2022-07-07 00:19:05 +03:00
}
for _, model := range records {
2024-09-29 19:23:19 +03:00
// init expand if not already
// (this is done to ensure that the "expand" key will be returned in the response even if empty)
if model.expand == nil {
model.SetExpand(nil)
}
2022-10-30 10:28:14 +02:00
relIds := model.GetStringSlice(relField.Name)
2022-07-07 00:19:05 +03:00
2024-09-29 19:23:19 +03:00
validRels := make([]*Record, 0, len(relIds))
2022-07-07 00:19:05 +03:00
for _, id := range relIds {
if rel, ok := indexedRels[id]; ok {
validRels = append(validRels, rel)
}
}
if len(validRels) == 0 {
continue // no valid relations
}
2022-10-30 10:28:14 +02:00
expandData := model.Expand()
2022-07-07 00:19:05 +03:00
// normalize access to the previously expanded rel records (if any)
2024-09-29 19:23:19 +03:00
var oldExpandedRels []*Record
switch v := expandData[relField.Name].(type) {
case nil:
// no old expands
2024-09-29 19:23:19 +03:00
case *Record:
oldExpandedRels = []*Record{v}
case []*Record:
oldExpandedRels = v
}
// merge expands
for _, oldExpandedRel := range oldExpandedRels {
// find a matching rel record
for _, rel := range validRels {
if rel.Id != oldExpandedRel.Id {
continue
}
rel.MergeExpand(oldExpandedRel.Expand())
}
}
// update the expanded data
2024-09-29 19:23:19 +03:00
if relField.IsMultiple() {
2022-07-07 00:19:05 +03:00
expandData[relField.Name] = validRels
2024-09-29 19:23:19 +03:00
} else {
expandData[relField.Name] = validRels[0]
2022-07-07 00:19:05 +03:00
}
2022-07-07 00:19:05 +03:00
model.SetExpand(expandData)
}
return nil
}
// normalizeExpands normalizes expand strings and merges self containing paths
// (eg. ["a.b.c", "a.b", " test ", " ", "test"] -> ["a.b.c", "test"]).
func normalizeExpands(paths []string) []string {
// normalize paths
normalized := make([]string, 0, len(paths))
2022-07-07 00:19:05 +03:00
for _, p := range paths {
p = strings.ReplaceAll(p, " ", "") // replace spaces
p = strings.Trim(p, ".") // trim incomplete paths
if p != "" {
normalized = append(normalized, p)
2022-07-07 00:19:05 +03:00
}
}
// merge containing paths
result := make([]string, 0, len(normalized))
2022-07-07 00:19:05 +03:00
for i, p1 := range normalized {
var skip bool
for j, p2 := range normalized {
if i == j {
continue
}
if strings.HasPrefix(p2, p1+".") {
// skip because there is more detailed expand path
skip = true
break
}
}
if !skip {
result = append(result, p1)
}
}
return list.ToUniqueStringSlice(result)
}