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

77.78% Statements 21/27
64.29% Branches 9/14
100% Functions 4/4
77.78% Lines 21/27
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                            320×     320× 320×                     320×                         320×         320×     320×       320×             320× 308×     320× 320×   321×     321×       321×     320×   320×    
/**
 * 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);
  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.connections);
};
 
/**
 * 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')) {
    // Set the connection as the default
    collection.prototype.connection = 'default';
    return collection;
  }
 
  // use first connection in queries
  if (!Array.isArray(collection.prototype.connection)) {
    collection.prototype.connection = [collection.prototype.connection];
  }
 
  var usedConnections = {};
  collection.prototype.connection.forEach(function(connectionName) {
    // create default connection if not exist
    Iif (!connections[connectionName] && connectionName === 'default') {
      connections.default = {};
    }
    Iif (!connections[connectionName]) {
      var msg = 'The connection ' + connectionName + ' specified in ' + collection.prototype.identity + ' does not exist!';
      throw new Error(msg);
    }
    usedConnections[connectionName] = connections[connectionName];
  });
 
  this.connections = usedConnections;
 
  return collection;
};