{"size": 1760, "ext": "js", "max_stars_repo_path": "Day 2/kittens.js", "max_stars_repo_name": "aravindbaskaran/PECMarch2018", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Day 2/kittens.js", "max_issues_repo_name": "aravindbaskaran/PECMarch2018", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Day 2/kittens.js", "max_forks_repo_name": "aravindbaskaran/PECMarch2018", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.4666666667, "max_line_length": 59, "alphanum_fraction": 0.5948863636}
// TODO Make it a module and refer the methods in index.js
var kittySchema = mongoose.Schema({
  name: String,
  color: String
});
kittySchema.methods.speak = function () {
  var greeting = this.name
    ? "Meow name is " + this.name
    : "I don't have a name";
  greeting = greeting + (
    this.color ? ("My color is " + this.color)
    : ""
  );
  console.log(greeting);
}

var Kitten = mongoose.model('Kitten', kittySchema);

function createFluffy(){
  var silence = new Kitten({ name: 'Silence' });
  console.log(silence.name); // 'Silence'

  var fluffy = new Kitten({ name: 'fluffy' });
  fluffy.speak(); // "Meow name is fluffy"

  fluffy.save(function (err, fluffy) {
    if (err) return console.error(err);
    fluffy.speak();
  });
}

function createTom(){
  var tom = new Kitten({ name: 'tom' });
  tom.speak(); // "Meow name is fluffy"

  tom.save(function (err, tom) {
    if (err) return console.error(err);
    tom.speak();
  });
}
//createTom();

function findKittens() {
  Kitten.find(function (err, kittens) {
    if (err) return console.error(err);
    console.log(kittens);
  });
}

function findFluffy() {
  Kitten.find({ name: 'fluffy' }, function (err, kittens) {
    if (err) return console.error(err);
    console.log('fluffy', kittens);
  });
}

function updateToms() {
  Kitten.find({ name: 'tom' }, function (err, kittens) {
    if (err) return console.error(err);
    console.log('tom', kittens);
    try {
      //for(var i = 0; i < kittens.length; i++)
      kittens.forEach(function(tom) {
        tom.color = 'grey';
        tom.save(function (err, tom) {
          if (err) return console.error(err);
          tom.speak();
        });
      });
    } catch(err) {
      console.log('Error saving toms', err);
    }
  });
}
