A Mongoose plugin to manage document versioning and history with JSON Patch diffs, now featuring rollback functionality and full support for NestJS.
Installations
npm install mongoose-version-handler
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
22.10.0
NPM Version
11.0.0
Score
71.2
Supply Chain
98.3
Quality
90.7
Maintenance
100
Vulnerability
99.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (97.8%)
JavaScript (2.2%)
Developer
AleckAstan
Download Statistics
Total Downloads
786
Last Day
11
Last Week
18
Last Month
786
Last Year
786
GitHub Statistics
3 Stars
38 Commits
1 Watching
1 Branches
1 Contributors
Bundle Size
16.18 kB
Minified
5.31 kB
Minified + Gzipped
Package Meta Information
Latest Version
0.3.5
Package Id
mongoose-version-handler@0.3.5
Unpacked Size
21.86 kB
Size
5.98 kB
File Count
7
NPM Version
11.0.0
Node Version
22.10.0
Publised On
18 Dec 2024
Total Downloads
Cumulative downloads
Total Downloads
786
Last day
-52.2%
11
Compared to previous day
Last week
-89.8%
18
Compared to previous week
Last month
0%
786
Compared to previous month
Last year
0%
786
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
2
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:
1npm install mongoose-version-handler
Purpose
The mongoose-version-handler
plugin allows you to:
- Track changes made to documents over time.
- Maintain a version history using JSON Patch format.
- Retrieve any version of a document easily.
- Rollback documents to previous versions.
- Seamlessly integrate with NestJS.
Basic Usage
Add the plugin to your schema:
1import { Schema } from 'mongoose'; 2import mongooseVersionHandler from 'mongoose-version-handler'; 3 4const UserSchema = new Schema({ 5 name: { 6 firstname: String, 7 lastname: String, 8 }, 9}); 10 11// Add versioning plugin 12UserSchema.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.
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:
1import mongoose, { Schema } from 'mongoose'; 2import mongooseVersionHandler from 'mongoose-version-handler'; 3 4const db = mongoose.createConnection('mongodb://localhost/my-database'); 5 6const UserSchema = new Schema({ 7 name: { 8 first: String, 9 last: String, 10 }, 11}); 12 13UserSchema.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:
1const user = await User.findOne({ ... }); 2const 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:
1const user = await User.findOne({ ... }); 2await 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:
1const UserHistory = User.getHistoryModel(); 2const history = await UserHistory.find({ parent: user._id });
The history collection schema looks like this:
1const ChangeSet = new mongoose.Schema({ 2 parent: mongoose.SchemaTypes.ObjectId, // Source document ID 3 version: Number, // Version number 4 patches: [{ // JSON Patch changes 5 op: String, 6 path: String, 7 value: mongoose.SchemaTypes.Mixed, 8 }], 9 date: Date, // Available if 'trackDate' is enabled 10});
NestJS Integration
The plugin includes support for NestJS applications.
Example: Using the Plugin in a NestJS Module
1import { Module } from '@nestjs/common'; 2import { MongooseModule, getConnectionToken } from '@nestjs/mongoose'; 3import { Connection } from 'mongoose'; 4import mongooseVersionHandler from 'mongoose-version-handler'; 5 6import { CatSchema, Cat } from './cat.schema'; 7import { CatController } from './cat.controller'; 8import { CatService } from './cat.service'; 9 10@Module({ 11 imports: [ 12 MongooseModule.forFeatureAsync([ 13 { 14 name: Cat.name, 15 inject: [getConnectionToken()], 16 useFactory: (connection: Connection) => { 17 const schema = CatSchema; 18 schema.plugin(mongooseVersionHandler, { connection }); 19 return schema; 20 }, 21 }, 22 ]), 23 ], 24 controllers: [CatController], 25 providers: [CatService], 26}) 27export class CatModule {}
Using Versioning Methods in Your Service
In your NestJS service, you can use the getVersion()
and rollback()
methods as demonstrated:
1const cat = await this.catModel.findOne({ ... }); 2const previousVersion = await cat.getVersion(2); 3await cat.rollback();
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.
Why Use mongoose-version-handler?
- Provides a reliable, structured way to manage document versions.
- Enables clear tracking of changes and rollbacks.
- Fits naturally into both Mongoose and NestJS workflows.
Feel free to explore the plugin, contribute, or report any issues.
No vulnerabilities found.
No security vulnerabilities found.
Gathering detailed insights and metrics for mongoose-version-handler