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

@ilhamtahir/nestjs-mapper

v1.0.4

Published

NestJS integration for @ilhamtahir/ts-mapper - A MapStruct-like object mapping library for TypeScript

Readme

@ilhamtahir/nest-mapper

npm version npm downloads npm license PRs Welcome

NestJS integration for @ilhamtahir/ts-mapper - A MapStruct-like object mapping library for TypeScript and NestJS.

📦 Installation

# Install both packages
npm install @ilhamtahir/ts-mapper @ilhamtahir/nestjs-mapper

# Or using yarn
yarn add @ilhamtahir/ts-mapper @ilhamtahir/nestjs-mapper

# Or using pnpm
pnpm add @ilhamtahir/ts-mapper @ilhamtahir/nestjs-mapper

🚀 Features

  • NestJS Integration: Seamless dependency injection support
  • Auto Registration: Automatic mapper registration in DI container
  • Enhanced Decorators: NestJS-specific decorator enhancements
  • Module Configuration: Easy module setup with MapperModule
  • Proxy Support: Automatic proxy creation for abstract mappers

📖 Quick Start

1. Configure Module

// app.module.ts
import { Module } from '@nestjs/common';
import { MapperModule } from '@ilhamtahir/nestjs-mapper';

@Module({
  imports: [
    MapperModule.forRoot(), // Auto-register all @Mapper() classes
  ],
})
export class AppModule {}

2. Create Mapper

// user.mapper.ts
import { Mapper, Mapping, transform } from '@ilhamtahir/nest-mapper';

@Mapper()
export class UserMapper {
  @Mapping({ source: 'fullName', target: 'name' })
  @Mapping({ source: 'profile.bio', target: 'bio' })
  @Mapping({ source: 'profile.avatar', target: 'avatar' })
  toDto(entity: UserEntity): UserDto {
    return transform(this, 'toDto', entity, UserDto);
  }
}

3. Use in Service

// user.service.ts
import { Injectable } from '@nestjs/common';
import { UserMapper } from './mappers/user.mapper';

@Injectable()
export class UserService {
  constructor(private readonly userMapper: UserMapper) {}

  async getUser(id: number): Promise<UserDto> {
    const entity = await this.userRepository.findById(id);
    return this.userMapper.toDto(entity);
  }

  async getUsers(): Promise<UserDto[]> {
    const entities = await this.userRepository.findAll();
    return entities.map(entity => this.userMapper.toDto(entity));
  }
}

🆕 Abstract Class Support

Using Abstract Mapper (Recommended)

// user-abstract.mapper.ts
import { Mapper, Mapping } from '@ilhamtahir/nest-mapper';

@Mapper()
export abstract class UserAbstractMapper {
  /**
   * Empty method body: system will automatically call transform
   */
  @Mapping({ source: 'fullName', target: 'name' })
  @Mapping({ source: 'profile.bio', target: 'bio' })
  @Mapping({ source: 'profile.avatar', target: 'avatar' })
  toDto(entity: UserEntity): UserDto {
    // Empty method body, system will automatically call transform
    return {} as UserDto;
  }

  /**
   * Custom method with business logic
   */
  toDtoWithCustomLogic(entity: UserEntity): UserDto {
    const dto = this.toDto(entity); // Calls auto-mapping

    // Add custom logic
    dto.displayName = `${dto.name} (${entity.age} years old)`;
    dto.isActive = entity.lastLoginAt > new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);

    return dto;
  }
}

Dependency Injection in Mappers

// advanced-user.mapper.ts
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Mapper, Mapping, transform } from '@ilhamtahir/nest-mapper';

@Mapper()
@Injectable()
export class AdvancedUserMapper {
  constructor(
    private readonly configService: ConfigService,
    private readonly logger: Logger
  ) {}

  @Mapping({ source: 'fullName', target: 'name' })
  toDto(entity: UserEntity): UserDto {
    this.logger.log(`Mapping user: ${entity.id}`);

    const dto = transform(this, 'toDto', entity, UserDto);

    // Use injected services
    const baseUrl = this.configService.get('app.baseUrl');
    dto.avatarUrl = `${baseUrl}/avatars/${dto.avatar}`;

    return dto;
  }
}

🔧 Advanced Configuration

Custom Module Configuration

// app.module.ts
@Module({
  imports: [
    MapperModule.forRoot({
      // Custom configuration options (if available in future versions)
    }),
  ],
})
export class AppModule {}

Feature Module Integration

// user.module.ts
import { Module } from '@nestjs/common';
import { MapperModule } from '@ilhamtahir/nest-mapper';
import { UserMapper } from './mappers/user.mapper';
import { UserService } from './user.service';

@Module({
  imports: [MapperModule], // Import without forRoot() in feature modules
  providers: [UserService, UserMapper],
  exports: [UserService, UserMapper],
})
export class UserModule {}

🔧 Troubleshooting

Mapper Not Found in DI Container

// Make sure to import MapperModule in your app module
@Module({
  imports: [
    MapperModule.forRoot(), // This is required!
  ],
})
export class AppModule {}

Circular Dependency Issues

// Use forwardRef for circular dependencies
@Injectable()
export class UserService {
  constructor(
    @Inject(forwardRef(() => UserMapper))
    private readonly userMapper: UserMapper
  ) {}
}

📚 API Documentation

Module

  • MapperModule.forRoot(): Configure and register the mapper module

Decorators

  • @Mapper(): Mark class as mapper and register in NestJS DI container
  • @Mapping({ source, target }): Explicit field mapping definition

Utility Functions

  • transform(mapper, method, input, OutputType): Execute mapping transformation

📋 Resources

🤝 Related Packages

📄 License

MIT License