TinyBase logoTinyBase

forEachResultTable

The forEachResultTable method takes a function that it will then call for each ResultTable in the Queries object.

forEachResultTable(tableCallback: ResultTableCallback): void
TypeDescription
tableCallbackResultTableCallback

The function that should be called for every query's ResultTable.

returnsvoid

This has no return value.

This method is useful for iterating over all the ResultTables of the queries in a functional style. The tableCallback parameter is a ResultTableCallback function that will be called with the Id of each ResultTable, and with a function that can then be used to iterate over each ResultRow of the ResultTable, should you wish.

Example

This example iterates over each query's ResultTable in a Queries object.

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

const queries = createQueries(store)
  .setQueryDefinition('dogColors', 'pets', ({select, where}) => {
    select('color');
    where('species', 'dog');
  })
  .setQueryDefinition('catColors', 'pets', ({select, where}) => {
    select('color');
    where('species', 'cat');
  });

queries.forEachResultTable((queryId, forEachRow) => {
  console.log(queryId);
  forEachRow((rowId) => console.log(`- ${rowId}`));
});
// -> 'dogColors'
// -> '- fido'
// -> '- cujo'
// -> 'catColors'
// -> '- felix'

Since

v2.0.0