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

91.18% Statements 62/68
86.11% Branches 31/36
83.33% Functions 5/6
91.04% Lines 61/67
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                                                                410× 410×   410×                     410×     410× 2230× 2057×       410×             410×                         2057×   2057× 4855×         1918× 1918×       829× 829×       403× 403× 403×       38× 38×       36× 36×       36× 36×       36× 36×       344× 344× 344×       313× 313×                                       137× 137×   137× 362× 137× 137×       137× 137× 137× 137× 137×       2057×                                     1847×   1847×       7803×     7579×         1847×    
/**
 * Module dependencies
 */
 
var _ = require('lodash');
var types = require('../utils/types');
var utils = require('../utils/helpers');
var hasOwnProperty = utils.object.hasOwnProperty;
 
/**
 * Builds a Schema Object from an attributes
 * object in a model.
 *
 * Loops through an attributes object to build a schema
 * containing attribute name as key and a type for casting
 * in the database. Also includes a default value if supplied.
 *
 * Example:
 *
 * attributes: {
 *   name: 'string',
 *   phone: {
 *     type: 'string',
 *     defaultsTo: '555-555-5555'
 *   }
 * }
 *
 * Returns: {
 *   name: { type: 'string' },
 *   phone: { type: 'string, defaultsTo: '555-555-5555' }
 * }
 *
 * @param {Object} context
 * @return {Object}
 */
 
var Schema = module.exports = function(context) {
  this.context = context || {};
  this.schema = {};
 
  return this;
};
 
/**
 * Initialize the internal schema object
 *
 * @param {Object} attrs
 * @param {Object} associations
 * @param {Boolean} hasSchema
 */
 
Schema.prototype.initialize = function(attrs, hasSchema, reservedAttributes) {
  var self = this;
 
  // Build normal attributes
  Object.keys(attrs).forEach(function(key) {
    if (hasOwnProperty(attrs[key], 'collection')) return;
    self.schema[key] = self.objectAttribute(key, attrs[key]);
  });
 
  // Build Reserved Attributes
  Iif (Array.isArray(reservedAttributes)) {
    reservedAttributes.forEach(function(key) {
      self.schema[key] = {};
    });
  }
 
  // Set hasSchema to determine if values should be cleansed or not
  this.hasSchema = typeof hasSchema !== 'undefined' ? hasSchema : true;
};
 
/**
 * Handle the building of an Object attribute
 *
 * Cleans any unnecessary attributes such as validation properties off of
 * the internal schema and set's defaults for incorrect values.
 *
 * @param {Object} value
 * @return {Object}
 */
 
Schema.prototype.objectAttribute = function(attrName, value) {
  var attr = {};
 
  for (var key in value) {
    switch (key) {
 
      // Set schema[attribute].type
      case 'type':
        // Allow validation types in attributes and transform them to strings
        attr.type = ~types.indexOf(value[key]) ? value[key] : 'string';
        break;
 
      // Set schema[attribute].defaultsTo
      case 'defaultsTo':
        attr.defaultsTo = value[key];
        break;
 
      // Set schema[attribute].primaryKey
      case 'primaryKey':
        attr.primaryKey = value[key];
        attr.unique = true;
        break;
 
      // Set schema[attribute].foreignKey
      case 'foreignKey':
        attr.foreignKey = value[key];
        break;
 
      // Set schema[attribute].references
      case 'references':
        attr.references = value[key];
        break;
 
      // Set schema[attribute].on
      case 'on':
        attr.on = value[key];
        break;
 
      // Set schema[attribute].via
      case 'via':
        attr.via = value[key];
        break;
 
      // Set schema[attribute].autoIncrement
      case 'autoIncrement':
        attr.autoIncrement = value[key];
        attr.type = 'integer';
        break;
 
      // Set schema[attribute].unique
      case 'unique':
        attr.unique = value[key];
        break;
 
      // Set schema[attribute].index
      case 'index':
        attr.index = value[key];
        break;
 
      // Set schema[attribute].enum
      case 'enum':
        attr.enum = value[key];
        break;
 
      // Set schema[attribute].size
      case 'size':
        attr.size = value[key];
        break;
 
      // Set schema[attribute].notNull
      case 'notNull':
        attr.notNull = value[key];
        break;
 
      // Handle Belongs To Attributes
      case 'model':
        var type;
        var attrs = this.context.offshore.schema[value[key].toLowerCase()].attributes;
 
        for (var attribute in attrs) {
          if (hasOwnProperty(attrs[attribute], 'primaryKey') && attrs[attribute].primaryKey) {
            type = attrs[attribute].type;
            break;
          }
        }
 
        attr.type = type.toLowerCase();
        attr.model = value[key].toLowerCase();
        attr.foreignKey = true;
        attr.alias = attrName;
        break;
    }
  }
 
  return attr;
};
 
 
/**
 * Clean Values
 *
 * Takes user inputted data and strips out any values not defined in
 * the schema.
 *
 * This is run after all the validations and right before being sent to the
 * adapter. This allows you to add temporary properties when doing validation
 * callbacks and have them stripped before being sent to the database.
 *
 * @param {Object} values to clean
 * @return {Object} clone of values, stripped of any extra properties
 */
 
Schema.prototype.cleanValues = function(values) {
 
  var clone = {};
 
  for (var key in values) {
 
    // The value can pass through if either the collection does have a schema and the key is in the schema,
    // or otherwise if the collection is schemaless and the key does not represent an associated collection.
    if ((this.hasSchema && hasOwnProperty(this.schema, key)) ||
        (!this.hasSchema && !(hasOwnProperty(this.context._attributes, key) && hasOwnProperty(this.context._attributes[key], 'collection')))) {
 
      clone[key] = values[key];
    }
 
  }
 
  return clone;
};