all files / lib/offshore/query/finders/ joins.js

89.58% Statements 86/96
75% Branches 39/52
100% Functions 12/12
91.67% Lines 77/84
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237                    1133×     1133×     1133×     1133×     1133×     1133×     1133×   1133×                   1133× 1133×     1133×         1133×     1133×     779×   533×             1133× 533×   533×     246× 503× 251×       246×         1133×                     1133× 1133× 1133×     1133×     1133× 1633× 1633×     1133×                       1633×     1633×   8850× 8850×       8850×           8850× 8850× 3820×   5030×         8850×     1107×       1107×     895×       895×     769×   769×   769× 1022×         1022× 168× 168× 168× 168×         854× 854× 854× 854×       769× 769×                             1633×                           895× 895× 895×     895× 1194× 1147× 1147×     895×    
/**
 * Module Dependencies
 */
 
var _ = require('lodash');
var utils = require('../../utils/helpers');
var hop = utils.object.hasOwnProperty;
 
/**
 * Logic For Handling Joins inside a Query Results Object
 */
 
var Joins = module.exports = function(joins, values, identity, schema, collections) {
 
  this.identity = identity;
 
  // Hold Joins specified in the criteria
  this.joins = joins || [];
 
  // Hold the result values
  this.values = values || [];
 
  // Hold the overall schema
  this.schema = schema || {};
 
  // Hold all the Offshore collections so we can make models
  this.collections = collections || {};
 
  // Build up modelOptions
  this.modelOptions();
 
  // Modelize values
  this.models = this.makeModels();
 
  return this;
};
 
/**
 * Build up Join Options that will be passed down to a Model instance.
 *
 * @api private
 */
 
Joins.prototype.modelOptions = function modelOptions() {
 
  var self = this;
  var joins;
 
  // Build Model Options, determines what associations to render in toObject
  this.options = {
    showJoins: !!this.joins
  };
 
  // If no joins were used, just return
  Iif (!this.joins) return;
 
  // Map out join names to pass down to the model instance
  joins = this.joins.filter(function(join) {
 
    // If the value is not being selected, don't add it to the array
    if (!join.select) return false;
 
    return join;
  });
 
  // Map out join key names and attach to the options object.
  // For normal assoiciations, use the child table name that is being joined. For many-to-many
  // associations the child table name won't work so grab the alias used and use that for the
  // join name. It will be the one that is transformed.
  this.options.joins = joins.map(function(join) {
    var child = [];
    // If a junctionTable was not used, return the child table
    if (!join.junctionTable) return join.child;
 
    // Find the original alias for the join
    self.joins.forEach(function(j) {
      if (j.child !== join.parent) return;
      child.push(j.alias);
    });
 
    // If a child was found, return it otherwise just return the original child join
    Eif (child) return child;
    return join.child;
  });
 
  // Flatten joins
  this.options.joins = _.uniq(_.flatten(this.options.joins));
};
 
/**
 * Transform Values into instantiated Models.
 *
 * @return {Array}
 * @api private
 */
 
Joins.prototype.makeModels = function makeModels() {
 
  var self = this;
  var models = [];
  var model;
 
  // If values are invalid (not an array), return them early.
  Iif (!this.values || !this.values.forEach) return this.values;
 
  // Make each result an instance of model
  this.values.forEach(function(value) {
    model = self.modelize(value);
    models.push(model);
  });
 
  return models;
};
 
/**
 * Handle a single Result and inspect it's values for anything
 * that needs to become a Model instance.
 *
 * @param {Object} value
 * @return {Object}
 * @api private
 */
 
Joins.prototype.modelize = function modelize(value) {
  var self = this;
 
  // Look at each key in the object and see if it was used in a join
  Object.keys(value).forEach(function(key) {
 
    var joinKey = false;
    var attr,
        usedInJoin;
 
    // If showJoins wasn't set or no joins were found there is nothing to modelize
    Iif (!self.options.showJoins || !self.options.joins) return;
 
    // Look at the schema for an attribute and check if it's a foreign key
    // or a virtual hasMany collection attribute
 
    // Check if there is a transformation on this attribute
    var transformer = self.collections[self.identity]._transformer._transformations;
    if (hop(transformer, key)) {
      attr = self.schema[transformer[key]];
    } else {
      attr = self.schema[key];
    }
 
    // If an attribute was found but it's not a model, this means it's a normal
    // key/value attribute and not an association so there is no need to modelize it.
    if (attr && !attr.hasOwnProperty('model')) return;
 
    // If the attribute has a `model` property, the joinKey is the collection of the model
    if (attr && attr.hasOwnProperty('model')) joinKey = attr.model;
 
    // If the attribute is a foreign key but it was not populated, just leave the foreign key
    // as it is and don't try and modelize it.
    if (joinKey && self.options.joins.indexOf(joinKey) < 0) return;
 
    // Check if the key was used in a join
    usedInJoin = self.checkForJoin(key);
 
    // If the attribute wasn't used in the join, don't turn it into a model instance.
    // NOTE: Not sure if this is correct or not?
    if (!usedInJoin.used) return;
 
    // If the attribute is an array of child values, for each one make a model out of it.
    Eif (Array.isArray(value[key])) {
 
      var records = [];
 
      value[key].forEach(function(val) {
        var collection,
            model;
 
        // If there is a joinKey this means it's a belongsTo association so the collection
        // containing the proper model will be the name of the joinKey model.
        if (joinKey) {
          collection = self.collections[joinKey];
          val = collection._transformer.unserialize(val);
          model = new collection._model(val, { showJoins: false });
          return records.push(model);
        }
 
        // Otherwise look at the join used and determine which key should be used to get
        // the proper model from the collections.
        collection = self.collections[usedInJoin.join.child];
        val = collection._transformer.unserialize(val);
        model = new collection._model(val, { showJoins: false });
        return records.push(model);
      });
 
      // Set the value to the array of model values
      value[key] = records;
      return;
    }
    if (!_.isNull(value[key])) {
      if (_.isObject(value[key])) {
        // If the value isn't an array it's a populated foreign key so modelize it and attach
        // it directly on the attribute
        collection = self.collections[joinKey];
        value[key] = collection._transformer.unserialize(value[key]);
        value[key] = new collection._model(value[key], { showJoins: false });
      } else {
        value[key] = null;
      }
    }
  });
 
  return value;
};
 
/**
 * Test if an attribute was used in a join.
 * Requires generating a key to test against an attribute because the model process
 * will be run before any transformations have taken place.
 *
 * @param {String} key
 * @return {Object}
 * @api private
 */
 
Joins.prototype.checkForJoin = function checkForJoin(key) {
 
  var generatedKey;
  var usedInJoin = false;
  var relatedJoin;
 
  // Loop through each join and see if the given key matches a join used
  this.joins.forEach(function(join) {
    if (join.alias !== key) return;
    usedInJoin = true;
    relatedJoin = join;
  });
 
  return { used: usedInJoin, join: relatedJoin };
};