all files / lib/offshore/utils/nestedOperations/ valuesParser.js

100% Statements 19/19
100% Branches 14/14
100% Functions 2/2
100% Lines 13/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                1823×     1823×         1823×     7482×     7455× 7209×   7151× 7151×   770× 770×       1823×    
/**
 * Module Dependencies
 */
 
var hasOwnProperty = require('../helpers').object.hasOwnProperty;
 
/**
 * Traverse an object representing values and map out any associations.
 *
 * @param {String} model
 * @param {Object} schema
 * @param {Object} values
 * @return {Object}
 * @api private
 */
 
 
module.exports = function(model, schema, values) {
  var self = this;
 
  // Pick out the top level associations
  var associations = {
    collections: {},
    models: {}
  };
 
  Object.keys(values).forEach(function(key) {
 
    // Ignore values equal to null
    if (values[key] === null) return;
 
    // Ignore joinTables
    if (hasOwnProperty(schema[model], 'junctionTable')) return;
    if (!hasOwnProperty(schema[model].attributes, key)) return;
 
    var attribute = schema[model].attributes[key];
    if (!hasOwnProperty(attribute, 'collection') && !hasOwnProperty(attribute, 'foreignKey')) return;
 
    if (hasOwnProperty(attribute, 'collection')) associations.collections[key] = values[key];
    if (hasOwnProperty(attribute, 'foreignKey')) associations.models[key] = values[key];
 
  });
 
  return associations;
};