I’m trying to implement a way of creating a record (Update) when changes are made in a MongoDB document, via Mongoose/Node.js/Express. I’m new to Node.js so I was wondering what the best way would be of doing this.
I imagine the record to be something like:
update: {
before: { ... },
after: { ... },
created_at: Date,
};
I know it’s possible to set up a pre function that gets called when another method is called, e.g. save.
Entry.pre('save', function(next) {
// create an Update record
next();
});
The problem is, I’m not sure how to determine what was changed in the save operation, so it’s hard to create a list of updated values.
Can anyone think of a clean way to do this?
You might want to look at the Model.js source. There’s two functions in the model prototype that should help you. _dirty() and _delta()