all files / lib/waterline/collection/ loader.js

78.57% Statements 22/28
58.33% Branches 7/12
100% Functions 5/5
78.57% Lines 22/28
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                            263×     263×     263×   263×                     263×                         263×         263×     263×       263×                     263×                         263×     263× 252×       263×     264×         264×     263×    
/**
 * Module Dependencies
 */
 
var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;
 
/**
 * Collection Loader
 *
 * @param {Object} connections
 * @param {Object} collection
 * @api public
 */
 
var CollectionLoader = module.exports = function(collection, connections, defaults) {
 
  this.defaults = defaults;
 
  // Normalize and validate the collection
  this.collection = this._validate(collection, connections);
 
  // Find the named connections used in the collection
  this.namedConnections = this._getConnections(collection, connections);
 
  return this;
};
 
/**
 * Initalize the collection
 *
 * @param {Object} context
 * @param {Function} callback
 * @api public
 */
 
CollectionLoader.prototype.initialize = function initialize(context) {
  return new this.collection(context, this.namedConnections);
};
 
/**
 * Validate Collection structure.
 *
 * @param {Object} collection
 * @param {Object} connections
 * @api private
 */
 
CollectionLoader.prototype._validate = function _validate(collection, connections) {
 
  // Throw Error if no Tablename/Identity is set
  Iif (!hasOwnProperty(collection.prototype, 'tableName') && !hasOwnProperty(collection.prototype, 'identity')) {
    throw new Error('A tableName or identity property must be set.');
  }
 
  // Ensure identity is lowercased
  collection.prototype.identity = collection.prototype.identity.toLowerCase();
 
  // Set the defaults
  collection.prototype.defaults = this.defaults;
 
  // Find the connections used by this collection
  // If none is specified check if a default connection exist
  Iif (!hasOwnProperty(collection.prototype, 'connection')) {
 
    // Check if a default connection was specified
    if (!hasOwnProperty(connections, 'default')) {
      throw new Error('No adapter was specified for collection: ' + collection.prototype.identity);
    }
 
    // Set the connection as the default
    collection.prototype.connection = 'default';
  }
 
  return collection;
};
 
/**
 * Get the named connections
 *
 * @param {Object} collection
 * @param {Object} connections
 * @api private
 */
 
CollectionLoader.prototype._getConnections = function _getConnections(collection, connections) {
 
  // Hold the used connections
  var usedConnections = {};
 
  // Normalize connection to array
  if (!Array.isArray(collection.prototype.connection)) {
    collection.prototype.connection = [collection.prototype.connection];
  }
 
  // Set the connections used for the adapter
  collection.prototype.connection.forEach(function(conn) {
 
    // Ensure the named connection exist
    Iif (!hasOwnProperty(connections, conn)) {
      var msg = 'The connection ' + conn + ' specified in ' + collection.prototype.identity + ' does not exist!';
      throw new Error(msg);
    }
 
    usedConnections[conn] = connections[conn];
  });
 
  return usedConnections;
};