0.0.4 • Published 9 years ago

mongoose-version2 v0.0.4

Weekly downloads
22
License
BSD
Repository
github
Last release
9 years ago

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 nametypedefaultdescription
versionPropertyString"_v"the name of the property holding the version number
modelNameFunction(name)=>name+"_version"name of the mognoose model
checkVersionBooleantruewhether to check if you're overwriting a newer version
trackCreatedBooleantruewhether to track the creation time
trackModifiedBooleantruewhether to track the modification time
createdPropertyString"_created"name of property holding the creation time
modifiedPropertyString"_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