Masquerade-Circus / mongoose-history-plugin

Mongoose plugin that saves history in JsonPatch format and SemVer format.
Apache License 2.0
48 stars 25 forks source link

Documents with populated fields are saved with population still in place #6

Closed Evertvdw closed 5 years ago

Evertvdw commented 5 years ago

When you save a populated document in Mongoose the populated field does not actually get saved in the database, but just the _id.

The history plugin does not follow this behaviour but saves the whole populated document in the history collection. Can this be prevented?

Masquerade-Circus commented 5 years ago

I'll be working on this as soon as i can. Nice catch @Evertvdw

Evertvdw commented 5 years ago

I'm not sure what the best approach is here, I think you should be able to do both, I could image that you would want to save the populated documents sometimes. Making it configurable is probably the best option.

Masquerade-Circus commented 5 years ago

I think the default must be to store only the id of the populated field as this is the expected behavior for mongoose. With this in mind, if the referenced object changes it will not trigger the history event for this document, and if you want to store the state of this ref, you will need to add the history plugin to its schema.

And add the configuration option to store the populated field to save its current status in the document in case the populated field changes and has no history plugin attached to it.

Although the last one seems to be more versatile and usable because with the last one you just need to get the verision and it will show all the state with its references.

With the first one you will need to get the state of the document, plus, go and get the sate of every populated field if its needed searching it by the closest timestamp i think.

Masquerade-Circus commented 5 years ago

Implemented in the commit: 2844fe2