{"size": 969, "ext": "js", "max_stars_repo_path": "lib/rate.js", "max_stars_repo_name": "emerleite/node-rating", "max_stars_repo_stars_event_min_datetime": "2015-11-05T01:20:48.000Z", "max_stars_repo_stars_event_max_datetime": "2015-11-05T01:20:48.000Z", "max_issues_repo_path": "lib/rate.js", "max_issues_repo_name": "emerleite/node-rating", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/rate.js", "max_forks_repo_name": "emerleite/node-rating", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.0227272727, "max_line_length": 48, "alphanum_fraction": 0.5572755418}
var mongoose = require('mongoose')
  , Rate;

Rate = new mongoose.Schema({
    context: { type: String, index: true}
  , subject: { type: String, index: true}
  , id: { type: String, index: true}
  , date: {type: Date, index:true}
  , total: {type: Number, default: 1}
});
var util = require('util');
Rate.static('rate', function(data, callback) {
    if (!data.date) {
      callback(true);
      return;
    }
    var RateModel = this;
    this.findOne(data, function (err, rate) {
      if (rate) {
        rate.total++;
        rate.save(callback);
      } else {
        rate = new RateModel(data);
        rate.save(callback);
      }
    });
});

Rate.static('total', function (data, callback) {
  this.findOne(data, function (err, rate) { 
      if (rate) {
        callback(null, rate.total.valueOf());   
      } else {
        callback(null, 0);
      }
  });
});

mongoose.model('Rate', Rate);
   
exports.Rate = function(db) {
  return db.model('Rate');
}
