Skip to content

FetchingRelationships

Mihael Safaric edited this page Jul 27, 2020 · 4 revisions

When fetching a model via findOne method, or when fetching a list of models via find method, includeRelationships parameter can be passed.

includeRelationships is an array of strings or RelationshipDescriptors which represent the model relationship indentificators or the relationship indentificators of nested models. When passing RelationshipDescriptors, different request options can be specified for each subsequent request.

Valid includeRelationships values:

All the examples below assume fetching a user with specified relationships.

  • []

    • fetches a user without any additional calls
  • ['address']

    • fetches a user and when the user is fetched, it extracts address relationship URL from the response and fetches address resource
  • [{ name: 'address' }]

    • the same behaviour as ['address'], but instead of a simple string, RelationshipDescriptor is passed
  • [{ name: 'address', options: { params: { size: '100' } } }]

    • the same behaviour as [{ name: 'address' }], but different paramaters are used when doing the "address" request
  • ['address.city']

    • fetches a user and when the user is fetched, it extracts address relationship URL from the response and fetches address resource
    • after the address is fetched, it extracts city relationship URL from the response and fetches city resource
  • ['address', 'address.city']

    • the same behaviour as ['address.city']
    • the library figures out that address will already be fetched as a part of address.city, so it skips that additional request
  • [{ name: 'address', options: { params: { size: '100' } } }, 'address.city']

    • the same behaviour as ['address', 'address.city'], but different paramaters are used when doing the "address" request
    • 'address.city' request would be the same as in the previous example