Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save vs update #25

Open
wlingke opened this issue Jan 30, 2017 · 5 comments
Open

Save vs update #25

wlingke opened this issue Jan 30, 2017 · 5 comments

Comments

@wlingke
Copy link

wlingke commented Jan 30, 2017

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!

@katp4
Copy link

katp4 commented Apr 26, 2017

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.

@benoror
Copy link

benoror commented Jun 30, 2017

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() } });
});

@rrubio
Copy link

rrubio commented Oct 11, 2017

+1

@tomercagan
Copy link

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? .

@ItayKatzCC
Copy link

Any updates? This feature is the major use-case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants