TinyBase logoTinyBase

forEachRelationship

The forEachRelationship method takes a function that it will then call for each Relationship in a specified Relationships object.

forEachRelationship(relationshipCallback: RelationshipCallback): void
TypeDescription
relationshipCallbackRelationshipCallback

The function that should be called for every Relationship.

returnsvoid

This has no return value.

This method is useful for iterating over the structure of the Relationships object in a functional style. The relationshipCallback parameter is a RelationshipCallback function that will be called with the Id of each Relationship, and with a function that can then be used to iterate over each local Row involved in the Relationship.

Example

This example iterates over each Relationship in a Relationships object, and lists each Row Id within them.

const store = createStore().setTable('pets', {
  fido: {species: 'dog', next: 'felix'},
  felix: {species: 'cat', next: 'cujo'},
  cujo: {species: 'dog'},
});
const relationships = createRelationships(store)
  .setRelationshipDefinition('petSpecies', 'pets', 'species', 'species')
  .setRelationshipDefinition('petSequence', 'pets', 'pets', 'next');

relationships.forEachRelationship((relationshipId, forEachRow) => {
  console.log(relationshipId);
  forEachRow((rowId) => console.log(`- ${rowId}`));
});
// -> 'petSpecies'
// -> '- fido'
// -> '- felix'
// -> '- cujo'
// -> 'petSequence'
// -> '- fido'
// -> '- felix'
// -> '- cujo'