Keyvalue store is iterable?


#1

i want to run a foreach loop on a key-value store. However I don’t see a way to get all the keys (or even the current key if it could be made iterable).

It looks like I have to create an index somewhere in order to be able to have a list of all the keys. Any chance of a
datastore.forEach() something like dataset.forEach(), or perhaps
datastore.getKeys(); //returns something like [key1,key2,key3,...]


#2

Hi jstanley,

the SDK does not provide helper methods for all the API endpoints of Apify, but you can use the apify-client package to do just that.

See the listKeys() function that maps the List keys API.

Best,
Ondra


#4

Thank you. In case anyone else comes across this, here is an example of how this could be done:

const Apify  = require('apify');
const ApifyClient = require('apify-client');
const apifyClient = new ApifyClient({
  userId: Apify.getEnv().userId,
  token: Apify.getEnv().token,
});
const keyValueStores = apifyClient.keyValueStores;
const store = await keyValueStores.getOrCreateStore({ storeName: "myStoreName"});
apifyClient.setOptions({ storeId: store.id });
var paginationList = {"isTruncated":true};
const storeKeys = {"datastore":"myStoreName","keys":[]};
while (paginationList.isTruncated){
  console.log("ADDING keys from "+store.id+"/"+paginationList.nextExclusiveStartKey);
  paginationList = await keyValueStores.listKeys({"exclusiveStartKey":paginationList.nextExclusiveStartKey});
  storeKeys.keys = storeKeys.keys.concat(paginationList.items);
}
//storeKeys.keys now has all the keys for the key-value-store