added extra collection field exist check to minimize misuse and have a more clear error message
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -525,6 +525,13 @@ func TestFindFirstRecordByData(t *testing.T) {
|
||||
"",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"demo2",
|
||||
"invalid_or_missing",
|
||||
"llvuca81nly1qls",
|
||||
"",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"demo2",
|
||||
"id",
|
||||
|
||||
Reference in New Issue
Block a user