
Security News
Static vs. Runtime Reachability: Insights from Latio’s On the Record Podcast
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
mongoose-user-history-plugin
Advanced tools
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.
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.
yarn add mongoose-user-history-plugin
or
npm 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:
import mongooseHistory from 'mongoose-user-history-plugin';
import mongoose, { Schema } from 'mongoose';
var PostSchema = new Schema({
title: String,
message: String,
});
PostSchema.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:
// /src/main.ts
import contextService from 'request-context';
// Creating a NestJS application instance
const app = await NestFactory.create(AppModule);
app.use(contextService.middleware('request'));
// In user.guard.ts or a similar guard
import contextService from 'request-context';
@Injectable()
export class AuthGuard implements CanActivate {
async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
// Assuming you get the user information here
contextService.set('request:userInfo', user._id);
}
}
For Express:
import contextService from 'request-context';
// wrap requests in the 'request' namespace
app.use(contextService.middleware('request'));
// set some object from the request object on the context
// to automatically save it when a document changes
app.use(function (req, res, next) {
contextService.setContext('request:userInfo', req.user._id);
next();
});
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:
import mongoose, { Types } from 'mongoose';
PostSchema.plugin(mongooseHistory, {
modifiedBy: {
schemaType: Types.ObjectId, // Can be String, ObjectId, etc.
contextPath: 'request:userInfo',
},
});
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:
import mongoose, { Types } from 'mongoose';
PostSchema.plugin(mongooseHistory, {
metadata: [
{ key: 'title', value: 'title' },
{
key: 'titleFunc',
value: function (original, newObject) {
return newObject.title;
},
},
{
key: 'titleAsync',
value: function (original, newObject, cb) {
cb(null, newObject.title);
},
},
],
});
Define indexes to enhance query performance in the history collection:
PostSchema.plugin(mongooseHistory, {
indexes: [{ collectionName: 1, action: 1, documentId: 1, modifiedBy: 1 }],
});
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.
FAQs
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.
The npm package mongoose-user-history-plugin receives a total of 26 weekly downloads. As such, mongoose-user-history-plugin popularity was classified as not popular.
We found that mongoose-user-history-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.
Security News
The latest Opengrep releases add Apex scanning, precision rule tuning, and performance gains for open source static code analysis.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.