Skip to content

Commit

Permalink
Merge pull request #160 from sliit-foss/feature/mongoose-audit
Browse files Browse the repository at this point in the history
Feature/mongoose audit
  • Loading branch information
Akalanka47000 authored Mar 10, 2024
2 parents ed2cfd7 + 1dca2c6 commit cda1b96
Show file tree
Hide file tree
Showing 14 changed files with 1,117 additions and 32 deletions.
44 changes: 22 additions & 22 deletions packages/functions/src/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ const logger = moduleLogger("tracer");

const _asyncHandler =
(fn, trace = false) =>
async (req, res, next) => {
let fnName;
try {
if (trace) {
fnName = _fnName(fn);
await _traced(fn.bind(this, req, res, next), {}, fnName);
} else {
await fn(req, res, next);
}
if (!res.headersSent) next();
} catch (err) {
if (!trace) {
fnName = fnName ?? _fnName(fn);
logger.error(`${fnName} execution failed - error: ${err.message} - stack: ${err.stack}`);
}
res.errorLogged = true;
if (!res.headersSent) next(err);
async (req, res, next) => {
let fnName;
try {
if (trace) {
fnName = _fnName(fn);
await _traced(fn.bind(this, req, res, next), {}, fnName);
} else {
await fn(req, res, next);
}
};
if (!res.headersSent) next();
} catch (err) {
if (!trace) {
fnName = fnName ?? _fnName(fn);
logger.error(`${fnName} execution failed - error: ${err.message} - stack: ${err.stack}`);
}
res.errorLogged = true;
if (!res.headersSent) next(err);
}
};

export const asyncHandler = (fn) => _asyncHandler(fn);

Expand All @@ -42,16 +42,16 @@ export const fallibleAsyncHandler = (fn) => async (req, res, next) => {

export const plainAsyncHandler = (fn) => async (req, res, next) => {
try {
const result = fn(req, res, next)
const result = fn(req, res, next);
if (result instanceof Promise) await result;
} catch (e) {
next(e)
next(e);
}
}
};

export default {
asyncHandler,
tracedAsyncHandler,
fallibleAsyncHandler,
plainAsyncHandler,
plainAsyncHandler
};
3 changes: 2 additions & 1 deletion packages/functions/src/traced.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const logger = moduleLogger("tracer");

export const _traced = (fn, loggable = {}, fnName, layer, fallible) => {
let startTime;
const disableTracing = process.env.DISABLE_FUNCTION_TRACING === "true" || process.env.DISABLE_FUNCTION_TRACING === "1";
const disableTracing =
process.env.DISABLE_FUNCTION_TRACING === "true" || process.env.DISABLE_FUNCTION_TRACING === "1";
if (!disableTracing) {
fnName = fnName ?? _fnName(fn, layer);
logger.info(`${fnName} execution initiated`, loggable);
Expand Down
4 changes: 2 additions & 2 deletions packages/functions/test/async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ describe("asyncHandler", () => {
});
test("test plain async handler with async function", async () => {
await plainAsyncHandler(async () => {
throw new Error("test")
throw new Error("test");
})(mockReq, mockRes, mockNext);
expect(mockNext).toHaveBeenCalled();
});
test("test plain async handler with normal function", async () => {
await plainAsyncHandler(() => {
throw new Error("test")
throw new Error("test");
})(mockReq, mockRes, mockNext);
expect(mockNext).toHaveBeenCalled();
});
Expand Down
Empty file modified packages/templates/src/index.js
100644 → 100755
Empty file.
45 changes: 45 additions & 0 deletions plugins/mongoose-audit/package.json
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"
}
}
68 changes: 68 additions & 0 deletions plugins/mongoose-audit/readme.md
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 = "[email protected]";
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.
}
});
```
12 changes: 12 additions & 0 deletions plugins/mongoose-audit/src/constants.js
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,
}
11 changes: 11 additions & 0 deletions plugins/mongoose-audit/src/index.js
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
};
24 changes: 24 additions & 0 deletions plugins/mongoose-audit/src/model.js
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;
Loading

0 comments on commit cda1b96

Please sign in to comment.