load a default fetchFunc for dao.ExpandRecord(s)

This commit is contained in:
Gani Georgiev 2023-07-14 08:36:01 +03:00
parent fdccdcebad
commit 2b465b0646
3 changed files with 39 additions and 21 deletions

View File

@ -80,6 +80,8 @@
- (@todo docs) For consistency and convenience it is now possible to call `Dao.RecordQuery(collectionModelOrIdentifier)` with just the collection id or name. - (@todo docs) For consistency and convenience it is now possible to call `Dao.RecordQuery(collectionModelOrIdentifier)` with just the collection id or name.
In case an invalid collection id/name string is passed the query will be resolved with cancelled context error. In case an invalid collection id/name string is passed the query will be resolved with cancelled context error.
- (@todo docs) Use a default fetch function that will return all relations in case the fetchFunc argument of `Dao.ExpandRecord()` and `Dao.ExpandRecords()` is `nil`.
## v0.16.8 ## v0.16.8

View File

@ -1,7 +1,6 @@
package daos package daos
import ( import (
"errors"
"fmt" "fmt"
"regexp" "regexp"
"strings" "strings"
@ -24,6 +23,9 @@ type ExpandFetchFunc func(relCollection *models.Collection, relIds []string) ([]
// ExpandRecord expands the relations of a single Record model. // ExpandRecord expands the relations of a single Record model.
// //
// If fetchFunc 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. // Returns a map with the failed expand parameters and their errors.
func (dao *Dao) ExpandRecord(record *models.Record, expands []string, fetchFunc ExpandFetchFunc) map[string]error { func (dao *Dao) ExpandRecord(record *models.Record, expands []string, fetchFunc ExpandFetchFunc) map[string]error {
return dao.ExpandRecords([]*models.Record{record}, expands, fetchFunc) return dao.ExpandRecords([]*models.Record{record}, expands, fetchFunc)
@ -31,6 +33,9 @@ func (dao *Dao) ExpandRecord(record *models.Record, expands []string, fetchFunc
// ExpandRecords expands the relations of the provided Record models list. // ExpandRecords expands the relations of the provided Record models list.
// //
// If fetchFunc 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. // Returns a map with the failed expand parameters and their errors.
func (dao *Dao) ExpandRecords(records []*models.Record, expands []string, fetchFunc ExpandFetchFunc) map[string]error { func (dao *Dao) ExpandRecords(records []*models.Record, expands []string, fetchFunc ExpandFetchFunc) map[string]error {
normalized := normalizeExpands(expands) normalized := normalizeExpands(expands)
@ -49,13 +54,16 @@ func (dao *Dao) ExpandRecords(records []*models.Record, expands []string, fetchF
var indirectExpandRegex = regexp.MustCompile(`^(\w+)\((\w+)\)$`) var indirectExpandRegex = regexp.MustCompile(`^(\w+)\((\w+)\)$`)
// notes: // notes:
// - fetchFunc must be non-nil func // - if fetchFunc is nil, dao.FindRecordsByIds will be used
// - all records are expected to be from the same collection // - all records are expected to be from the same collection
// - if MaxExpandDepth is reached, the function returns nil ignoring the remaining expand path // - if MaxExpandDepth is reached, the function returns nil ignoring the remaining expand path
// - indirect expands are supported only with single relation fields // - indirect expands are supported only with single relation fields
func (dao *Dao) expandRecords(records []*models.Record, expandPath string, fetchFunc ExpandFetchFunc, recursionLevel int) error { func (dao *Dao) expandRecords(records []*models.Record, expandPath string, fetchFunc ExpandFetchFunc, recursionLevel int) error {
if fetchFunc == nil { if fetchFunc == nil {
return errors.New("Relation records fetchFunc is not set.") // load a default fetchFunc
fetchFunc = func(relCollection *models.Collection, relIds []string) ([]*models.Record, error) {
return dao.FindRecordsByIds(relCollection.Id, relIds)
}
} }
if expandPath == "" || recursionLevel > MaxExpandDepth || len(records) == 0 { if expandPath == "" || recursionLevel > MaxExpandDepth || len(records) == 0 {

View File

@ -48,15 +48,6 @@ func TestExpandRecords(t *testing.T) {
0, 0,
0, 0,
}, },
{
"empty fetchFunc",
"demo4",
[]string{"i9naidtvr6qsgb4", "qzaqccwrmva4o1n"},
[]string{"self_rel_one", "self_rel_many.self_rel_one"},
nil,
0,
2,
},
{ {
"fetchFunc with error", "fetchFunc with error",
"demo4", "demo4",
@ -101,6 +92,19 @@ func TestExpandRecords(t *testing.T) {
0, 0,
1, 1,
}, },
{
"with nil fetchfunc",
"users",
[]string{
"bgs820n361vj1qd",
"4q1xlclmfloku33",
"oap640cot4yru2s", // no rels
},
[]string{"rel"},
nil,
2,
0,
},
{ {
"expand normalizations", "expand normalizations",
"demo4", "demo4",
@ -132,6 +136,19 @@ func TestExpandRecords(t *testing.T) {
2, 2,
0, 0,
}, },
{
"with nil fetchfunc",
"users",
[]string{
"bgs820n361vj1qd",
"4q1xlclmfloku33",
"oap640cot4yru2s", // no rels
},
[]string{"rel"},
nil,
2,
0,
},
{ {
"maxExpandDepth reached", "maxExpandDepth reached",
"demo4", "demo4",
@ -228,15 +245,6 @@ func TestExpandRecord(t *testing.T) {
0, 0,
0, 0,
}, },
{
"empty fetchFunc",
"demo4",
"i9naidtvr6qsgb4",
[]string{"self_rel_one", "self_rel_many.self_rel_one"},
nil,
0,
2,
},
{ {
"fetchFunc with error", "fetchFunc with error",
"demo4", "demo4",