mongoose-audit-logger

2.0.6 • Public • Published

mongoose-audit-logger

mongoose-audit-logger is a mongoose plugin to manage an audit log of changes to the MongoDB database (forked from @ccanow/mongoosee-audit-log).

Install

npm install mongoose-audit-logger

Features

  • Store changes to entities on persist (save, update, delete)
  • Remember the user, that executed the change
  • Log when the change has been done

Storing the current user

In order to collect the information about who actually did a change to an entity, the user information is mandatory. This can be set on a per usage (1) or global (2) level:

  1. Set the current user on an entity right before persisting:
...
Order.findById(123)
  .then(order => {
    order.__user = 'me@test.de';
    order.amount = 1000;
  })
  .save();
  1. Override the getUser-handler on application start:
// [audit.js] required on startup (e.g. in the server.js/app.js)
const auditLog = require('mongoose-audit-logger').plugin;
// the userContext is a request-bound object, that is updated before the request is handled by a controller/route
const userContext = require('./passport').userContext;

module.exports = () => {
  auditLog.getUser = () => userContext.user.id;
};

---

// [anywhere]
...
Order.findById(123)
  .then(order => {
    order.amount = 1000;
  })
  .save();

Query history

Please find below an example route, to request the history of a given type and id:

const Audit = require('mongoose-audit-logger').model;
const User = require('../models/User');

router.get('/api/users/:id/history', (req, res, next) => {
  const itemName = User.collection.collectionName;
  Audit.find({ itemId: req.params.id, itemName })
    .then(history => res.json(history))
    .catch(next);
});

Package Sidebar

Install

npm i mongoose-audit-logger

Weekly Downloads

2

Version

2.0.6

License

MIT

Unpacked Size

43.2 kB

Total Files

8

Last publish

Collaborators

  • harrisyn