npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@allnow/nestjs-audit-log

v0.2.0

Published

Reusable audit log module for NestJS with request context, structured payloads, and pluggable storage.

Downloads

242

Readme

@allnow/nestjs-audit-log

Reusable NestJS audit log module based on the audit concept from bms-core-service.

It provides:

  • request context capture: user id, username, IP, user-agent, request id
  • structured audit payloads for CREATE, UPDATE, DELETE, or custom events
  • pluggable storage so logs can go to TypeORM, a queue, Kafka, RabbitMQ, HTTP, or any custom sink
  • optional @AuditLog() decorator and interceptor for controller-level action logs
  • TypeORM entity and storage helper for an audit_logs table

Install

npm install @allnow/nestjs-audit-log

Peer dependencies:

npm install @nestjs/common @nestjs/core reflect-metadata rxjs

For the TypeORM helper:

npm install typeorm @nestjs/typeorm

Basic Usage

import { Module } from '@nestjs/common';
import { AuditLogModule } from '@allnow/nestjs-audit-log';

@Module({
  imports: [
    AuditLogModule.forRoot(),
  ],
})
export class AppModule {}

By default logs are written through Nest Logger. Production applications should provide a storage adapter.

Custom Storage

import { Injectable } from '@nestjs/common';
import { AuditLogPayload, AuditLogStorage } from '@allnow/nestjs-audit-log';

@Injectable()
export class DatabaseAuditStorage implements AuditLogStorage {
  async save(payload: AuditLogPayload): Promise<void> {
    // insert into DB, publish to queue, etc.
  }
}
@Module({
  imports: [
    AuditLogModule.forRoot({
      storage: DatabaseAuditStorage,
      failSilently: true,
    }),
  ],
  providers: [DatabaseAuditStorage],
})
export class AppModule {}

Manual Logging

import { AuditLoggerService } from '@allnow/nestjs-audit-log';

export class FuelTypeService {
  constructor(private readonly auditLogger: AuditLoggerService) {}

  async update(id: number, dto: UpdateFuelTypeDto) {
    const before = await this.repo.findOneByOrFail({ id });
    const saved = await this.repo.save({ ...before, ...dto });

    await this.auditLogger.logUpdate({
      before,
      after: saved,
      entityType: 'FuelType',
      entityId: id,
      action: 'updateFuelType',
    });

    return saved;
  }
}

Controller Decorator

Register the interceptor once:

import { APP_INTERCEPTOR } from '@nestjs/core';
import { AuditLogInterceptor } from '@allnow/nestjs-audit-log';

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

Use it on handlers:

import { AuditLog } from '@allnow/nestjs-audit-log';

@Post()
@AuditLog({ event: 'CREATE', entityType: 'FuelType', entityId: 'id', newValuesPath: 'data' })
create(@Body() dto: CreateFuelTypeDto) {
  return this.service.create(dto);
}

entityId: 'id' reads result.id. Dot paths such as data.id are supported. entityType, entityId, metadata, and newValues also accept callbacks:

@AuditLog({
  event: 'CREATE',
  entityType: (result) => result.type,
  entityId: (result) => result.data.id,
  newValuesPath: 'entries.data',
  metadata: (_result, req: any) => ({ requestBody: req.body }),
})

TypeORM Storage

The package exports AuditLogTypeOrmModule, TypeOrmAuditLogEntity, and TypeOrmAuditLogStorage. For the common case, let the package wire TypeOrmModule.forFeature() and the storage provider:

import { Module } from '@nestjs/common';
import {
  AuditLogTypeOrmModule,
} from '@allnow/nestjs-audit-log';

@Module({
  imports: [
    AuditLogTypeOrmModule.forRoot({
      failSilently: true,
    }),
  ],
})
export class AppModule {}

You can also use the base module directly:

AuditLogModule.forRoot({
  storage: 'typeorm',
});

Recommended indexes are included on the entity:

  • entity_type, entity_id
  • event, created_at
  • user_id, created_at
  • request_id

For transactional manual writes, pass the active TypeORM manager:

await this.dataSource.transaction(async (manager) => {
  const saved = await manager.save(FuelTypeEntity, next);

  await this.auditLogger.logUpdate(
    { before, after: saved, entityType: 'FuelType', entityId: saved.id },
    { manager },
  );
});

Safety Options

String fields are truncated before save using the entity column limits. JSON payloads are redacted recursively. Default redacted keys are password, token, authorization, and secret.

AuditLogModule.forRoot({
  redactKeys: ['password', 'token', 'authorization', 'secret', 'apiKey'],
  truncate: {
    userAgent: 512,
  },
});

Request Context

The middleware reads:

  • request id from x-request-id, or generates a UUID
  • user from request.user.id, request.user.userId, or request.user.sub
  • username from request.user.username, request.user.email, or request.user.name
  • IP and user-agent from the request

Override user resolution:

AuditLogModule.forRoot({
  userResolver: {
    resolve: (req: any) => ({
      id: req.auth?.accountId,
      username: req.auth?.displayName,
    }),
  },
});

Publish

Before publishing, confirm name, version, and author in package.json.

npm install
npm run build
npm pack --dry-run
npm publish --access public