all files / lib/offshore/core/ transformations.js

96.33% Statements 105/109
93.2% Branches 96/103
100% Functions 17/17
98.04% Lines 100/102
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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253                                  415×     415×   414×                   415× 415×   415×     2242× 141×       2101×         2101×     6096×     709×       708× 708× 631×                             5212× 5212×   5212× 2218×   2218× 17× 31× 16×           2218× 408× 433× 410× 410×           2218× 27× 32× 24×           2218× 19× 22× 18×           2218× 15× 18× 14×           2218× 13× 14× 14×           2218× 13× 22× 18×       2218× 2024×   2218×       2994× 410× 410× 2057× 628× 628×     410×       2584× 2584×       4983×     4983×         4977×     10340×     10340× 10340×     10340×     10308×     343× 152× 152×   152×     191×       9965× 4263× 4263× 4263× 4263×       9965×                           4826× 4826×     4826× 25896×   25896×   11298× 11298×     4826×    
/**
 * Module dependencies
 */
 
var _ = require('lodash');
var utils = require('../utils/helpers');
var hasOwnProperty = utils.object.hasOwnProperty;
 
/**
 * Transformation
 *
 * Allows for a Offshore Collection to have different
 * attributes than what actually exist in an adater's representation.
 *
 * @param {Object} attributes
 * @param {Object} tables
 */
 
var Transformation = module.exports = function(attributes, tables) {
 
  // Hold an internal mapping of keys to transform
  this._transformations = {};
 
  // Initialize
  this.initialize(attributes, tables);
 
  return this;
};
 
/**
 * Initial mapping of transformations.
 *
 * @param {Object} attributes
 * @param {Object} tables
 */
 
Transformation.prototype.initialize = function(attributes, tables) {
  var self = this;
  self.attributes = attributes;
 
  _.keys(attributes).forEach(function(attr) {
 
    // Ignore Functions and Strings
    if (_.isFunction(attributes[attr]) || _.isString(attributes[attr])) {
      return;
    }
 
    // If not an object, ignore
    Iif (!_.isObject(attributes[attr])) {
      return;
    }
 
    // Loop through an attribute and check for transformation keys
    _.keys(attributes[attr]).forEach(function(key) {
 
      // Currently just works with `columnName`, `collection`, `groupKey`
      if (key !== 'columnName') return;
 
      // Error if value is not a string
      if (!_.isString(attributes[attr][key])) {
        throw new Error('columnName transformation must be a string');
      }
 
      // Set transformation attr to new key
      Eif (key === 'columnName') {
        if (attr === attributes[attr][key]) return;
        self._transformations[attr] = attributes[attr][key];
      }
 
    });
  });
};
 
/**
 * Transforms a set of attributes into a representation used
 * in an adapter.
 *
 * @param {Object} attributes to transform
 * @return {Object}
 */
 
Transformation.prototype.serialize = function(attributes, behavior) {
  var self = this;
  var values = _.clone(attributes);
  // Transform criteria
  if (_.isUndefined(behavior) && !_.isUndefined(values.where)) {
    var criteria = values;
    // Transform select
    if (criteria.select && _.isArray(criteria.select)) {
      criteria.select.forEach(function(selector, index) {
        if (self._transformations[selector]) {
          criteria.select[index] = self._transformations[selector];
        }
      });
    }
 
    // Transform sort column name
    if (criteria.sort && _.isObject(criteria.sort)) {
      _.keys(criteria.sort).forEach(function(order) {
        if (self._transformations[order]) {
          criteria.sort[self._transformations[order]] = criteria.sort[order];
          delete criteria.sort[order];
        }
      });
    }
 
    // Transform sum
    if (criteria.sum && _.isArray(criteria.sum)) {
      criteria.sum.forEach(function(sum, index) {
        if (self._transformations[sum]) {
          criteria.sum[index] = self._transformations[sum];
        }
      });
    }
 
    // Transform average
    if (criteria.average && _.isArray(criteria.average)) {
      criteria.average.forEach(function(average, index) {
        if (self._transformations[average]) {
          criteria.average[index] = self._transformations[average];
        }
      });
    }
 
    // Transform min
    if (criteria.min && _.isArray(criteria.min)) {
      criteria.min.forEach(function(min, index) {
        if (self._transformations[min]) {
          criteria.min[index] = self._transformations[min];
        }
      });
    }
 
    // Transform max
    if (criteria.max && _.isArray(criteria.max)) {
      criteria.max.forEach(function(max, index) {
        Eif (self._transformations[max]) {
          criteria.max[index] = self._transformations[max];
        }
      });
    }
 
    // Transform groupBy
    if (criteria.groupBy && _.isArray(criteria.groupBy)) {
      criteria.groupBy.forEach(function(groupBy, index) {
        if (self._transformations[groupBy]) {
          criteria.groupBy[index] = self._transformations[groupBy];
        }
      });
    }
    if (criteria.where) {
      recursiveSerialize(criteria.where);
    }
    return criteria;
  }
 
  // Schema must be serialized in first level only
  if (behavior === 'schema') {
    var schema = values;
    _.keys(schema).forEach(function(property) {
      if (hasOwnProperty(self._transformations, property)) {
        schema[self._transformations[property]] = schema[property];
        delete schema[property];
      }
    });
    return schema;
  }
 
  // Recursivly serialize attributes to handle nested criteria
  recursiveSerialize(values);
  return values;
 
  function recursiveSerialize(obj, parentAttr) {
 
    // Return if no object
    Iif (!obj) return;
 
    // Handle array of types for findOrCreateEach
    if (_.isString(obj)) {
      Eif (hasOwnProperty(self._transformations, obj)) {
        values = self._transformations[obj];
        return;
      }
      return;
    }
 
    _.keys(obj).forEach(function(property) {
 
      // Just a double check to exit if hasOwnProperty fails
      Iif (!hasOwnProperty(obj, property)) return;
 
      // Detect attribute
      parentAttr = self.attributes[property] || self.attributes[self._transformations[property]] || parentAttr;
      var type = parentAttr ? parentAttr.type || parentAttr : null;
 
      // Recursively serialize `OR` and `AND` criteria objects to transform keys
      if (_.isArray(obj[property]) && (property === 'or' || property === 'and')) return recursiveSerialize(obj[property], parentAttr);
 
      // If Nested Object check it's not a json attribute property
      if (type !== 'json' && _.isPlainObject(obj[property])) {
 
        // check if object key is in the transformations
        if (hasOwnProperty(self._transformations, property)) {
          obj[self._transformations[property]] = _.clone(obj[property]);
          delete obj[property];
 
          return recursiveSerialize(obj[self._transformations[property]], parentAttr);
        }
 
        return recursiveSerialize(obj[property], parentAttr);
      }
 
      // Check if property is a transformation key
      if (hasOwnProperty(self._transformations, property)) {
        var value = obj[property];
        delete obj[property];
        obj[self._transformations[property]] = value;
        property = self._transformations[property];
      }
 
      // Cast types
      if (_.isString(obj[property]) && (type === 'date' || type === 'datetime')) {
        obj[property] = new Date(obj[property]);
      }
    });
  }
};
 
/**
 * Transforms a set of attributes received from an adapter
 * into a representation used in a collection.
 *
 * @param {Object} attributes to transform
 * @return {Object}
 */
 
Transformation.prototype.unserialize = function(attributes) {
  var self = this;
  var values = _.clone(attributes);
 
  // Loop through the attributes and change them
  _.keys(this._transformations).forEach(function(key) {
    var transformed = self._transformations[key];
 
    if (!hasOwnProperty(attributes, transformed)) return;
 
    values[key] = attributes[transformed];
    Eif (transformed !== key) delete values[transformed];
  });
 
  return values;
};