TinyBase logoTinyBase

forEachResultRow

The forEachResultRow method takes a function that it will then call for each ResultRow in the ResultTable of a query.

forEachResultRow(
  queryId: Id,
  rowCallback: ResultRowCallback,
): void
TypeDescription
queryIdId

The Id of a query.

rowCallbackResultRowCallback

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

returnsvoid

This has no return value.

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

Example

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

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');
  },
);

queries.forEachResultRow('dogColors', (rowId, forEachCell) => {
  console.log(rowId);
  forEachCell((cellId) => console.log(`- ${cellId}`));
});
// -> 'fido'
// -> '- color'
// -> 'cujo'
// -> '- color'

Since

v2.0.0