Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Snkz committed Nov 11, 2014
1 parent ada8637 commit efd5107
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -61,7 +61,7 @@ if (mongoose.models.Model) {
exports.Model = Model;
```

### Example usage
#### Example usage
```javascript
describe('Rollback Hell', function(done) {

Expand Down Expand Up @@ -101,46 +101,64 @@ describe('Rollback Hell', function(done) {
});
```
## API
#### Methods
These extensions happen on <b>instances</b> 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
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 <b>your Schema</b>.

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.
Expand Down

0 comments on commit efd5107

Please sign in to comment.