lib/serializer.js
function parseRelations (data, relations, options) {
  var relationships = {}

  _.each(relations, function (relation, name) {
    var fkName = relation.keyTo

    // If relation is belongsTo then fk is the other way around
    if (utils.relationFkOnModelFrom(relation)) {
      fkName = relation.keyFrom
    }

    var pk = data[options.primaryKeyField]
    var fk = data[fkName]

    var toType = ''
    if (relation.polymorphic && utils.relationFkOnModelFrom(relation)) {
      var discriminator = relation.polymorphic.discriminator
      var model = options.app.models[data[discriminator]]
      toType = utils.pluralForModel(model)
    } else {
      toType = utils.pluralForModel(relation.modelTo)
    }

    // Relationship `links` should always be defined unless this is a
    // relationship request
    if (!options.isRelationshipRequest) {
      relationships[name] = {
        links: {
          related: options.host +
            options.restApiRoot +
            '/' +
            options.modelPath +
            '/' +
            pk +
            '/' +
            name
        }
      }
    }

    var relationship = null

    if (!_.isUndefined(fk) && relation.modelTo !== relation.modelFrom) {
      if (_.isArray(fk)) {
        relationship = makeRelations(toType, fk, options)
      } else {
        relationship = makeRelation(toType, fk, options)
      }
    }

    // No `data` key should be present unless there is actual relationship data.
    if (relationship) {
      relationships[name].data = relationship
    } else if (relation.type === 'belongsTo') {
      relationships[name].data = null
    }
  })

  return relationships
}
function parseRelations (data, relations, options) {
  var relationships = {}

  _.each(relations, function (relation, name) {
    var fkName = relation.keyTo

    // If relation is belongsTo then fk is the other way around
    if (utils.relationFkOnModelFrom(relation)) {
      fkName = relation.keyFrom
    }

    var pk = data[options.primaryKeyField]
    var fk = data[fkName]

    var toType = ''
    if (relation.polymorphic && utils.relationFkOnModelFrom(relation)) {
      var discriminator = relation.polymorphic.discriminator
      var model = options.app.models[data[discriminator]]
      toType = utils.pluralForModel(model)
    } else {
      toType = utils.pluralForModel(relation.modelTo)
    }

    // Relationship `links` should always be defined unless this is a
    // relationship request
    if (!options.isRelationshipRequest) {
      relationships[name] = {
        links: {
          related: options.host +
            options.restApiRoot +
            '/' +
            options.modelPath +
            '/' +
            pk +
            '/' +
            name
        }
      }
    }

    var relationship = null

    if (!_.isUndefined(fk) && relation.modelTo !== relation.modelFrom) {
      if (_.isArray(fk)) {
        relationship = makeRelations(toType, fk, options)
      } else {
        relationship = makeRelation(toType, fk, options)
      }
    }

    // No `data` key should be present unless there is actual relationship data.
    if (relationship) {
      relationships[name].data = relationship
    } else if (relation.type === 'belongsTo') {
      relationships[name].data = null
    }
  })

  return relationships
}
function parseRelations ( data , relations , options ) { var relationships = { } _ . each ( relations , function ( relation , name ) { var fkName = relation . keyTo // If relation is belongsTo then fk is the other way around if ( utils . relationFkOnModelFrom ( relation ) ) { fkName = relation . keyFrom } var pk = data [ options . primaryKeyField ] var fk = data [ fkName ] var toType = '' if ( relation . polymorphic && utils . relationFkOnModelFrom ( relation ) ) { var discriminator = relation . polymorphic . discriminator var model = options . app . models [ data [ discriminator ] ] toType = utils . pluralForModel ( model ) } else { toType = utils . pluralForModel ( relation . modelTo ) } // Relationship `links` should always be defined unless this is a // relationship request if ( ! options . isRelationshipRequest ) { relationships [ name ] = { links : { related : options . host + options . restApiRoot + '/' + options . modelPath + '/' + pk + '/' + name } } } var relationship = null if ( ! _ . isUndefined ( fk ) && relation . modelTo !== relation . modelFrom ) { if ( _ . isArray ( fk ) ) { relationship = makeRelations ( toType , fk , options ) } else { relationship = makeRelation ( toType , fk , options ) } } // No `data` key should be present unless there is actual relationship data. if ( relationship ) { relationships [ name ] . data = relationship } else if ( relation . type === 'belongsTo' ) { relationships [ name ] . data = null } } ) return relationships }
Parses relations from the request.
@private
@memberOf {Serializer}
@param {Object} data
@param {Object} relations
@param {Object} options
@return {Object}
Parses relations from the request .