TinyBase logoTinyBase

forEachSlice

The forEachSlice method takes a function that it will then call for each Slice in a specified Index.

forEachSlice(
  indexId: string,
  sliceCallback: SliceCallback,
): void
TypeDescription
indexIdstring

The Id of the Index to iterate over.

sliceCallbackSliceCallback

The function that should be called for every Slice.

returnsvoid

This has no return value.

This method is useful for iterating over the Slice structure of the Index in a functional style. The sliceCallback parameter is a SliceCallback function that will be called with the Id of each Slice and a function for iterating over its Rows.

Example

This example iterates over each Row in a Slice, and lists its Id.

import {createIndexes, createStore} from 'tinybase';

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

indexes.forEachSlice('bySpecies', (sliceId, forEachRow) => {
  console.log(sliceId);
  forEachRow((rowId) => console.log(`- ${rowId}`));
});
// -> 'dog'
// -> '- fido'
// -> '- cujo'
// -> 'cat'
// -> '- felix'

Since

v1.0.0