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:
a string:author.fetch({ related: 'books' }) fetches the main JSON record with all records in the authored_works relationship.
an array of strings:author.fetch({ related: [ 'books', 'influences' ] }) fetches the main JSON record with all records in the authored_works and influences relationships.
a boolean:author.fetch({ related: true }) fetches the main JSON record with all records in the authored_works, authored_categories, and influences relationships.
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.
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 thefetch()
method with arelated
key whose value indicates which related records should be fetched along with the main record.Given the following model…
… the value of the
related
key in the.fetch()
options object can be any one of the following:author.fetch({ related: 'books' })
fetches the main JSON record with all records in theauthored_works
relationship.author.fetch({ related: [ 'books', 'influences' ] })
fetches the main JSON record with all records in theauthored_works
andinfluences
relationships.author.fetch({ related: true })
fetches the main JSON record with all records in theauthored_works
,authored_categories
, andinfluences
relationships.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: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.