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

88.06% Statements 59/67
83.33% Branches 35/42
100% Functions 9/9
91.38% Lines 53/58
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                                  271×     271×   270×                   271×   271×     1242×     1015×     1015×     3013×     116×       115× 115× 56×                             2781× 2781×   2781×       3880×     3880×             3874×     9243×     9243× 1169× 53× 53×   1169×       8074×     8068×     1093×             1093×       6975×   216× 216×           2781×   2781×                     2447× 2447×     2447× 1185×   1185×   432× 432×     2447×    
/**
 * Module dependencies
 */
 
var _ = require('lodash');
var utils = require('../utils/helpers');
var hasOwnProperty = utils.object.hasOwnProperty;
 
/**
 * Transformation
 *
 * Allows for a Waterline 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;
 
  Object.keys(attributes).forEach(function(attr) {
 
    // Ignore Functions and Strings
    if (['function', 'string'].indexOf(typeof attributes[attr]) > -1) return;
 
    // If not an object, ignore
    Iif (attributes[attr] !== Object(attributes[attr])) return;
 
    // Loop through an attribute and check for transformation keys
    Object.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 (typeof attributes[attr][key] !== 'string') {
        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);
 
  behavior = behavior || 'default';
 
  function recursiveParse(obj) {
 
    // Return if no object
    Iif (!obj) return;
 
    // Handle array of types for findOrCreateEach
    if (typeof obj === 'string') {
      Iif (hasOwnProperty(self._transformations, obj)) {
        values = self._transformations[obj];
        return;
      }
 
      return;
    }
 
    Object.keys(obj).forEach(function(property) {
 
      // Just a double check to exit if hasOwnProperty fails
      Iif (!hasOwnProperty(obj, property)) return;
 
      // Schema must be serialized in first level only
      if (behavior === 'schema') {
        if (hasOwnProperty(self._transformations, property)) {
          obj[self._transformations[property]] = _.clone(obj[property]);
          delete obj[property];
        }
        return;
      }
 
      // Recursively parse `OR` criteria objects to transform keys
      if (Array.isArray(obj[property]) && property === 'or') return recursiveParse(obj[property]);
 
      // If Nested Object call function again passing the property as obj
      if ((toString.call(obj[property]) !== '[object Date]') && (_.isPlainObject(obj[property]))) {
 
        // check if object key is in the transformations
        Iif (hasOwnProperty(self._transformations, property)) {
          obj[self._transformations[property]] = _.clone(obj[property]);
          delete obj[property];
 
          return recursiveParse(obj[self._transformations[property]]);
        }
 
        return recursiveParse(obj[property]);
      }
 
      // Check if property is a transformation key
      if (hasOwnProperty(self._transformations, property)) {
 
        obj[self._transformations[property]] = obj[property];
        delete obj[property];
      }
    });
  }
 
  // Recursivly parse attributes to handle nested criteria
  recursiveParse(values);
 
  return values;
};
 
/**
 * 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
  Object.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;
};