Skip to main content

Audit System

The audit package in @repo/crud provides a comprehensive system for tracking changes to data. It automatically logs all create, update, and delete operations, recording who made what changes and when.

Features

  • Automatic Logging: Automatically log all data changes
  • User Tracking: Record which user made each change
  • Change Tracking: Record the exact changes made to data
  • Flexible Storage: Store audit logs in any database or service
  • Customizable: Configure what gets logged and how
  • TypeScript Support: Full TypeScript support with generics

Basic Usage

Setting Up Audit Logging

Setting the Current User

Before performing CRUD operations, you need to set the current user:

Integration with Authentication

You can integrate the audit system with your authentication system:

Audit Log Structure

Audit Log Interface

Example Audit Logs

Create Operation

Update Operation

Delete Operation

Audit Providers

Creating an Audit Provider

You can create an audit provider to store audit logs in any database or service:

Multiple Audit Providers

You can use multiple audit providers to store audit logs in different places:

Customizing Audit Logs

Filtering Audit Logs

You can filter which operations are logged:

Transforming Audit Logs

You can transform audit logs before they are stored:

Resource-Specific Audit Configuration

You can configure audit logging differently for each resource:

Querying Audit Logs

Basic Queries

Once you have stored audit logs, you can query them:

Advanced Queries

You can also perform more advanced queries:

UI Integration

Displaying Audit Logs

You can create a UI to display audit logs:

Filtering Audit Logs in the UI

You can add filtering to the audit log UI:

Advanced Usage

Custom Audit Context

You can provide additional context to audit logs:

Audit Events

You can listen for audit events:

Audit Hooks

You can use hooks to modify audit logs before they are stored:

Manual Audit Logging

You can manually log audit events:

API Reference

For a complete API reference, please refer to the TypeScript definitions in the package.