Skip to content

Commit f3a49bf

Browse files
committed
feat(serializer) added relationships object to included data
1 parent 9678cad commit f3a49bf

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

lib/serializer.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function defaultSerialize (options, cb) {
4242
resultData,
4343
options.requestedIncludes,
4444
options.relationships,
45-
options.app
45+
options
4646
)
4747
} catch (err) {
4848
cb(err)
@@ -344,7 +344,11 @@ function makeLinks (links, item) {
344344
* @throws {Error}
345345
* @return {undefined}
346346
*/
347-
function handleIncludes (resp, includes, relations, app) {
347+
function handleIncludes (resp, includes, relations, options) {
348+
var app = options.app;
349+
350+
relations = utils.setIncludedRelations(relations, app)
351+
348352
var resources = _.isArray(resp.data) ? resp.data : [resp.data]
349353

350354
if (typeof includes === 'string') {
@@ -360,6 +364,7 @@ function handleIncludes (resp, includes, relations, app) {
360364
var embedded = resources.map(function subsituteEmbeddedForIds (resource) {
361365
return includes.map(function (include) {
362366
var relation = relations[include]
367+
var includedRelations = relations[include].relations
363368
var propertyKey = relation.keyFrom
364369
var plural = ''
365370
if (relation.polymorphic && utils.relationFkOnModelFrom(relation)) {
@@ -392,7 +397,9 @@ function handleIncludes (resp, includes, relations, app) {
392397
rel,
393398
propertyKey,
394399
relation.keyTo,
395-
plural
400+
plural,
401+
includedRelations,
402+
options
396403
)
397404
})
398405
embeds = _.compact(embeds)
@@ -410,7 +417,9 @@ function handleIncludes (resp, includes, relations, app) {
410417
rel,
411418
propertyKey,
412419
relation.keyFrom,
413-
plural
420+
plural,
421+
includedRelations,
422+
options
414423
)
415424

416425
resource.relationships[include].data = {
@@ -459,9 +468,19 @@ function handleIncludes (resp, includes, relations, app) {
459468
* @param {String} type
460469
* @return {Object}
461470
*/
462-
function createCompoundIncludes (relationship, key, fk, type) {
471+
function createCompoundIncludes (relationship, key, fk, type, includedRelations, options) {
463472
var compoundInclude = makeRelation(type, String(relationship[key]))
464473

474+
475+
if (options && !_.isEmpty(includedRelations)) {
476+
var defaultModelPath = options.modelPath
477+
options.modelPath = type
478+
479+
compoundInclude.relationships = parseRelations(relationship, includedRelations, options)
480+
481+
options.modelPath = defaultModelPath
482+
}
483+
465484
// remove the id key since its part of the base compound document, not part of attributes
466485
delete relationship[key]
467486
delete relationship[fk]

lib/utils.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ module.exports = {
1818
primaryKeyForModel: primaryKeyForModel,
1919
shouldNotApplyJsonApi: shouldNotApplyJsonApi,
2020
shouldApplyJsonApi: shouldApplyJsonApi,
21-
relationFkOnModelFrom: relationFkOnModelFrom
21+
relationFkOnModelFrom: relationFkOnModelFrom,
22+
setIncludedRelations: setIncludedRelations
2223
}
2324

2425
function primaryKeyForModel (model) {
@@ -227,3 +228,14 @@ function shouldNotApplyJsonApi (ctx, options) {
227228
function relationFkOnModelFrom (relation) {
228229
return relation.type === 'belongsTo' || relation.type === 'referencesMany'
229230
}
231+
232+
function setIncludedRelations (relations, app) {
233+
for (var key in relations) {
234+
if (relations.hasOwnProperty(key)) {
235+
var name = relations[key].modelTo.sharedClass.name
236+
relations[key].relations = app.models[name].relations
237+
}
238+
}
239+
return relations
240+
241+
}

0 commit comments

Comments
 (0)