Mongoose - instance methods updating the model, is this good practice?

Viewed 3672

I'm using a few mongoose model instance methods in an attempt to encapsulate some business logic, rather than have is spread around my application. An example:

 MySchema.methods.doSomethingAndUpdateCount = function (somedata) {
        //Do something to model here using somedata
        this.someCount ++;
        this.save();
 };

I've been looking at this again today though, and I'm not sure this is really good practice. For one thing, it doesn't really give the part of the application that uses this a way to handle errors. Perhaps the problem is the this.save(); - maybe this should not be in the instance method, but rather called by the consumer of the method. So...

  • Is it good practice to have instance methods that update the instance data?
  • If so, should you call this.save(); from the instance method, or do that in consuming code?
  • How are you supposed to handle errors in this scenario?
1 Answers
Related