I'm developing a Nest.js and I use different packages such as @ttshivers/automapper to map between entities and dtos. Since decorating class members with @Automap() is verbose, I followed the steps in the documentation to enable transformer plugins to automatically perform mapping. So for projects using nest-cli, I had to configure plugins in compilerOptions according documentation here and as shown below:
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"builder": "swc",
"typeCheck": true,
"plugins": ["@ttshivers/automapper-classes/transformer-plugin"],
"assets": ["infrastructure/services/support/mail/templates/**/*"],
"watchAssets": true
}
}
Here's an example showing all records but with not info:
Here's my Entity:
import {
Column,
CreateDateColumn,
Entity,
OneToMany,
PrimaryGeneratedColumn,
Relation,
UpdateDateColumn,
} from 'typeorm';
import { Persona } from './persona.entity';
@Entity('paises')
export class Pais {
@PrimaryGeneratedColumn()
id: number;
@Column({
length: 100,
})
nombre: string;
@Column({
length: 10,
})
codigo: string;
@Column({
length: 3,
})
abbreviatura: string;
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn({
nullable: true,
})
updatedAt?: Date;
@OneToMany(() => Persona, (persona) => persona.pais, { cascade: false })
personas?: Relation<Persona[]>;
}
and here's my dto:
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsNumber, IsOptional, MaxLength } from 'class-validator';
import { Type } from 'class-transformer';
export class PaisDto {
@ApiProperty()
@IsNumber()
id: number;
@ApiProperty()
@IsNotEmpty()
@MaxLength(100)
nombre: string;
@ApiProperty()
@MaxLength(10)
@IsOptional()
codigo?: string;
@ApiProperty()
@MaxLength(3)
@IsOptional()
abbreviatura?: string;
@ApiProperty()
@Type(() => Date)
createdAt: Date;
@ApiProperty({
nullable: true,
})
@Type(() => Date)
@IsOptional()
updatedAt?: Date | null;
}
Here's is the profile:
import { Mapper, createMap } from '@ttshivers/automapper-core';
import { Injectable } from '@nestjs/common';
import { AutomapperProfile, InjectMapper } from '@ttshivers/automapper-nestjs';
// Domain
import { Pais } from '@domain/entities/pais.entity';
// Application
import { PaisDto } from '@application/models/pais.dto';
/* istanbul ignore file */
@Injectable()
export class PaisProfile extends AutomapperProfile {
constructor(@InjectMapper() mapper: Mapper) {
super(mapper);
}
get profile() {
return (mapper) => {
createMap(mapper, Pais, PaisDto);
};
}
}
As you can see, it's not necessary to add @Automap() decorators, not even use forMember in the profile mapper to do the right field mapping because properties in entity class and dto class are the same.
Before installing and configuring SWC as the builder, the mapping was working fine and before I rollback changes I would like to know if somebody has faced or is facing the same or similar issue.
A workaround is to use @Automap() decorator, but I wouldn't like.
