all files / lib/offshore/query/dql/ count.js

80% Statements 20/25
81.25% Branches 13/16
100% Functions 1/1
80% Lines 20/25
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                            24×   24×           24× 13× 13×       24×     24× 10×         14×       14×     14×         14×   14×    
/**
 * Module Dependencies
 */
 
var _ = require('lodash');
var usageError = require('../../utils/usageError');
var utils = require('../../utils/helpers');
var normalize = require('../../utils/normalize');
var Deferred = require('../deferred');
 
/**
 * Count of Records
 *
 * @param {Object} criteria
 * @param {Object} options
 * @param {Function} callback
 * @return Deferred object if no callback
 */
 
module.exports = function(criteria, options, cb, metaContainer) {
  var usage = utils.capitalize(this.identity) + '.count([criteria],[options],callback)';
 
  Iif (typeof criteria === 'function') {
    cb = criteria;
    criteria = null;
    options = null;
  }
 
  if (typeof options === 'function') {
    cb = options;
    options = null;
  }
 
  // Normalize criteria and fold in options
  criteria = normalize.criteria(criteria);
 
  // Return Deferred or pass to adapter
  if (typeof cb !== 'function') {
    return new Deferred(this, this.count, criteria);
  }
 
  // If there was something defined in the criteria that would return no results, don't even
  // run the query and just return 0
  Iif (criteria === false) {
    return cb(null, 0);
  }
 
  if (_.isObject(options) && _.isObject(criteria)) {
    criteria = _.extend({}, criteria, options);
  }
 
  Iif (_.isFunction(criteria) || _.isFunction(options)) {
    return usageError('Invalid options specified!', usage, cb);
  }
 
  // Transform Search Criteria
  criteria = this._transformer.serialize(criteria);
 
  this.adapter.count(criteria, cb, metaContainer);
};