@nathapp/nestjs-audit
Decorator-driven, storage-agnostic audit logging for NestJS applications.
Features
- Declarative capture —
@Audit({ action, resource? }) on a controller/service method, picked up by AuditInterceptor
- Pluggable storage —
IAuditStorage seam; ships InMemoryAuditStorage (default) and LoggerAuditStorage; bring your own Prisma/TypeORM-backed storage
- Pluggable actor resolution —
IAuditActorResolver seam; ships DefaultActorResolver (req.user?.id ?? 'anonymous')
- Never fails the request — audit storage write failures are caught and logged, not propagated to the response
- Correlation-id aware — reads
@nathapp/nestjs-logging's request-scoped correlation id automatically
Installation
npm install @nathapp/nestjs-audit @nathapp/nestjs-common @nathapp/nestjs-logging
Peer requirements: ClockModule and LoggingModule. AuditModule does not import ClockModule itself — AuditInterceptor depends on the CLOCK token from @nathapp/nestjs-common, and expects it to already be available in your application's module tree. Register ClockModule.register() in your root module's imports if it is not already available. Opt-in LoggerAuditStorage likewise needs a Logger from @nathapp/nestjs-logging: it resolves the exported Logger class — register LoggingModule.register() (or its async equivalent) in your module tree — and also accepts a provider registered under the LOGGER_SERVICE token, so an app that supplies that token directly can inject the storage without importing the logging module. When both are available the Logger class wins; when neither is available, constructing the storage fails with an error that names LoggerAuditStorage. Manual construction, new LoggerAuditStorage(logger), still works with a single logger argument. ClockModule is global: true, so apps using nestjs-common's clock seam often already satisfy the clock requirement.
Quick Start
import { Module } from '@nestjs/common';
import { AuditModule } from '@nathapp/nestjs-audit';
@Module({
imports: [AuditModule.register()],
})
export class AppModule {}
import { Audit, AuditInterceptor } from '@nathapp/nestjs-audit';
@Controller('users')
@UseInterceptors(AuditInterceptor)
export class UsersController {
@Audit({ action: 'USER_UPDATED', resource: 'user', resourceIdParam: 'id' })
@Patch(':id')
updateUser(@Param('id') id: string, @Body() dto: UpdateUserDto) {
return this.usersService.update(id, dto);
}
}
Custom storage
import { AuditModule } from '@nathapp/nestjs-audit';
@Module({
imports: [
AuditModule.registerAsync({
useFactory: (prisma: PrismaService) => ({ storage: new PrismaAuditStorage(prisma) }),
inject: [PrismaService],
}),
],
})
export class AppModule {}
Scope
v1 does not include automatic before/after state diffing — pass whatever context you need through IAuditStorage directly if you need it, since AuditEntry.metadata is available to any direct storage consumer (not populated by AuditInterceptor itself, since @Audit() options are static and set at method-definition time).
Security
AuditEntry.metadata and the built-in LoggerAuditStorage write whatever is passed through verbatim (including to structured logs). Do not put secrets, API keys, passwords, or other sensitive personally-identifiable information into metadata — treat it the same way you would any other logged field.