0

In our app, we have a large document that is the source of most of our data for our REST api.

In order to properly invalidate our client-side cache for the REST api, i want to keep track of any modifications made to teh document. The best we came up with is to extend the mongo save command for the document to send off the notification (and then save as usual).

The question is, how does one actually do this in practice? Is there a direct way to extend mongo's "save" method, or should we create a custom method (i.e. "saveAndNotify") on the model that we use instead (which i would avoid if i can)?

[edit] So in principle, i am looking to do this, but am having an issue not clobbering the parent save function();

mySchema.methods.save = function() {
  // notify some stuff
  ...
  //call mongo save function
  return this.super.save();
};
Zak Kus
  • 1,503
  • 3
  • 15
  • 28

1 Answers1

0

Monkey patching the core mongo object is a bad idea, however it turns out mogoose has a middleware concept that will handle this just fine:

var schema = new Schema(..);
schema.pre('save', function (next) {
  // do stuff
  next();
});
Zak Kus
  • 1,503
  • 3
  • 15
  • 28