added extra collection field exist check to minimize misuse and have a more clear error message

This commit is contained in:
Gani Georgiev
2026-01-27 16:44:18 +02:00
parent 55e24344ff
commit 4a1e3aed6e
2 changed files with 18 additions and 1 deletions

View File

@@ -314,9 +314,19 @@ func (app *BaseApp) FindAllRecords(collectionModelOrIdentifier any, exprs ...dbx
// FindFirstRecordByData returns the first found record matching
// the provided key-value pair.
func (app *BaseApp) FindFirstRecordByData(collectionModelOrIdentifier any, key string, value any) (*Record, error) {
collection, err := getCollectionByModelOrIdentifier(app, collectionModelOrIdentifier)
if err != nil {
return nil, err
}
field := collection.Fields.GetByName(key)
if field == nil {
return nil, errors.New("invalid or missing field " + key)
}
record := &Record{}
err := app.RecordQuery(collectionModelOrIdentifier).
err = app.RecordQuery(collection).
AndWhere(dbx.HashExp{inflector.Columnify(key): value}).
Limit(1).
One(record)

View File

@@ -525,6 +525,13 @@ func TestFindFirstRecordByData(t *testing.T) {
"",
true,
},
{
"demo2",
"invalid_or_missing",
"llvuca81nly1qls",
"",
true,
},
{
"demo2",
"id",