all files / lib/offshore/query/finders/ dynamicFinders.js

95.83% Statements 46/48
93.1% Branches 27/29
100% Functions 6/6
100% Lines 46/46
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                              410×     410×   2230× 174×     2056×     2056×     2056× 2052× 18468×   2052× 6156×                                   24624× 24624×     24624×     24624×     24624×   48×     48×   48×   48× 48×     48× 48×   48×                                                   11×                                                                            
/**
 * Dynamic Queries
 *
 * Query the collection using the name of the attribute directly
 */
 
var _ = require('lodash');
var usageError = require('../../utils/usageError');
var utils = require('../../utils/helpers');
var normalize = require('../../utils/normalize');
var hasOwnProperty = utils.object.hasOwnProperty;
 
var finder = module.exports = {};
 
/**
 * buildDynamicFinders
 *
 * Attaches shorthand dynamic methods to the prototype for each attribute
 * in the schema.
 */
 
finder.buildDynamicFinders = function() {
  var self = this;
 
  // For each defined attribute, create a dynamic finder function
  Object.keys(this._attributes).forEach(function(attrName) {
    // Check if attribute is an association, if so generate dynamic finders
    if ((self._attributes[attrName].model && self.associationFinders === false) || self._attributes[attrName].collection) {
      return;
    }
 
    var capitalizedMethods = ['findOneBy*', 'findOneBy*In', 'findOneBy*Like', 'findBy*', 'findBy*In',
      'findBy*Like', 'countBy*', 'countBy*In', 'countBy*Like'];
 
    var lowercasedMethods = ['*StartsWith', '*Contains', '*EndsWith'];
 
 
    if (self.dynamicFinders !== false) {
      capitalizedMethods.forEach(function(method) {
        self.generateDynamicFinder(attrName, method);
      });
      lowercasedMethods.forEach(function(method) {
        self.generateDynamicFinder(attrName, method, true);
      });
    }
  });
};
 
 
/**
 * generateDynamicFinder
 *
 * Creates a dynamic method based off the schema. Used for shortcuts for various
 * methods where a criteria object can automatically be built.
 *
 * @param {String} attrName
 * @param {String} method
 * @param {Boolean} dont capitalize the attrName or do, defaults to false
 */
 
finder.generateDynamicFinder = function(attrName, method, dontCapitalize) {
  var self = this;
  var criteria;
 
  // Capitalize Attribute Name for camelCase
  var preparedAttrName = dontCapitalize ? attrName : utils.capitalize(attrName);
 
  // Figure out actual dynamic method name by injecting attribute name
  var actualMethodName = method.replace(/\*/g, preparedAttrName);
 
  // Assign this finder to the collection
  this[actualMethodName] = function dynamicMethod(value, options, cb) {
 
    if (typeof options === 'function') {
      cb = options;
      options = null;
    }
 
    options = options || {};
 
    var usage = utils.capitalize(self.identity) + '.' + actualMethodName + '(someValue,[options],callback)';
 
    Iif (typeof value === 'undefined') return usageError('No value specified!', usage, cb);
    Iif (options.where) return usageError('Cannot specify `where` option in a dynamic ' + method + '*() query!', usage, cb);
 
    // Build criteria query and submit it
    options.where = {};
    options.where[attrName] = value;
 
    switch (method) {
 
 
      ///////////////////////////////////////
      // Finders
      ///////////////////////////////////////
 
 
      case 'findOneBy*':
      case 'findOneBy*In':
        return self.findOne(options, cb);
 
      case 'findOneBy*Like':
        criteria = _.extend(options, {
          where: {
            like: options.where
          }
        });
 
        return self.findOne(criteria, cb);
 
 
      ///////////////////////////////////////
      // Aggregate Finders
      ///////////////////////////////////////
 
 
      case 'findBy*':
      case 'findBy*In':
        return self.find(options, cb);
 
      case 'findBy*Like':
        criteria = _.extend(options, {
          where: {
            like: options.where
          }
        });
 
        return self.find(criteria, cb);
 
 
      ///////////////////////////////////////
      // Count Finders
      ///////////////////////////////////////
 
 
      case 'countBy*':
      case 'countBy*In':
        return self.count(options, cb);
 
      case 'countBy*Like':
        criteria = _.extend(options, {
          where: {
            like: options.where
          }
        });
 
        return self.count(criteria, cb);
 
 
      ///////////////////////////////////////
      // Searchers
      ///////////////////////////////////////
 
      case '*StartsWith':
        return self.startsWith(options, cb);
 
      case '*Contains':
        return self.contains(options, cb);
 
      case '*EndsWith':
        return self.endsWith(options, cb);
    }
  };
};