KVStore: Extend kvstore to retrieve all items (#50848)

* Extend kvstore to retrieve all items

* Fix comment

* Fix tests

* Change test order

* Move test outside to avoid order conditions

* Update Items to GetAll function and return a map

* Add explanation of map result

* Add description comment

Co-authored-by: Tania B <yalyna.ts@gmail.com>
This commit is contained in:
Selene
2022-06-23 11:12:07 +02:00
committed by GitHub
co-authored by Tania B
parent 496c2e26f4
commit ecc15a2f71
4 changed files with 80 additions and 0 deletions
+25
View File
@@ -109,3 +109,28 @@ func (kv *kvStoreSQL) Keys(ctx context.Context, orgId int64, namespace string, k
})
return keys, err
}
// GetAll get all items a given namespace and org. To query for all
// organizations the constant 'kvstore.AllOrganizations' can be passed as orgId.
// The map result is like map[orgId]map[key]value
func (kv *kvStoreSQL) GetAll(ctx context.Context, orgId int64, namespace string) (map[int64]map[string]string, error) {
var results []Item
err := kv.sqlStore.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
query := dbSession.Where("namespace = ?", namespace)
if orgId != AllOrganizations {
query.And("org_id = ?", orgId)
}
return query.Find(&results)
})
items := map[int64]map[string]string{}
for _, r := range results {
if _, ok := items[*r.OrgId]; !ok {
items[*r.OrgId] = map[string]string{}
}
items[*r.OrgId][*r.Key] = r.Value
}
return items, err
}