mongoose-old-values

1.0.3 • Public • Published

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 :

// Globally
const mongoose = require('mongoose');
mongoose.plugin(require('mongoose-old-values'));
 
// Singularly
const TestSchema = new Schema({
    a: {},
    b: { c: {} },
    ...
 });
TestSchema.plugin(require('mongoose-old-values'));
const TestModel = mongoose.model('Test', TestSchema);

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.findById(...);
 
// Any edit you want
document.a = ...;
document.a.b = ...;
 
...
 
// Get the old props
 
const oldA = document.$locals.old.get('a');
const oldAB = document.$locals.old.get('a.b');

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.

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i mongoose-old-values

    Weekly Downloads

    4

    Version

    1.0.3

    License

    MIT

    Unpacked Size

    4.3 kB

    Total Files

    4

    Last publish

    Collaborators

    • exsoax