all files / lib/waterline/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                                1278×     1278×         1278×     3508×     3487× 3267×   3232× 3232×   284× 284×       1278×    
/**
 * 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.push(key);
    if (hasOwnProperty(attribute, 'foreignKey')) associations.models.push(key);
 
  });
 
  return associations;
};