diff --git a/daos/collection.go b/daos/collection.go index 2e8ec550..a2952a23 100644 --- a/daos/collection.go +++ b/daos/collection.go @@ -32,15 +32,15 @@ func (dao *Dao) FindCollectionsByType(collectionType string) ([]*models.Collecti return collections, nil } -// FindCollectionByNameOrId finds the first collection by its name or id. +// FindCollectionByNameOrId finds a single collection by its name (case insensitive) or id. func (dao *Dao) FindCollectionByNameOrId(nameOrId string) (*models.Collection, error) { model := &models.Collection{} err := dao.CollectionQuery(). - AndWhere(dbx.Or( - dbx.HashExp{"id": nameOrId}, - dbx.HashExp{"name": nameOrId}, - )). + AndWhere(dbx.NewExp("[[id]] = {:id} OR LOWER([[name]])={:name}", dbx.Params{ + "id": nameOrId, + "name": strings.ToLower(nameOrId), + })). Limit(1). One(model) diff --git a/daos/collection_test.go b/daos/collection_test.go index a58980f8..b992fa07 100644 --- a/daos/collection_test.go +++ b/daos/collection_test.go @@ -3,6 +3,7 @@ package daos_test import ( "encoding/json" "errors" + "strings" "testing" "github.com/pocketbase/pocketbase/daos" @@ -71,6 +72,7 @@ func TestFindCollectionByNameOrId(t *testing.T) { {"missing", true}, {"wsmn24bux7wo113", false}, {"demo1", false}, + {"DEMO1", false}, // case insensitive check } for i, scenario := range scenarios { @@ -81,7 +83,7 @@ func TestFindCollectionByNameOrId(t *testing.T) { t.Errorf("(%d) Expected hasErr to be %v, got %v (%v)", i, scenario.expectError, hasErr, err) } - if model != nil && model.Id != scenario.nameOrId && model.Name != scenario.nameOrId { + if model != nil && model.Id != scenario.nameOrId && !strings.EqualFold(model.Name, scenario.nameOrId) { t.Errorf("(%d) Expected model with identifier %s, got %v", i, scenario.nameOrId, model) } }