lib/serializer.js
function parseResource (type, data, relations, options) {
  var resource = {}
  var attributes = {}
  var relationships

  resource.type = type
  relationships = parseRelations(data, relations, options)

  if (!_.isEmpty(relationships)) {
    resource.relationships = relationships
  }

  if (options.foreignKeys !== true) {
    // Remove any foreign keys from this resource
    options.app.models().forEach(function (model) {
      _.each(model.relations, function (relation) {
        var fkModel = relation.modelTo
        var fkName = relation.keyTo
        if (utils.relationFkOnModelFrom(relation)) {
          fkModel = relation.modelFrom
          fkName = relation.keyFrom
        }
        if (fkModel === options.model && fkName !== options.primaryKeyField) {
          // check options and decide whether to remove foreign keys.
          if (
            options.foreignKeys !== false && Array.isArray(options.foreignKeys)
          ) {
            for (var i = 0; i < options.foreignKeys.length; i++) {
              // if match on model
              if (options.foreignKeys[i].model === fkModel.sharedClass.name) {
                // if no method specified
                if (!options.foreignKeys[i].method) return
                // if method match
                if (options.foreignKeys[i].method === options.method) return
              }
            }
          }
          delete data[fkName]
        }
      })
    })
  }

  _.each(data, function (value, property) {
    if (property === options.primaryKeyField) {
      resource.id = _(value).toString()
    } else {
      if (
        !options.attributes[type] ||
        _.includes(options.attributes[type], property)
      ) {
        attributes[property] = value
      }
    }
  })

  if (_.isUndefined(resource.id)) {
    return null
  }

  // if it's a relationship request
  if (options.isRelationshipRequest) {
    return resource
  }

  resource.attributes = attributes
  if (options.dataLinks) {
    resource.links = makeLinks(options.dataLinks, resource)
  }

  return resource
}
function parseResource (type, data, relations, options) {
  var resource = {}
  var attributes = {}
  var relationships

  resource.type = type
  relationships = parseRelations(data, relations, options)

  if (!_.isEmpty(relationships)) {
    resource.relationships = relationships
  }

  if (options.foreignKeys !== true) {
    // Remove any foreign keys from this resource
    options.app.models().forEach(function (model) {
      _.each(model.relations, function (relation) {
        var fkModel = relation.modelTo
        var fkName = relation.keyTo
        if (utils.relationFkOnModelFrom(relation)) {
          fkModel = relation.modelFrom
          fkName = relation.keyFrom
        }
        if (fkModel === options.model && fkName !== options.primaryKeyField) {
          // check options and decide whether to remove foreign keys.
          if (
            options.foreignKeys !== false && Array.isArray(options.foreignKeys)
          ) {
            for (var i = 0; i < options.foreignKeys.length; i++) {
              // if match on model
              if (options.foreignKeys[i].model === fkModel.sharedClass.name) {
                // if no method specified
                if (!options.foreignKeys[i].method) return
                // if method match
                if (options.foreignKeys[i].method === options.method) return
              }
            }
          }
          delete data[fkName]
        }
      })
    })
  }

  _.each(data, function (value, property) {
    if (property === options.primaryKeyField) {
      resource.id = _(value).toString()
    } else {
      if (
        !options.attributes[type] ||
        _.includes(options.attributes[type], property)
      ) {
        attributes[property] = value
      }
    }
  })

  if (_.isUndefined(resource.id)) {
    return null
  }

  // if it's a relationship request
  if (options.isRelationshipRequest) {
    return resource
  }

  resource.attributes = attributes
  if (options.dataLinks) {
    resource.links = makeLinks(options.dataLinks, resource)
  }

  return resource
}
function parseResource ( type , data , relations , options ) { var resource = { } var attributes = { } var relationships resource . type = type relationships = parseRelations ( data , relations , options ) if ( ! _ . isEmpty ( relationships ) ) { resource . relationships = relationships } if ( options . foreignKeys !== true ) { // Remove any foreign keys from this resource options . app . models ( ) . forEach ( function ( model ) { _ . each ( model . relations , function ( relation ) { var fkModel = relation . modelTo var fkName = relation . keyTo if ( utils . relationFkOnModelFrom ( relation ) ) { fkModel = relation . modelFrom fkName = relation . keyFrom } if ( fkModel === options . model && fkName !== options . primaryKeyField ) { // check options and decide whether to remove foreign keys. if ( options . foreignKeys !== false && Array . isArray ( options . foreignKeys ) ) { for ( var i = 0 ; i < options . foreignKeys . length ; i ++ ) { // if match on model if ( options . foreignKeys [ i ] . model === fkModel . sharedClass . name ) { // if no method specified if ( ! options . foreignKeys [ i ] . method ) return // if method match if ( options . foreignKeys [ i ] . method === options . method ) return } } } delete data [ fkName ] } } ) } ) } _ . each ( data , function ( value , property ) { if ( property === options . primaryKeyField ) { resource . id = _ ( value ) . toString ( ) } else { if ( ! options . attributes [ type ] || _ . includes ( options . attributes [ type ] , property ) ) { attributes [ property ] = value } } } ) if ( _ . isUndefined ( resource . id ) ) { return null } // if it's a relationship request if ( options . isRelationshipRequest ) { return resource } resource . attributes = attributes if ( options . dataLinks ) { resource . links = makeLinks ( options . dataLinks , resource ) } return resource }
Parses a a request and returns a resource
@private
@memberOf {Serializer}
@param {String} type
@param {Object} data
@param {Object} relations
@param {Object} options
@return {Object|null}
Parses a a request and returns a resource