-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #160 from sliit-foss/feature/mongoose-audit
Feature/mongoose audit
- Loading branch information
Showing
14 changed files
with
1,117 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"name": "@sliit-foss/mongoose-audit", | ||
"version": "0.0.0", | ||
"description": "A rework of the mongoose-audit-log package to support newer versions of mongoose and more flexible options", | ||
"main": "dist/index.js", | ||
"types": "types/index.d.ts", | ||
"scripts": { | ||
"build": "node ../../scripts/esbuild.config.js", | ||
"build:watch": "bash ../../scripts/esbuild.watch.sh", | ||
"bump-version": "bash ../../scripts/bump-version.sh --name=@sliit-foss/express-http-context", | ||
"lint": "bash ../../scripts/lint.sh", | ||
"release": "bash ../../scripts/release.sh", | ||
"test": "if [ \"$CI\" = \"true\" ]; then \n bash ../../scripts/test/test.sh; else \n echo \"Skipping as it is not a CI environment\"; fi" | ||
}, | ||
"dependencies": { | ||
"deep-diff": "1.0.2", | ||
"dot-object": "2.1.4", | ||
"lodash": "4.17.10" | ||
}, | ||
"peerDependencies": { | ||
"mongoose": ">=5" | ||
}, | ||
"devDependencies": { | ||
"mongoose": "^5" | ||
}, | ||
"author": "SLIIT FOSS", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/sliit-foss/npm-catalogue.git" | ||
}, | ||
"homepage": "https://github.com/sliit-foss/npm-catalogue/blob/main/plugins/mongoose-audit/readme.md", | ||
"keywords": [ | ||
"mongoose", | ||
"mongoose-plugin", | ||
"audit", | ||
"log", | ||
"trail", | ||
"history", | ||
"version" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/sliit-foss/npm-catalogue/issues" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# @sliit-foss/mongoose-audit | ||
|
||
#### A rework of the [mongoose-audit-log](https://www.npmjs.com/package/mongoose-audit-log) package to support newer versions of mongoose and more flexible options<br> | ||
|
||
#### !IMPORTANT - The behaviour of this is different from the original `mongoose-audit-log` package and cannot be considered as a drop in replacement for it. | ||
|
||
It is a mongoose plugin to manage an audit log of changes to a MongoDB database. | ||
|
||
## 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 required. | ||
This can be set on a per usage (1) or global (2) level: | ||
|
||
1. Set the current user on an entity right before persisting: | ||
|
||
```javascript | ||
Order.findById(123) | ||
.then((order) => { | ||
order.__user = "me@test.de"; | ||
order.amount = 1000; | ||
}) | ||
.save(); | ||
``` | ||
|
||
2. Set it as an option when registering the plugin: | ||
|
||
```javascript | ||
const { plugin } = require("@sliit-foss/mongoose-audit"); | ||
|
||
SomeSchema.plugin(plugin, { | ||
getUser: () => "user details from wherever you wish to get it" | ||
}); | ||
``` | ||
|
||
## Query history | ||
|
||
Please find below an example express route, to request the history of a given type and id: | ||
|
||
```javascript | ||
const { plugin, Audit } = require("@sliit-foss/mongoose-audit"); | ||
|
||
router.get("/api/users/:id/history", (req, res, next) => { | ||
Audit.find({ entity_id: req.params.id, entity: "User" }) | ||
.then((history) => res.json(history)) | ||
.catch(next); | ||
}); | ||
``` | ||
|
||
## All supported plugin options | ||
|
||
```javascript | ||
const { plugin, AuditType } = require("@sliit-foss/mongoose-audit"); | ||
|
||
SomeSchema.plugin(plugin, { | ||
getUser: () => "user details from wherever you wish to get it", | ||
types: [AuditType.Edit], // default: ['add', 'edit', 'delete'] | ||
exclude: ["field1", "field2"], | ||
onAudit: (audit) => { | ||
// Called before persisting the audit is saved. Use this to use your own audit model instead of the default one. | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export const AuditType = { | ||
Add: "Add", | ||
Edit: "Edit", | ||
Delete: "Delete" | ||
}; | ||
|
||
export const ChangeAuditType = { | ||
N: AuditType.Add, | ||
D: AuditType.Delete, | ||
E: AuditType.Edit, | ||
A: AuditType.Edit, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { default as plugin } from "./plugin"; | ||
import { default as Audit } from "./model"; | ||
import { AuditType } from "./constants"; | ||
|
||
export { plugin, Audit, AuditType }; | ||
|
||
export default { | ||
plugin, | ||
Audit, | ||
AuditType | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const auditSchema = new mongoose.Schema( | ||
{ | ||
entity_id: {}, | ||
entity: String, | ||
changes: {}, | ||
user: { | ||
type: mongoose.Schema.Types.Mixed, | ||
ref: "User", | ||
collection: "users" | ||
} | ||
}, | ||
{ | ||
timestamps: { | ||
createdAt: "created_at", | ||
updatedAt: false | ||
} | ||
} | ||
); | ||
|
||
const model = mongoose.model("Audit", auditSchema); | ||
|
||
export default model; |
Oops, something went wrong.