all files / lib/waterline/model/lib/internalMethods/ normalizeAssociations.js

94.12% Statements 16/17
92.86% Branches 13/14
100% Functions 3/3
93.33% Lines 14/15
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62                          2586×   2586×   2586× 2586×                   2586×     22078×     1056×                         2586×     22078×     827× 50×        
 
/**
 * Check and normalize belongs_to and has_many association keys
 *
 * Ensures that a belongs_to association is an object and that a has_many association
 * is an array.
 *
 * @param {Object} context,
 * @param {Object} proto
 * @api private
 */
 
var Normalize = module.exports = function(context, proto) {
 
  this.proto = proto;
 
  var attributes = context.waterline.collections[context.identity].attributes || {};
 
  this.collections(attributes);
  this.models(attributes);
};
 
/**
 * Normalize Collection Attribute to Array
 *
 * @param {Object} attributes
 * @api private
 */
 
Normalize.prototype.collections = function(attributes) {
  for (var attribute in attributes) {
 
    // If attribute is not a collection, it doesn't need normalizing
    if (!attributes[attribute].collection) continue;
 
    // Sets the attribute as an array if it's not already
    Iif (this.proto[attribute] && !Array.isArray(this.proto[attribute])) {
      this.proto[attribute] = [this.proto[attribute]];
    }
  }
};
 
/**
 * Normalize Model Attribute to Object
 *
 * @param {Object} attributes
 * @api private
 */
 
Normalize.prototype.models = function(attributes) {
  for (var attribute in attributes) {
 
    // If attribute is not a model, it doesn't need normalizing
    if (!attributes[attribute].model) continue;
 
    // Sets the attribute to the first item in the array if it's an array
    if (this.proto[attribute] && Array.isArray(this.proto[attribute])) {
      this.proto[attribute] = this.proto[attribute][0];
    }
  }
};