Package: mongoose-old-values
Package for mongoose useful for storing the old values of a model's document.
Install
npm i mongoose-old-values
Usage
Just apply as plugin on your schema or globally :
// Globallyconst mongoose = ;mongoose; // Singularlyconst TestSchema = a: {} b: c: {} ... ;TestSchema;const TestModel = mongoose;
It will create a new post init hook and a new post save hook, storing the old values (copying them with one of the fastest library available fast-copy).
After the application of this plugin you will notice this behaviour:
const document = await TestModel; // Any edit you wantdocumenta = ...;documentab = ...; ... // Get the old props const oldA = document$localsold;const oldAB = document$localsold;
Just use document.$locals.old.get(path) to access the old value
Support
If you would like to support my work, please buy me a coffe ☕. Thanks in advice.