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-historise

v1.0.5

Published

Keep track of modifications history of your documents!

Downloads

31

Readme

mongoose-historise

License Version

Star Watch Fork

Keep track of modifications history of your documents!

Install & Test

$> npm i mongoose-historise
$> npm test

Why mongoose-historise?

Historise helps you keeping track of modifications within a document of any of your collections.

Unlike several Mongoose versioning plugins which duplicates a document to store it as an archive when it is updated, mongoose-historise stores a history of the modified fields directly within the document itself.

Example of a new "Movie" document:

{
  _id: 6055282e4992b599b0874155,
  castOverview: [ 'Daniel Radcliffe', 'Rupert Grint', 'Richard Harris' ],
  title: "Harry Potter and the Sorcerer's Stone",
  duration: '2h32min',
  director: 'Chris Columbus',
  summary: 'An orphaned boy enrolls in a school of wizardry, where he learns the truth about himself, his family and the terrible evil that haunts the magical world.',
  releaseDate: 2001-11-16T00:00:00.000Z,
  history: [],
  __v: 0
}

After updating some fields (e.g. 'duration' and 'castOverview') and saving the document, the 'history' is automatically updated as follows:

{
  _id: 6055282e4992b599b0874155,
  castOverview: [ 'Daniel Radcliffe', 'Rupert Grint', 'Richard Harris', 'Maggie Smith' ],
  title: "Harry Potter and the Sorcerer's Stone",
  duration: '2:32',
  director: 'Chris Columbus',
  summary: 'An orphaned boy enrolls in a school of wizardry, where he learns the truth about himself, his family and the terrible evil that haunts the magical world.',
  releaseDate: 2001-11-16T00:00:00.000Z,
  history: [
    {
      timestamp: 2021-03-19T22:40:30.499Z,
      modifications: [
        { field: 'duration', oldValue: '2h32min', newValue: '2:32' },
        {
          field: 'castOverview',
          oldValue: [ 'Daniel Radcliffe', 'Rupert Grint', 'Richard Harris' ],
          newValue: [ 'Daniel Radcliffe', 'Rupert Grint', 'Richard Harris', 'Maggie Smith' ]
        }
      ]
    }
  ],
  __v: 1
}

Quick Start

WARNING: mongoose-historise can generate history only on 'save' operation due to Mongoose limitations. If you want to use it, please use 'save' instead of 'update' operations to properly historise modifications made to your documents.

When creating your Mongoose Model, simply add mongoose-historise plugin (more options available in the section below):

const mongoose = require("mongoose");
const historise = require("mongoose-historise");

const schema = new mongoose.Schema({
    title: String,
    duration: String,
    director: String,
    castOverview: [String],
    summary: String,
    releaseDate: Date,
});

schema.plugin(historise, { mongooseInstance: mongoose, mongooseModelName: "Movie", });

const Movie = mongoose.model("Movie", schema);
module.exports = Movie;

Options

Currently, the following options are available for mongoose-historise (default values indicated, only 'mongooseInstance' and 'mongooseModelName' are required):

{
    // Mongoose instance, required to interact with your Mongoose model
    mongooseInstance: mongoose,
    // Name of your model (string)
    mongooseModelName: "Model",
    // Names of all fields in which history will be stored (see history structure below)
    fieldnames: {
        history: "history",
        modifications: "modifications",
        timestamp: "timestamp",
        field: "field",
        oldValue: "oldValue",
        newValue: "newValue"
    },
    limit: false, // False: no history limits; fill-in a number to indicate the maximum number of 'history' to store
    order: -1, // -1: Reverse chronological order (last modifications at the beginning); 1: Chronological order (last modifications at the end)
    deepComparison: true, // Enables deep comparison of objects (preventing reference comparison) using JSON.stringify(). Disable to increase speed if your collection does not use nested/sub-documents.
    ignore: ['createdAt', '__v', 'history'] // Ignore the mentioned fields when generating and storing the history
}

History structure

The history of a document is automatically generated based on fields that have been modified when saving a document. This plugin adds the following structure to your Mongoose Model:

{
    history: [
        {
            timestamp: Date
            modifications: [
                {
                    field: String, // Name of the field modified (also works with deep/nested fields)
                    oldValue: Mixed, // Previous value of that field
                    newValue: Mixed // New value of that field
                },
                ... // All fields modified during the same 'save' operation
            ]
        },
        ... // New item added on each 'save' (if fields have been modified)
    ]
}

You can edit the name of all these history structure's fields using the options above.

Issues & Features requests

If you encounter any bug/issue or would like new features to be added please feel free to open an issue on GitHub.