ChromaticHQ / jsonmonger

23 stars 1 forks source link

Fetch related records #5

Closed agarzola closed 6 years ago

agarzola commented 6 years ago

This PR implements fetching with relationships (#3) as an opt-in feature of the Model.fetch() method. This is accomplished via an options object passed to the fetch() method with a related key whose value indicates which related records should be fetched along with the main record.

Given the following model…

const Model = require('jsonmonger').Model;
const Author = new Model({
  name: 'attributes.full_name',
  books: 'relationships.authored_works',
  topics: 'relationships.authored_categories',
  influences: 'relationships.influences',
});

… the value of the related key in the .fetch() options object can be any one of the following:

By default, no relationships are included in the request if related is not set in .fetch(), but a default can be set at the model level:

const Author = new Model({
  /* model props */
}, {
  related: true,
});

The Author model’s default behavior will be to fetch all related records. This behavior can still be overridden in the .fetch() call’s options object.