From efd5107f604801b6770dfd42334c35c3f504af98 Mon Sep 17 00:00:00 2001 From: Abdi Dahir Date: Mon, 10 Nov 2014 23:50:30 -0500 Subject: [PATCH] Update README.md --- README.md | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 60e0abd..8846891 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Make sure to supply your connection name as an option (conn: mongodb://host/db} DELETES CASCADE! Delete your model => history is removed. I trust that you know what youre doing. -### Example Setup +#### Example Setup ```javascript var mongoose = require('mongoose'); @@ -61,7 +61,7 @@ if (mongoose.models.Model) { exports.Model = Model; ``` -### Example usage +#### Example usage ```javascript describe('Rollback Hell', function(done) { @@ -101,19 +101,20 @@ describe('Rollback Hell', function(done) { }); ``` ## API +#### Methods These extensions happen on instances of the model for convenience. Callbacks take the same arguments Mongoose's save does. ```javascript -Model.rollback(version_num, callback(err, model)) +model.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. ```javascript -Model.revert(version_num, callback(err, model)) +model.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. ```javascript -Model.getVersion(version_num, callback(err, model)) +model.getVersion(version_num, callback(err, model)) ``` Returns model at revision version_num Errors version does not exist. Creates a version 0 if neccassary; ```javascript @@ -121,26 +122,43 @@ Model.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. ```javascript -Model.history(min_version=0, max_version=current_version, callback(err, model_array)) +model.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. ```javascript -Schema.plugin({conn: seperate_mongo_location}) +Schema.plugin({ + conn: seperate_mongo_location, + index: boolean, + collectionName: mongo_collection_name +}); + +Schema.RollbackModel ``` -Allow for history model to be stored somewhere else. +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. -The history model can be directly accessed from your Schema. It is added as a static variable called RollbackModel. +```javascript +Schema.wipeHistory(callback); +``` +Nuke the rollback collection, there is no going back. Callback takes the same arguments mongoose's remove does. -### Coming Soon! +```javascript +Schema.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); ## 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.