Options
All
  • Public
  • Public/Protected
  • All
Menu

travis npm downloads bundlephobia license coveralls Greenkeeper

NestJSX Automapper

A wrapper around @nartc/automapper to be used with NestJS as a Module.

Migrations

Migrate to v3

forRoot() method will be deprecated soon (still available in v3). Please use withMapper() instead.

Documentations

This module is a wrapper around @nartc/automapper so all usage documentations should be referenced at the link below.

Github Pages https://automapper.netlify.com/ Github Repo https://github.com/nartc/mapper

Features

  • Mapping between two classes
  • Mapping for nested classes
  • Array/List Mapping
  • Flattening
  • Basic ReverseMap
  • Value Converters
  • Value Resolvers
  • Async
  • Before/After Callback
  • Naming Conventions (PascalCase and camelCase)

Contributions are appreciated.

Setup

npm i -s nestjsx-automapper

Installing nestjsx-automapper will also install @nartc/automapper.

Note 1: Please make sure that you've read @nartc/automapper documentations to familiarize yourself with AutoMapper's terminology and how to setup your Profile and such.

Setup

  1. Import AutomapperModule in AppModule and call .withMapper() method
@Module({
  imports: [AutomapperModule.withMapper()],
})
export class AppModule {}

AutomapperModule.withMapper() has the following overloads:

static withMapper(name?: string, options?: AutoMapperGlobalSettings);
static withMapper(options?: AutoMapperGlobalSettings);
  • name: Name of the AutoMapper instance being created with withMapper(). Default to "default"
  • options: Check AutoMapperGlobalSettings for more information
  1. nestjsx-automapper exposes a @Profile() decorator to decorate your Profile classes.
@Profile()
class UserProfile extends ProfileBase {}

@Profile() takes in an optional name argument. This is the name if the AutoMapper instance you use to create the instance with withMapper(). Default to "default"

Usually, NestJS will have many Feature Modules for each of the Domain Models. Hence, a Profile should stay in close to where the feature module is. If you want to separate Profile out to a separate file, then you need to make sure that file gets executed by importing it somewhere (again, the module is a good place).

  1. Inject the AutoMapper instance in your Injectable
@Injectable()
export class UserService {
  constructor(@InjectMapper() private readonly mapper: AutoMapper) {}
}

@InjectMapper() takes in an optional name argument which will tell the decorator which AutoMapper instance to inject. Default to "default"

InjectMapper is imported from nestjsx-automapper. AutoMapper is imported from @nartc/automapper

  1. Use AutoMapper on your models
// ...
const result = await newUser.save();
return this.mapper.map(result.toJSON(), UserVm, User);

Index

Variables

Const AUTOMAPPER

AUTOMAPPER: "nestjs__AUTO_MAPPER__" = "nestjs__AUTO_MAPPER__"

Const MAPPER_MAP

MAPPER_MAP: "nestjs___MAPPER_MAP" = "nestjs___MAPPER_MAP"

Const MapperMap

MapperMap: Map<string, AutoMapper> = new Map<string, AutoMapper>()

Const PROFILE_MAP

PROFILE_MAP: "nestjs__PROFILE_MAP" = "nestjs__PROFILE_MAP"

Const ProfileMap

ProfileMap: Map<Constructible<MappingProfile>, Constructible<MappingProfile>> = new Map<Constructible<MappingProfile>, Constructible<MappingProfile>>()

Functions

Const InjectMapper

  • InjectMapper(name?: undefined | string): (target: object, key: string | symbol, index?: undefined | number) => void
  • Inject the AutoMapper intsance with name.

    default

    default

    Parameters

    • Optional name: undefined | string

      Name of the AutoMapper instance

    Returns (target: object, key: string | symbol, index?: undefined | number) => void

      • (target: object, key: string | symbol, index?: undefined | number): void
      • Parameters

        • target: object
        • key: string | symbol
        • Optional index: undefined | number

        Returns void

Const Profile

  • Profile(name?: undefined | string): (Anonymous function)

Const forRootProviders

Const getMapperToken

  • getMapperToken(name?: undefined | string): string

getWithMapperArgs

  • getWithMapperArgs(args: any[]): [string, undefined | AutoMapperGlobalSettings]

Const withMapperProviders

  • withMapperProviders(mapper: AutoMapper, name: string): Provider[]

Legend

  • Constructor
  • Method
  • Private property
  • Private method
  • Property
  • Static method

Generated using TypeDoc