Installations
npm install mongoose-rollback
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
1.8.1
NPM Version
2.8.3
Score
53.6
Supply Chain
97.4
Quality
72.8
Maintenance
25
Vulnerability
98.2
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (100%)
Developer
Snkz
Download Statistics
Total Downloads
15,874
Last Day
1
Last Week
4
Last Month
42
Last Year
272
GitHub Statistics
15 Stars
45 Commits
8 Forks
3 Watching
1 Branches
1 Contributors
Package Meta Information
Latest Version
1.1.4
Package Id
mongoose-rollback@1.1.4
Size
6.41 kB
NPM Version
2.8.3
Node Version
1.8.1
Total Downloads
Cumulative downloads
Total Downloads
15,874
Last day
0%
1
Compared to previous day
Last week
300%
4
Compared to previous week
Last month
500%
42
Compared to previous month
Last year
-20.2%
272
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Mongoose Rollback machine
Overview
Mongoose-Rollback adds rollback abilities to a Model.
Look up previous document revisions as well as rollback\revert to any previous version.
Requires _id field to exist (true by default) Has not been tested on custom _id fields as of yet. (i.e should be object id);
A new field _version is added to the model, this can be ignored safely. An index can be set for this in the plugin options.
The __v field is used by mongoose to help with concurrent updates, this is not altered in anyway.
The rollback model is stored in a seperate collection, the name is specified in the plugin options, _hist is appened to the name. It is recommended you use the same collection name as the Schema you intend to keep history for.
Make sure to supply your connection name as an option (conn: mongodb://host/db} to the options field if you intend to use a seperate connection!.
DELETES CASCADE! Delete your model => history is removed. I trust that you know what youre doing.
Example Setup
1 2var mongoose = require('mongoose'); 3var rollback = require('mongoose-rollback'); 4 5var Schema = mongoose.Schema; 6var ModelSchema = new Schema({ 7 name: { 8 type: String, 9 required: true 10 }, 11 12 createdAt: { 13 type: Date, 14 default: Date.now 15 }, 16 17 data: { 18 type: String 19 } 20 } 21); 22 23ModelSchema.plugin(rollback, { 24 index: true, // index on _version field 25 conn: 'mongodb://localhost/test', // required if connection isn't explict 26 collectionName: 'model_collection' // your collection name or custom collection 27}); 28 29var Model; 30 31if (mongoose.models.Model) { 32 Model = mongoose.model('Model'); 33} else { 34 Model = mongoose.model('Model', ModelSchema, 'model_collection'); 35} 36 37exports.Model = Model;
Example usage
1describe('Rollback Hell', function(done) { 2 3 it('should rollback to PREVIOUS version', function(done) { 4 var name = 'Hello'; 5 var data = 'World'; 6 var model = new Model({name: name, data: data}); 7 model.save(function(err, model) { 8 if (err) throw (err); 9 10 model.should.have.property('_version'); 11 model._version.should.be.exactly(0); 12 model.name = "Hey"; 13 model.data = "Yo"; 14 15 model.save(function(err, model) { 16 if (err) throw (err); 17 18 model.should.have.property('_version'); 19 model._version.should.be.exactly(1); 20 21 model.rollback(0, function(err, hist) { 22 if (err) throw (err); 23 hist.name.should.match(name); 24 hist.data.should.match(data); 25 hist._version.should.equal(2); 26 27 model.name.should.match(name); 28 model.data.should.match(data); 29 model._version.should.equal(2); 30 done(); 31 }); 32 }); 33 }); 34 35 }); 36});
API
Methods
These extensions happen on instances of the model for convenience. Callbacks take the same arguments Mongoose's save does.
1model.rollback(version_num, callback(err, model))
Rollsback model to version specified by version_num. Returns error if version is greater then models current version (if version supplied in model) and if version number doesnt exist. Note: This is considered an 'update' i.e the version number is incremented and the model is updated with data from a previous revision.
1model.revert(version_num, callback(err, model))
Reverts model to version specified by version_num. Returns error if version is greater then models current version (if version supplied in model) and if version number doesnt exist. Note: This is a destructive update i.e the version number is set to the supplied version and the model is updated with data from a previous revision. History after this version number is lost.
1model.getVersion(version_num, callback(err, model))
Returns model at revision version_num Errors version does not exist. Creates a version 0 if neccassary;
1Model.currentVersion()
Returns the current version number, this is different the checking the _version field as it queries the history model instead. Can help with concurrent updates with outdated copies of a model.
1model.history(min_version=0, max_version=current_version, callback(err, model_array))
Returns history of model changes between specified version number. Creates a version 0 if neccassary. Pass in values (0, BIG) to get all of your history;
Schema
Options for initing the plugin are pretty self-explanatory.
1Schema.plugin({ 2 conn: seperate_mongo_location, 3 index: boolean, 4 collectionName: mongo_collection_name 5}); 6 7Schema.RollbackModel
The history model can be directly accessed from your Schema. It is added as a static variable called RollbackModel.
Statics
These functions are accessed through your Schema.
1Schema.wipeHistory(callback);
Nuke the rollback collection, there is no going back. Callback takes the same arguments mongoose's remove does.
1Schema.initHistory(id, callback(err, model_array))
For models that have been inserted outside of mongoose's save methods. This will save it without updating the version number. This is also done on calls to .getVersion(0, callback); and .history(0,max, callback); internally on existing models that have no history. Do this only if you care about preserving the current version of the model. Future updates will be recorded just fine with or without this call to init.
Coming Soon!
Handle concurrency, (see below)
Mark models to prevent history updates (can be toggled on the fly). Allow for storage of diffs along with full model. Remove requirement to add in collection name removed.
About concurrency
Concurrent saves currently work like they do in mongo, latest update wins. Initial commits to the hist model are also kind of iffy cause of _id collisions. Avoid initing history for an existing model concurrently.
Rollbacks have not been tested concurrently, however they are expected to work like save. Last rollback is the one that will happen. Confirm version value after a rollback incase you expect to do such a thing often.
The following three ops CAN create new hist models: save getVersion history
Try not to do anything concurrently on models that haven't been init'd through mongoose's save. Updates after are A-OK.
Reverts however are dangerous, I will not make any promises. I will document expected behaviour after more testing. Either way, it is best to not revert willy-nilly. That should be a privileged operation!
About Mongoose
Mongoose does not hook into findByIdUpdate/remove mongo methods. Those bypass any middleware by design, including this rollback system. In order to keep history correctly make sure to use this pattern for updates instead.
1Model.find({field: value}, function (err, model) { 2 model.somefield = somevalue; 3 model.save(function (err, updatedModel) { 4 // more code 5 }); 6});
Likewise, deletes should also be done through mongoose. Make sure to do one of the following:
1Model.remove({_id: id}).exec(function(err, num_removed) { 2 // more code 3}); 4 5// OR on a model instance 6 7model.remove(function(err, model) { 8 // more code 9});
They're other mongoose ways to update/delete, they should work fine. Incase an issue does come up, try the above and report it please.
Finally, this is ready to use but being updated very rapidly. Please open issues, report bugs etc and I will get to them.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
license file not detected
Details
- Warn: project does not have a license file
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Score
2.6
/10
Last Scanned on 2024-12-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to mongoose-rollback
mongoose-rollback-2
mongoose rollback machine
mongoose-history-einatec
Mongoose plugin that saves a history of JSON patch operations for all documents belonging to a schema in an associated 'patches' collection
mongoose-version-handler
A Mongoose plugin to manage document versioning and history with JSON Patch diffs, now featuring rollback functionality and full support for NestJS.
@andrew_l/mongo-transaction
![license](https://img.shields.io/npm/l/%40andrew_l%2Fmongo-transaction) <!-- omit in toc --> ![npm version](https://img.shields.io/npm/v/%40andrew_l%2Fmongo-transaction) <!-- omit in toc --> ![npm bundle size](https://img.shields.io/bundlephobia/minzip/%