Skip to content

nestjs-drizzle-auditing is a lightweight auditing module for NestJS applications using the Drizzle ORM. It provides an easy way to track changes in your database by automatically logging create, update, and delete operations on selected entities.

Notifications You must be signed in to change notification settings

rrodrigofranco/nestjs-drizzle-auditing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nestjs-drizzle-auditing

Automated auditing for NestJS using Drizzle ORM.
Inspired by laravel-auditing.

📦 Installation

npm install nestjs-drizzle-auditing
# or
yarn add nestjs-drizzle-auditing

🔧 Setup

1. Import the module

import { AuditModule } from 'nestjs-drizzle-auditing';

@Module({
  imports: [AuditModule],
})
export class AppModule {}

2. Register your Drizzle client

When initializing the module, you must provide your DrizzleClient using a custom provider:

{
  provide: 'DRIZZLE_CLIENT',
  useValue: drizzleInstance, // your configured Drizzle ORM client
}

3. Add the AuditInterceptor globally or per controller

import { APP_INTERCEPTOR } from '@nestjs/core';
import { AuditInterceptor } from 'nestjs-drizzle-auditing';

@Module({
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: AuditInterceptor,
    },
  ],
})
export class AppModule {}

🏷 Usage

Decorate a service method:

import { Auditable } from 'nestjs-drizzle-auditing';

@Injectable()
export class UsersService {
  @Auditable({ entity: 'User', action: 'update' })
  async updateUser(id: string, dto: UpdateUserDto) {
    // your update logic here
  }
}

By default, the action is set to 'update' if omitted.

🗃 Schema: Audit Log Table

You can create a table like the following in your Drizzle schema:

export const auditLogs = pgTable('audit_logs', {
  id: serial('id').primaryKey(),
  entity: text('entity').notNull(),
  action: text('action').notNull(), // create, update, delete
  oldValue: jsonb('old_value'),
  newValue: jsonb('new_value'),
  userId: text('user_id'),
  createdAt: timestamp('created_at', { withTimezone: true }).defaultNow(),
});

📌 Example Inserted Log

{
  "entity": "User",
  "action": "update",
  "old_value": {
    "name": "John Bonham"
  },
  "new_value": {
    "name": "Pat Phillips"
  },
  "user_id": "12345",
  "created_at": "2025-06-05T18:30:00.000Z"
}

🔍 Roadmap

  • Support for ignoring specific fields (ignoreFields)
  • Async logging via queues (BullMQ or RabbitMQ)
  • Deep diff support for complex objects
  • Decorator for class-level auditing

📄 License

MIT © [Rodrigo Franco]

About

nestjs-drizzle-auditing is a lightweight auditing module for NestJS applications using the Drizzle ORM. It provides an easy way to track changes in your database by automatically logging create, update, and delete operations on selected entities.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published