@wizeapps/sequelize-audit
Description
Enables auditing on sequelize models. It works on CREATE, UPDATE, DESTROY events on sequelize model instance.
Models
- WizeAudit: Stores all historical changes performed on model
- WizeAuditModel: Stores all models on which auditing in enabled
APIs
-
[
GET
] /api/wizeaudits - Lists all changes performed across all models -
[
GET
] /api/wizeaudits/:id - Fetches specific change set for the identifier -
[
GET
] /api/wizeauditmodels - Lists all models for which auditing is enabled -
[
POST
] /api/wizeauditmodels - Enable auditing on a specific model({"modelName": "User", "displayField": "FirstName"})
-
[
DELETE
] /api/wizeauditmodels/:id - Remove auditing on a specific model
Audit Schema
{
"id": <Auto-increment> INTEGER,
"diff": JSONB (Difference deep-diff),
"email": STRING (Actor),
"identifier": INTEGER (Instance ID),
"model": STRING (Model),
"action": CHAR(1) (Action),
"oldValue": JSONB (Previous state of instance)
}
Usage
Installation
npm install --save @wizeapps/sequelize-audit
Initialize
import wizeSequelizeAudit from '@wizeapps/sequelize-audit';
.
.
.
/*
app: koa-app returned from 'const app = createServer({});'
sequelizeSource: sequelize object returned from 'import sequelizeSource from './sources/postgresql';'
opts: JSON object
*/
wizeSequelizeAudit(app, sequelizeSource, opts);
Opts (Options)
{
"exclude": ["id", "createdAt", "updatedAt"]
}
Model Definition
Module assumes that model has system fields - createdBy and modifiedBy, which are used to store the actor. These system fields are used to capture the actor information in audit table. Supply createdBy in instance during Create and modifiedBy during Update and Delete operations.
Test
npm run test
License
MIT License