audit-logs-package
v1.0.6
Published
This is is a Node.js package for logging audit messages to a database. It uses Sequelize as an ORM for database interactions
Downloads
3
Readme
Audit Logs Package
Audit Logs Package is a Node.js package for logging audit messages to a database. It uses Sequelize as an ORM for database interactions.
Features
- Define Sequelize model for Audit Log.
- Create audit logs with detailed information.
Getting Started
- Installation
npm install audit-logs-package
How to use Audit Logs package
const { Sequelize } = require('sequelize');
const { defineAuditLogModel, createAuditLog, closeConnection } = require('audit-logs-package');
// Initialize Sequelize instance
const sequelize = new Sequelize(/* Your database connection options */);
// Define AuditLog model
const AuditLog = defineAuditLogModel(sequelize);
// Example: Create an audit log
const auditLogParams = {
entityName: 'User',
entityId: '123',
tenantId: '456',
action: 'UPDATE',
previousRecord: /* Your previous record */,
currentRecord: /* Your current record */,
createdById: '789',
};
await createAuditLog(auditLogParams, AuditLog);
// Close the database connection on process exit
closeConnection(sequelize);
- Model Details
The defineAuditLogModel
function creates a Sequelize model for the Audit Log with the following properties:
- id: UUID primary key.
- entityName: String (255) representing the entity name.
- entityId: String (255) representing the entity ID.
- tenantId: Optional UUID for multi-tenancy support.
- action: String (32) representing the action performed.
- createdById: Optional UUID representing the user who created the log.
- timestamp: Date representing the log creation time.
- previousRecord: JSON representing the previous state.
- currentRecord: JSON representing the current state.
Logging Function
The createAuditLog
function is used to create an audit log with the specified parameters.
- params: An object containing the audit log details.
- AuditLog: The Sequelize model for Audit Log.
The log
function handles the actual creation of the log and includes error handling.
Closing Connection
The closeConnection function can be used to close the Sequelize database connection when your application exits.