Gathering detailed insights and metrics for mongoose-user-history-plugin
Gathering detailed insights and metrics for mongoose-user-history-plugin
Gathering detailed insights and metrics for mongoose-user-history-plugin
Gathering detailed insights and metrics for mongoose-user-history-plugin
The Mongoose User History Plugin is designed to provide an extensive and detailed log of all modifications made to documents within a MongoDB collection managed by Mongoose.
npm install mongoose-user-history-plugin
Typescript
Module System
Node Version
NPM Version
TypeScript (84.88%)
JavaScript (15.12%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
BSD-3-Clause License
2 Stars
19 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Apr 06, 2025
Latest Version
0.5.49
Package Id
mongoose-user-history-plugin@0.5.49
Unpacked Size
100.14 kB
Size
18.36 kB
File Count
9
NPM Version
10.8.2
Node Version
22.5.1
Published on
Aug 08, 2024
Cumulative downloads
Total Downloads
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
2
The Mongoose User History Plugin is designed to provide an extensive and detailed log of all modifications made to documents within a MongoDB collection managed by Mongoose.
This functionality is crucial for applications that require an audit trail or version control of document changes over time. By capturing a comprehensive history of each document's evolution, the plugin facilitates better data management, accountability, and traceability.
1yarn add mongoose-user-history-plugin
or
1npm install mongoose-user-history-plugin
Alternatively, you can manually add it to your project's package.json.
To begin tracking changes on your collections, simply integrate the mongoose-history plugin into your schema:
1import mongooseHistory from 'mongoose-user-history-plugin'; 2import mongoose, { Schema } from 'mongoose'; 3 4var PostSchema = new Schema({ 5 title: String, 6 message: String, 7}); 8 9PostSchema.plugin(mongooseHistory);
This integration automatically logs all changes made to the schema.
To track the user responsible for changes, first, establish a context in any middleware:
For NestJS:
1// /src/main.ts 2import contextService from 'request-context'; 3 4// Creating a NestJS application instance 5const app = await NestFactory.create(AppModule); 6app.use(contextService.middleware('request')); 7 8// In user.guard.ts or a similar guard 9import contextService from 'request-context'; 10 11@Injectable() 12export class AuthGuard implements CanActivate { 13 async canActivate(context: ExecutionContext): Promise<boolean> { 14 const request = context.switchToHttp().getRequest(); 15 // Assuming you get the user information here 16 contextService.set('request:userInfo', user._id); 17 } 18}
For Express:
1import contextService from 'request-context'; 2 3// wrap requests in the 'request' namespace 4app.use(contextService.middleware('request')); 5 6// set some object from the request object on the context 7// to automatically save it when a document changes 8app.use(function (req, res, next) { 9 contextService.setContext('request:userInfo', req.user._id); 10 next(); 11});
Configure the modifiedBy field in your schema to reflect the user making the change. This can be a direct reference or an entire user object:
1import mongoose, { Types } from 'mongoose'; 2 3PostSchema.plugin(mongooseHistory, { 4 modifiedBy: { 5 schemaType: Types.ObjectId, // Can be String, ObjectId, etc. 6 contextPath: 'request:userInfo', 7 }, 8});
To include extra data, utilize the metadata option. It accepts an array of objects with key and value parameters. The schema parameter (default {type: mongoose.Schema.Types.Mixed}) can also be specified for mongoose schema options. The value can be a string (extracted from the updated document) or a (sync/async) function:
1import mongoose, { Types } from 'mongoose'; 2 3PostSchema.plugin(mongooseHistory, { 4 metadata: [ 5 { key: 'title', value: 'title' }, 6 { 7 key: 'titleFunc', 8 value: function (original, newObject) { 9 return newObject.title; 10 }, 11 }, 12 { 13 key: 'titleAsync', 14 value: function (original, newObject, cb) { 15 cb(null, newObject.title); 16 }, 17 }, 18 ], 19});
Define indexes to enhance query performance in the history collection:
1PostSchema.plugin(mongooseHistory, { 2 indexes: [{ collectionName: 1, action: 1, documentId: 1, modifiedBy: 1 }], 3});
The plugin automatically stores history records for each operation performed on the documents within your collections. The format of these history records is determined by the plugin's configuration and the type of operation (create, update, delete). Below is an explanation of how history records are stored, using default settings:
action
field is set to "created" and the currentDocument
field contains the snapshot of the document at the time of creation.oldDocument
) and updated (currentDocument
) states of the document. The changes
field details the modifications made, providing a before-and-after comparison.action
field set to "deleted". The oldDocument
field stores the last known state of the document before it was removed.Here's an example of what history records might look like for different operations:
Each history record includes a timestamp (createdAt
) indicating when the change was made. Depending on the plugin options used, additional information such as the user who made the change (modifiedBy
) and any extra metadata can also be stored.
By default, history records are stored in a collection named 'History', but this can be customized via the plugin's configuration options.
This project is licensed under the BSD 3-Clause "New" or "Revised" License. See the LICENSE file for more details.
No vulnerabilities found.
No security vulnerabilities found.