mongoose-version
mongoose-version copied to clipboard
Save vs update
I wasn't able to get a sense from the documentation whether "update" and "findOneAndUpdate" are supported by mongoose-version. I know they have different hooks from standard "save" so can you confirm whether or not those are supported?
Thanks!
I'm currently using this module and i can confirm that this doesn't work on update. The versioning is applied only on pre('save') which is not fired on updates.
Would it be difficult to add such functionality? According to http://mongoosejs.com/docs/middleware.html
Pre and post save() hooks are not executed on update(), findOneAndUpdate(), etc. You can see a more detailed discussion why in this GitHub issue. Mongoose 4.0 has distinct hooks for these functions.
[...]
For instance, if you wanted to add an updatedAt timestamp to every update() call, you would use the following pre hook.
schema.pre('update', function() { this.update({},{ $set: { updatedAt: new Date() } }); });
+1
I may need this kind of functionality as well so I did a little bit of searching/reading.
As @benoror pointed- pre/post hooks for update (and findOneAndUpdate) do not receive the instance but a Query object. In discussion/documentation, they show how to add "updatedAt" field using these hooks.
From what I've gathered, it is (possibly) possible to add the version tracking functionality for update by executing the query within the hook and then replicating the "versioning" functionality for each of the instances returned.
From what I see, this plugin works by adding pre hooks on the schema for 'add' and 'remove'. To add update functionality, I'd suggest something like so:
In array / collection strategies (lib/strategies):
schema.post('update', function(query) {
// we have the query - we can fetch the instances
query.find(function(updatedInstances) {
// here we can update the array/collection
// need to take care of the plumbing etc but something along (assuming collection strategy):
updatedInstance.forEach(function(doc) {
if (!options.suppressVersionIncrement) {
// figure out to increment... maybe:
doc.increment(); // Increment origins version - this won't work without additional doc.save()
doc.save(); // this will trigger the pre-save mechanism, so we could be done
// not sure whether there are other implications...
}
var clone = doc.toObject();
delete clone._id
clone.refVersion = this._doc.__v; // Saves current document version
clone.refId = doc._id; // Sets origins document id as a reference
new schema.statics.VersionedModel(clone).save(function(err) {
if (err) {
debug(err);
} else {
debug('Created versioned model in mongodb');
}
});
});
});
});
Note - I believe the post hook is better because it is executed at the end and doesn't receive flow control so it will not slow the process .
Of course, need to figure out how to set the refVersion property properly without causing havoc... Need to think this through...
Note: this is all conceptual and I didn't even try to run it (yet). I posted here to receive comments/suggestions. Is this repository still active? Anyway one the authors is around to comment/assist/merge? .
Any updates? This feature is the major use-case.