npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-version2

v0.0.4

Published

Mongoose plugin for document versioning.

Downloads

57

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