mongoose-version2
v0.0.4
Published
Mongoose plugin for document versioning.
Downloads
116
Readme
Mongoose Version 2
Mongoose Version is a mongoose plugin that automatically versions documents as they're modified. The previous versions are saved into a separate mongo collection.
!!!It disables the default mongoose3 versioning!!!
Installation
$ npm install mongoose-version2
Usage
To use mongoose-version for an existing mongoose schema you'll have to require and plugin mongoose-version into the existing schema.
The following schema definition defines a "Page" schema, and uses mongoose-version plugin with default options
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
version = require('mongoose-version2');
var connection = mongoose.createConnection(/*connection url*/);
var page = new Schema({
title : { type : String, required : true},
content : { type : String, required : true },
path : { type : String, required : true},
tags : [String],
});
page.plugin(version, {});
// the models
var Page = connection.model("Page", page);
var PrevVersions = Page.VersionModel;
Mongoose-version2 will
- define a schema that has a property doc holding the document before being modified.
- add the fields _created and _modified to the page schema
- add the VersionModel static property to the Page model
Mongoose-version2 will add a static field VersionedModel to Page that can be used to access the versioned model of Page, for example for querying old versions of a document.
Option keys and defaults
| property name | type | default | description | |-----------------------|----------|--------------------------|--------------------------------------------------------| | versionProperty | String | "_v" | the name of the property holding the version number | | modelName | Function | (name)=>name+"_version" | name of the mognoose model | | checkVersion | Boolean | true | whether to check if you're overwriting a newer version | | trackCreated | Boolean | true | whether to track the creation time | | trackModified | Boolean | true | whether to track the modification time | | createdProperty | String | "_created" | name of property holding the creation time | | modifiedProperty | String | "_modified" | name of property holding the modification time |
Versions format
The version model has the following structure:
{
action: "save", // reason for versioning "save" | "remove"
time: Date.now(), // Date of versioning
doc: { } // the document
}
Fork info
This is a based on https://github.com/saintedlama/mongoose-version.
Reasons for forking:
- The original doesn't support multiple connections
- The original is completely wroing as a mongoose plugin, as a schema can be instantiated as multiple models (multiple times) but the plugin doesn't support it.
- Complex code for such a simple plugin
- Strange work with the mongoose __v property
- Unneeded mongoose requirement
- Weird versioning action on document removal