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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mongoose-version-handler

v0.3.9

Published

A Mongoose plugin to manage document versioning and history with JSON Patch diffs, now featuring rollback functionality and full support for NestJS.

Readme

mongoose-version-handler

A Mongoose plugin to manage document versioning and history with JSON Patch diffs, now featuring rollback functionality and full support for NestJS.


Installation

Install the plugin via npm:

npm install mongoose-version-handler

Purpose

The mongoose-version-handler plugin allows you to:

  1. Track changes made to documents over time.
  2. Maintain a version history using JSON Patch format.
  3. Retrieve any version of a document easily.
  4. Rollback documents to previous versions.
  5. Seamlessly integrate with NestJS.

Basic Usage

Add the plugin to your schema:

import { Schema } from 'mongoose';
import mongooseVersionHandler from 'mongoose-version-handler';

const UserSchema = new Schema({
    name: {
        firstname: String,
        lastname: String,
    },
});

// Add versioning plugin
UserSchema.plugin(mongooseVersionHandler);

The plugin will:

  • Add a version field (documentVersion by default) to your schema.
  • Maintain a separate history collection storing JSON Patch diffs for each change.
  • Automatically increment the version number on each save/update.

Metadata in save() Method

You can pass metadata during a document save operation to associate it with the history document.

Example

const doc = await User.findOne({ ... });
await doc.save({ metadata: { updatedBy: '6762be74ff14f3257509c4c3' } });

The metadata object will be saved alongside the JSON Patch changes in the history collection.

History Collection Schema (with Metadata)

const ChangeSet = new mongoose.Schema({
    parent: mongoose.SchemaTypes.ObjectId, // Source document ID
    version: Number,                      // Version number
    patches: [{                           // JSON Patch changes
        op: String,
        path: String,
        value: mongoose.SchemaTypes.Mixed,
    }],
    metadata: mongoose.SchemaTypes.Mixed, // Metadata associated with this change
    date: Date, // Available if 'trackDate' is enabled
});

Options

versionKey

Specifies the version field name added to the schema. Default: documentVersion.


collection

Sets the name of the history collection. Default: <original_collection>_h.


connection

Passes a specific database connection for version tracking. This is required when using mongoose.createConnection.

Example:

import mongoose, { Schema } from 'mongoose';
import mongooseVersionHandler from 'mongoose-version-handler';

const db = mongoose.createConnection('mongodb://localhost/my-database');

const UserSchema = new Schema({
    name: {
        first: String,
        last: String,
    },
});

UserSchema.plugin(mongooseVersionHandler, { connection: db });

trackDate

Tracks the creation date for each version. Adds a date field to the history collection when enabled.


addDateToDocument

Stores the version creation date redundantly in the main document. Requires trackDate: true.


versionDateKey

Specifies the name of the date field added to the document when addDateToDocument is enabled. Default: documentVersionDate.


Retrieving a Specific Document Version

You can retrieve any version of a document using the getVersion() method:

const user = await User.findOne({ ... });
const version2 = await user.getVersion(2);

getVersion() returns a Promise resolving to the document version.


Rolling Back a Document

Rollback to a previous version using the rollback() method:

const user = await User.findOne({ ... });
await user.rollback();
  • If a previous version exists, the document rolls back to that version.
  • If no previous version exists, the document will be deleted.

Accessing the History Collection

To query the history collection directly, use the getHistoryModel() method:

const UserHistory = User.getHistoryModel();
const history = await UserHistory.find({ parent: user._id });

NestJS Integration

The plugin includes support for NestJS applications.

Example: Using the Plugin in a NestJS Module

import { Module } from '@nestjs/common';
import { MongooseModule, getConnectionToken } from '@nestjs/mongoose';
import { Connection } from 'mongoose';
import mongooseVersionHandler from 'mongoose-version-handler';

import { CatSchema, Cat } from './cat.schema';
import { CatController } from './cat.controller';
import { CatService } from './cat.service';

@Module({
    imports: [
        MongooseModule.forFeatureAsync([
            {
                name: Cat.name,
                inject: [getConnectionToken()],
                useFactory: (connection: Connection) => {
                    const schema = CatSchema;
                    schema.plugin(mongooseVersionHandler, { connection });
                    return schema;
                },
            },
        ]),
    ],
    controllers: [CatController],
    providers: [CatService],
})
export class CatModule {}

Key Features Recap

  • Automatic Version Tracking: Tracks document changes with JSON Patch diffs.
  • Rollback Support: Revert documents to previous versions with ease.
  • Customizable Options: Configure version keys, history collections, and connection settings.
  • NestJS Ready: Works seamlessly with NestJS and dependency injection.

Feel free to explore the plugin, contribute, or report any issues.