@node-lib-tech/audit-utils
v1.0.13
Published
Reusable AuditTrail middleware for Node/Express
Downloads
1,331
Maintainers
Readme
@node-lib-tech/audit-utils
Reusable, configurable Audit Trail middleware for Node.js / Express.
Automatically captures request metadata, user context, modified records, and before/after data — and sends them to ANY storage (DB, Kafka, queues, logs, etc.)
⭐ Features
- Express middleware
- Automatic batching
- DB agnostic
- Microservice friendly
- Zero config required
- Fully customizable
- Before/after change tracking
- Works with POST / PUT / DELETE
- Reusable across projects and services
Captures automatically:
- HTTP Method
- URL
- Module
- Table
- Record ID
- Payload
- User
- Response Code
- Duration
- Before / After row data
- Batched logs
📦 Install
npm install @node-lib-tech/audit-utils🚀 Quick Usage
import express from "express";
import { auditMiddleware, setAuditCallback } from "@node-lib-tech/audit-utils";
const app = express();
app.use(express.json());
setAuditCallback(async ({ auditId, logs }) => {
console.log("AUDIT:", auditId, logs.length);
});
app.use(auditMiddleware);
app.post("/users", (req,res)=>{
return res.json({ ok: true });
});
app.listen(3000);🔧 Configuration
All configuration is optional.
import {
setAuditConfig,
setModuleFieldAuditConfig,
setTableKeywordsAuditConfig,
setAllowedMethodsForData,
setWhitelistUrlSkip,
setAuditTrailSkipUrls,
setAuditCallback,
setTableDataByKeyword,
} from "@node-lib-tech/audit-utils";Where to Store Logs
setAuditCallback(({ auditId, logs }) => {
return DB.saveAudit(logs);
});Module → Primary Key
setModuleFieldAuditConfig({
USERS: "UserId",
ORDERS: "OrderId",
DEFAULT: "name"
});URL → Table mapping
setTableKeywordsAuditConfig({
"training-items": {
tableName: "TrainingItems",
id: "TrainingItemId"
}
});Allowed HTTP Methods
setAllowedMethodsForData(["POST", "PUT", "DELETE"]);Skip URLs
setWhitelistUrlSkip(["/health"]);Skip Heavy/Reporting APIs
setAuditTrailSkipUrls(["/reports", "/export"]);Provide Existing DB Row (PUT, DELETE)
setTableDataByKeyword(async ({ TableName, IdType, RecordId }) => {
return db(TableName).where(IdType, RecordId).first();
});🧰 Helper APIs
import {
generateAuditId,
unwrapJsonDataDeep,
extractTableNameFromUrl,
extractRecordIdFromUrl
} from "@node-lib-tech/audit-utils";📦 Works With
Backend Works
Express ✔
Node.js ✔
Microservices ✔
REST APIs ✔
Kafka ✔
MongoDB ✔
SQL ✔
CloudWatch ✔
Serverless ✔🌐 Supported runtimes
Runtime Works
Node 14 ✔
Node 16 ✔
Node 18+ ✔
Docker ✔
Lambda ✔
ECS ✔
EC2 ✔🔥 Example Output
{
"User": "admin",
"Table": "Users",
"Method": "POST",
"RecordId": 1501,
"Payload": {...},
"ResponseCode": 201,
"Duration": 210,
"AuditId": "89213-21321-1293",
"CreatedAt": "2025-01-11T10:32:22"
}