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

100% Statements 13/13
100% Branches 4/4
100% Functions 6/6
100% Lines 13/13
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                                 266× 266×                   266× 267×   267× 1210×                             266× 264×     266×    
var _ = require('lodash');
 
/**
 * Handle Building an Adapter/Connection dictionary
 *
 * @param {Object} connections
 * @param {Array} ordered
 * @return {Object}
 * @api public
 *
 * Manages a 'dictionary' object of the following structure:
 * {
 *    CONNECTION: {
 *      METHOD: ADAPTER_NAME
 *    }
 * }
 */
var Dictionary = module.exports = function(connections, ordered) {
  this.dictionary = this._build(connections);
  return this._smash(ordered);
};
 
/**
 * Build Dictionary. This maps adapter methods to the effective connection
 * for which the method is pertinent.
 *
 * @param {Object} connections
 * @api private
 */
Dictionary.prototype._build = function _build(connections) {
  return _.mapValues(connections, function(connection, connectionName) {
    var adapter = connection._adapter || { };
 
    return _.mapValues(adapter, function(method) {
      return connectionName;
    });
  });
};
 
/**
 * Combine Dictionary into a single level object.
 *
 * Appends methods from other adapters onto the left most connection adapter,
 * but does not override any existing methods defined in the leftmost adapter.
 *
 * @param {Array} ordered
 * @return {Object}
 * @api private
 */
Dictionary.prototype._smash = function _smash(ordered) {
  var mergeArguments = _.map((ordered || [ ]).reverse(), function(adapterName) {
    return this.dictionary[adapterName];
  }, this);
 
  return _.merge.apply(null, mergeArguments);
};