mongoose-historise
Keep track of modifications history of your documents!
Install & Test
$> npm i mongoose-historise
$> npm test
Why mongoose-historise?
Historise helps you keeping track of modifications within a document of any of your collections.
Unlike several Mongoose versioning plugins which duplicates a document to store it as an archive when it is updated, mongoose-historise stores a history of the modified fields directly within the document itself.
Example of a new "Movie" document:
{
_id: 6055282e4992b599b0874155,
castOverview: [ 'Daniel Radcliffe', 'Rupert Grint', 'Richard Harris' ],
title: "Harry Potter and the Sorcerer's Stone",
duration: '2h32min',
director: 'Chris Columbus',
summary: 'An orphaned boy enrolls in a school of wizardry, where he learns the truth about himself, his family and the terrible evil that haunts the magical world.',
releaseDate: 2001-11-16T00:00:00.000Z,
history: [],
__v: 0
}
After updating some fields (e.g. 'duration' and 'castOverview') and saving the document, the 'history' is automatically updated as follows:
{
_id: 6055282e4992b599b0874155,
castOverview: [ 'Daniel Radcliffe', 'Rupert Grint', 'Richard Harris', 'Maggie Smith' ],
title: "Harry Potter and the Sorcerer's Stone",
duration: '2:32',
director: 'Chris Columbus',
summary: 'An orphaned boy enrolls in a school of wizardry, where he learns the truth about himself, his family and the terrible evil that haunts the magical world.',
releaseDate: 2001-11-16T00:00:00.000Z,
history: [
{
timestamp: 2021-03-19T22:40:30.499Z,
modifications: [
{ field: 'duration', oldValue: '2h32min', newValue: '2:32' },
{
field: 'castOverview',
oldValue: [ 'Daniel Radcliffe', 'Rupert Grint', 'Richard Harris' ],
newValue: [ 'Daniel Radcliffe', 'Rupert Grint', 'Richard Harris', 'Maggie Smith' ]
}
]
}
],
__v: 1
}
Quick Start
WARNING: mongoose-historise can generate history only on 'save' operation due to Mongoose limitations. If you want to use it, please use 'save' instead of 'update' operations to properly historise modifications made to your documents.
When creating your Mongoose Model, simply add mongoose-historise plugin (more options available in the section below):
const mongoose = require("mongoose");
const historise = require("mongoose-historise");
const schema = new mongoose.Schema({
title: String,
duration: String,
director: String,
castOverview: [String],
summary: String,
releaseDate: Date,
});
schema.plugin(historise, { mongooseInstance: mongoose, mongooseModelName: "Movie", });
const Movie = mongoose.model("Movie", schema);
module.exports = Movie;
Options
Currently, the following options are available for mongoose-historise (default values indicated, only 'mongooseInstance' and 'mongooseModelName' are required):
{
mongooseInstance: mongoose,
mongooseModelName: "Model",
fieldnames: {
history: "history",
modifications: "modifications",
timestamp: "timestamp",
field: "field",
oldValue: "oldValue",
newValue: "newValue"
},
limit: false,
order: -1,
deepComparison: true,
ignore: ['createdAt', '__v', 'history']
}
History structure
The history of a document is automatically generated based on fields that have been modified when saving a document. This plugin adds the following structure to your Mongoose Model:
{
history: [
{
timestamp: Date
modifications: [
{
field: String,
oldValue: Mixed,
newValue: Mixed
},
...
]
},
...
]
}
You can edit the name of all these history structure's fields using the options above.
Issues & Features requests
If you encounter any bug/issue or would like new features to be added please feel free to open an issue on GitHub.