@automapper/nestjs
v9.0.2
Published
AutoMapper TypeScript NestJS integration
Downloads
290,177
Readme
@automapper/nestjs
This is a NestJS module to integrate @automapper with NestJS.
Installation
npm i @automapper/nestjsor with yarn:
yarn add @automapper/nestjspeerDependencies
@automapper/nestjs depends on @automapper/core
npm i @automapper/coreor with yarn:
yarn add @automapper/coreUsage
Call AutomapperModule.forRoot() and provide some options to initialize the Mapper object(s).
// Single Mapper
@Module({
imports: [
AutomapperModule.forRoot({
strategyInitializer: classes(),
}),
],
})
export class AppModule {}
// Multiple Mappers
@Module({
imports: [
AutomapperModule.forRoot(
[
{
name: 'classes',
strategyInitializer: classes(),
},
{
name: 'pojos',
strategyInitializer: pojos(),
},
],
{
/* globalErrorHandler: ErrorHandler */
/* globalNamingConventions: NamingConvention | {source, destination} */
}
),
],
})
export class AppModule {}AutomapperModule is a Global module so when Mapper object(s) are initialized, they're available across the application.
Interceptors and pipes
MapInterceptor maps route-handler responses, while MapPipe maps incoming @Body() or @Query() values. Both support synchronous and asynchronous mappings, including promise-returning member resolvers and mapping callbacks. Mapping errors propagate through Nest's normal exception handling.
Logging
AutoMapper logs through the global AutoMapperLogger from @automapper/core.
If you use a structured logger, configure it as early as possible in your
application entrypoint:
import { AutoMapperLogger } from '@automapper/core';
AutoMapperLogger.configure({
warn: (message, ...params) => logger.warn(message, ...params),
error: (message, ...params) => logger.error(message, ...params),
});Some AutoMapper logs can happen while decorators run, before
AutomapperModule.forRoot() or forRootAsync() executes. Configure
AutoMapperLogger before importing decorated model classes if those logs need
to use your logger.
For mapper-specific runtime error handling, use errorHandler or
globalErrorHandler. AutomapperModule does not accept a logger option.
Read more about this on the documentation site
