mongoose-audit-log
mongoose-audit-log is a mongoose plugin to manage an audit log of changes to the MongoDB database.
Install
npm install mongoose-audit-log
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:
- Set the current user on an entity right before persisting:
...Order ;
- Override the getUser-handler on application start:
// [audit.js] required on startup (e.g. in the server.js/app.js)const auditLog = plugin;// the userContext is a request-bound object, that is updated before the request is handled by a controller/routeconst userContext = userContext; module { auditLog userContextuserid;}; --- // [anywhere]...Order ;
Query history
Please find below an example route, to request the history of a given type and id:
const Audit = model;const User = ; router;