<commit_before>/*
 * MODEL
 */
exports.model = {
  count: function(field){
    var self = this.chain();  
    self.setInternal('count', field);  
    return self;
  },
  
  
  sum: function(field){
    var self = this.chain();  
    self.setInternal('sum', field);  
    return self;
  },
  
  
  max: function(field){
    var self = this.chain();  
    self.setInternal('max', field);  
    return self;
  },
  
  
  min: function(field){
    var self = this.chain();  
    self.setInternal('min', field);  
    return self;
  },
  
  /* //Not yet supported by knex (?)
  avg: function(field){
    var self = this.chain();  
    self.setInternal('avg', field);  
    return self;
  }
  */
};



/*
 * DEFINITION
 */
exports.definition = {
  mixinCallback: function(){
    var self = this;    
    
    this.beforeFind(function(query){            
      var agg_fns = ['count', 'sum', 'min', 'max']; //, 'avg'];
      
      for(var i in agg_fns){
        var tmp = this.getInternal(agg_fns[i]);
        if(tmp){
          query[agg_fns[i]].call(query, tmp + ' as ' + agg_fns[i]);
        }
      }
                  
      return true;
    });
    
    
    
    this.afterFind(function(data){
      var count = this.getInternal('count');
      var sum = this.getInternal('sum');
      var min = this.getInternal('min');
      var max = this.getInternal('max');
      var avg = this.getInternal('avg');
      
      if((count || sum || min || max || avg) && data.result.length == 1){
        data.result = data.result[0];
      }
      
      return true;
    }, 50);
    
  }
};<commit_msg>use asJson() on aggregate functions<commit_after>/*
 * MODEL
 */
exports.model = {
  count: function(field){
    var self = this.chain();  
    self.setInternal('count', field);  
    return self;
  },
  
  
  sum: function(field){
    var self = this.chain();  
    self.setInternal('sum', field);  
    return self;
  },
  
  
  max: function(field){
    var self = this.chain();  
    self.setInternal('max', field);  
    return self;
  },
  
  
  min: function(field){
    var self = this.chain();  
    self.setInternal('min', field);  
    return self;
  },
  
  /* //Not yet supported by knex (?)
  avg: function(field){
    var self = this.chain();  
    self.setInternal('avg', field);  
    return self;
  }
  */
};



/*
 * DEFINITION
 */
exports.definition = {
  mixinCallback: function(){
    var self = this;    
    
    this.beforeFind(function(query){            
      var agg_fns = ['count', 'sum', 'min', 'max']; //, 'avg'];
      
      for(var i in agg_fns){
        var tmp = this.getInternal(agg_fns[i]);
        if(tmp){
          query[agg_fns[i]].call(query, tmp + ' as ' + agg_fns[i]);
        }
      }
                  
      return true;
    });
    
    
    
    this.afterFind(function(data){
      var count = this.getInternal('count');
      var sum = this.getInternal('sum');
      var min = this.getInternal('min');
      var max = this.getInternal('max');
      var avg = this.getInternal('avg');
      
      if((count || sum || min || max || avg) && data.result.length == 1){
        this.asJson();
        data.result = data.result[0];
      }
      
      return true;
    }, 60);
    
  }
};
{'fraction_non_alphanumeric': 0.16599566429235058, 'fraction_numerical': 0.0024775472282440383, 'mean_word_length': 1.9230769230769231, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1672352', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1079, 'n_words': 225}