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-patch-history-plugin

v0.9.5

Published

Mongoose plugin that saves a history of JSON patch operations for all documents belonging to a schema in an associated 'patches' collection

Downloads

3

Readme

Mongoose Patch History

Build Status Dependency Status npm npm

Mongoose Patch History is a mongoose plugin that saves a history of JSON Patch operations for all documents belonging to a schema in an associated "patches" collection.

Installation

$ npm install mongoose-patch-history

Requirements

You should have mongoose 4 installed

Usage

To use mongoose-patch-history for an existing mongoose schema you can simply plug it in. As an example, the following schema definition defines a Post schema, and uses mongoose-patch-history with default options:

import mongoose, { Schema } from 'mongoose'
import patchHistory from 'mongoose-patch-history'

const PostSchema = new Schema({
  title: { type: String, required: true },
  comments: Array
})

PostSchema.plugin(patchHistory)
const Post = mongoose.model('Post', PostSchema)

mongoose-patch-history will define a schema that has a ref field containing the ObjectId of the original document, a ops array containing all json patch operations and a date field storing the date where the patch was applied.

Storing a new document

Continuing the previous example, a new patch is added to the associated patch collection whenever a new post is added to the posts collection:

Post.create({ title: 'JSON patches' })
  .then((post) => post.patches.findOne({ ref: post.id }))
  .then(console.log)

// {
//   _id: ObjectId('4edd40c86762e0fb12000003'),
//   ref: ObjectId('4edd40c86762e0fb12000004'),
//   ops: [
//     { value: 'JSON patches', path: '/title', op: 'add' },
//     { value: [], path: '/comments', op: 'add' }
//   ],
//   date: new Date(1462360838107),
//   __v: 0
// }

Updating an existing document

mongoose-patch-history also adds a static field Patches to the model that can be used to access the patch model associated with the model, for example to query all patches of a document. Whenever a post is edited, a new patch that reflects the update operation is added to the associated patch collection:

const data = {
  title: 'JSON patches with mongoose',
  comments: [{ message: 'Wow! Such Mongoose! Very NoSQL!' }]
}

Post.create({ title: 'JSON patches' })
  .then((post) => post.set(data).save())
  .then((post) => post.patches.find({ ref: post.id }))
  .then(console.log)

// [{
//   _id: ObjectId('4edd40c86762e0fb12000003'),
//   ref: ObjectId('4edd40c86762e0fb12000004'),
//   ops: [
//     { value: 'JSON patches', path: '/title', op: 'add' },
//     { value: [], path: '/comments', op: 'add' }
//   ],
//   date: new Date(1462360838107),
//   __v: 0
// }, {
//   _id: ObjectId('4edd40c86762e0fb12000005'),
//   ref: ObjectId('4edd40c86762e0fb12000004'),
//   ops: [
//     { value: { message: 'Wow! Such Mongoose! Very NoSQL!' }, path: '/comments/0', op: 'add' },
//     { value: 'JSON patches with mongoose', path: '/title', op: 'replace' }
//   ],
//   "date": new Date(1462361848742),
//   "__v": 0
// }]

Rollback to a specific patch

Documents have a rollback method that accepts the ObjectId of a patch doc and sets the document to the state of that patch, adding a new patch to the history.

Post.create({ title: 'First version' })
  .then((post) => post.set({ title: 'Second version' }).save())
  .then((post) => post.set({ title: 'Third version' }).save())
  .then((post) => {
    return post.patches.find({ ref: post.id })
      .then((patches) => post.rollback(patches[1].id))
  })
  .then(console.log)

// {
//   _id: ObjectId('4edd40c86762e0fb12000006'),
//   title: 'Second version',
//   __v: 0
// }

The rollback method will throw an Error when invoked with an ObjectId that is

  • not a patch of the document
  • the latest patch of the document

Options

PostSchema.plugin(patchHistory, {
  modelName: 'PostPatches'
})
  • modelName String for patches model name. By default, generated by append string Patches to source model name
  • removePatches Removes patches when origin document is removed. Default: true
  • includes Property definitions that will be included in the patch schema. Read more about includes in the next chapter of the documentation. Default: {}

Includes

PostSchema.plugin(patchHistory, {
  includes: {
    title: { type: String, required: true }
  }
})

This will add a title property to the patch schema. All options that are available in mongoose's schema property definitions such as required, default or index can be used.

Post.create({ title: 'Included in every patch' })
  .then((post) => post.patches.findOne({ ref: post.id })
  .then((patch) => {
    console.log(patch.title) // 'Included in every patch'
  })

The value of the patch documents properties is read from the versioned documents property of the same name.

Reading from virtuals

There is an additional option that allows storing information in the patch documents that is not stored in the versioned documents. To do so, you can use a combination of virtual type setters on the versioned document and an additional from property in the include options of mongoose-patch-history:

// save user as _user in versioned documents
PostSchema.virtual('user').set(function (user) {
  this._user = user
})

// read user from _user in patch documents
PostSchema.plugin(patchHistory, {
  includes: {
    user: { type: Schema.Types.ObjectId, required: true, from: '_user' }
  }
})

// create post, pass in user information
Post.create({
  title: 'Why is hiring broken?',
  user: mongoose.Types.ObjectId()
})
  .then((post) => {
    console.log(post.user) // undefined
    return post.patches.findOne({ ref: post.id })
  })
  .then((patch) => {
    console.log(patch.user) // 4edd40c86762e0fb12000012
  })

In case of a rollback in this scenario, the rollback method accepts an object as its second parameter where additional data can be injected:

Post.create({ title: 'v1', user: mongoose.Types.ObjectId() })
  .then((post) => post.set({
    title: 'v2',
    user: mongoose.Types.ObjectId()
  }).save())
  .then((post) => {
    return post.patches.find({ ref: post.id })
      .then((patches) => post.rollback(patches[0].id, {
        user: mongoose.Types.ObjectId()
      }))
  })