TinyBase logoTinyBase

forEachIndex

The forEachIndex method takes a function that it will then call for each Index in a specified Indexes object.

forEachIndex(indexCallback: IndexCallback): void
TypeDescription
indexCallbackIndexCallback

The function that should be called for every Index.

returnsvoid

This has no return value.

This method is useful for iterating over the structure of the Indexes object in a functional style. The indexCallback parameter is a IndexCallback function that will be called with the Id of each Index, and with a function that can then be used to iterate over each Slice of the Index, should you wish.

Example

This example iterates over each Index in an Indexes object, and lists each Slice Id within them.

const store = createStore().setTable('pets', {
  fido: {species: 'dog', color: 'brown'},
  felix: {species: 'cat', color: 'black'},
  cujo: {species: 'dog', color: 'black'},
});
const indexes = createIndexes(store)
  .setIndexDefinition('bySpecies', 'pets', 'species')
  .setIndexDefinition('byColor', 'pets', 'color');

indexes.forEachIndex((indexId, forEachSlice) => {
  console.log(indexId);
  forEachSlice((sliceId) => console.log(`- ${sliceId}`));
});
// -> 'bySpecies'
// -> '- dog'
// -> '- cat'
// -> 'byColor'
// -> '- brown'
// -> '- black'