Gathering detailed insights and metrics for mongoose-version-handler
Gathering detailed insights and metrics for mongoose-version-handler
Gathering detailed insights and metrics for mongoose-version-handler
Gathering detailed insights and metrics for 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.
npm install mongoose-version-handler
Typescript
Module System
Node Version
NPM Version
TypeScript (98.84%)
JavaScript (1.16%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
4 Stars
49 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Jun 23, 2025
Latest Version
0.3.9
Package Id
mongoose-version-handler@0.3.9
Unpacked Size
38.02 kB
Size
7.67 kB
File Count
9
NPM Version
10.8.2
Node Version
20.18.0
Published on
Feb 05, 2025
Cumulative downloads
Total Downloads
2
A Mongoose plugin to manage document versioning and history with JSON Patch diffs, now featuring rollback functionality and full support for NestJS.
Install the plugin via npm:
1npm install mongoose-version-handler
The mongoose-version-handler
plugin allows you to:
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:
documentVersion
by default) to your schema.save()
MethodYou can pass metadata during a document save operation to associate it with the history document.
1const doc = await User.findOne({ ... }); 2await doc.save({ metadata: { updatedBy: '6762be74ff14f3257509c4c3' } });
The metadata
object will be saved alongside the JSON Patch changes in the history collection.
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 metadata: mongoose.SchemaTypes.Mixed, // Metadata associated with this change 10 date: Date, // Available if 'trackDate' is enabled 11});
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
.
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.
Rollback to a previous version using the rollback()
method:
1const user = await User.findOne({ ... }); 2await user.rollback();
To query the history collection directly, use the getHistoryModel()
method:
1const UserHistory = User.getHistoryModel(); 2const history = await UserHistory.find({ parent: user._id });
The plugin includes support for NestJS applications.
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 {}
Feel free to explore the plugin, contribute, or report any issues.
No vulnerabilities found.
No security vulnerabilities found.
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year