title: NestJs - DTO and Entities
body: <p>I'm trying to use cleverly DTO and Entities in my project but it seems more complicated than it should be. I'm building a backend for managing inventory, I use NestJs and TypeOrm.</p>
  <p>My client is sending me a set of data throught a POST request, let's say :</p>
  <pre><code>{
    &quot;length&quot;: 25,
    &quot;quantity&quot;: 100,
    &quot;connector_A&quot;: {
      &quot;id&quot;: &quot;9244e41c-9da7-45b4-a1e4-4498bb9de6de&quot;
    },
    &quot;connector_B&quot;: {
      &quot;id&quot;: &quot;48426cf0-de41-499b-9c02-94c224392448&quot;
    },
    &quot;category&quot;: {
      &quot;id&quot;: &quot;f961d67f-aea0-48a3-b298-b2f78be18f1f&quot;
    }
  }
  </code></pre>
  <p>My controller has for responsability to check the field by using a custom ValidationPipe :</p>
  <pre><code>@Post()
    @UsePipes(new ValidationPipe())
    create(@Body() data: CableDto) {
      return this.cablesService.create(data);
  }
  </code></pre>
  <p>I read in many places that in best practices, RAW data should be converted into DTO and when it comes to data insertion, I should cast my DTO into an typeOrm Entity.</p>
  <p>I'm ok with this method, but I found it very complicated, even more when there are relations between my tables and prefix noun into it.</p>
  <p>Here is my Entity cable</p>
  <pre><code>@Entity('t_cable')
  export class Cable {
  
    @PrimaryGeneratedColumn('uuid')
    CAB_Id: string;
  
    @Column({
      type: &quot;double&quot;
    })
    CAB_Length: number;
  
    @Column({
      type: &quot;int&quot;
    })
    CAB_Quantity: number;
  
    @Column()
    CON_Id_A: string
  
    @Column()
    CON_Id_B: string
    
    @Column()
    CAT_Id: string
  
    @ManyToOne(type =&gt; Connector, connector =&gt; connector.CON_Id_A)
    @JoinColumn({ name: &quot;CON_Id_A&quot; })
    CON_A: Connector;
  
    @ManyToOne(type =&gt; Connector, connector =&gt; connector.CON_Id_B)
    @JoinColumn({ name: &quot;CON_Id_B&quot; })
    CON_B: Connector;
  
    @ManyToOne(type =&gt; Category, category =&gt; category.CAB_CAT_Id)
    @JoinColumn({ name: &quot;CAT_Id&quot; })
    CAT: Category;
  
  }
  </code></pre>
  <p>And here is my DTO for the cable interactions :</p>
  <pre><code>export class CableDto {
  
    id: string;
  
    @IsOptional()
    @IsPositive()
    @Max(1000)
    length: number;
    quantity: number;
  
    connector_A: ConnectorDto;
    connector_B: ConnectorDto;
    category: CategoryDto
  
    public static from(dto: Partial&lt;CableDto&gt;) {
      const it = new CableDto();
      it.id = dto.id;
      it.length = dto.length;
      it.quantity = dto.quantity;
      it.connector_A = dto.connector_A
      it.connector_B = dto.connector_B
      it.category = dto.category
      return it;
    }
  
    public static fromEntity(entity: Cable) {
      return this.from({
        id: entity.CAB_Id,
        length: entity.CAB_Length,
        quantity: entity.CAB_Quantity,
        connector_A: ConnectorDto.fromEntity(entity.CON_A),
        connector_B: ConnectorDto.fromEntity(entity.CON_B),
         category: CategoryDto.fromEntity(entity.CAT)
      });
    }
  
    public static toEntity(dto: Partial&lt;CableDto&gt;) {
      const it = new Cable();
      if (dto.hasOwnProperty('length')) {
        it.CAB_Length = dto.length;
      }
      if (dto.hasOwnProperty('quantity')) {
        it.CAB_Quantity = dto.quantity;
      }
      if (dto.hasOwnProperty('connector_A')) {
        it.CON_Id_A = dto.connector_A.id;
      }
      if (dto.hasOwnProperty('connector_B')) {
        it.CON_Id_B = dto.connector_B.id;
      }
      if (dto.hasOwnProperty('category')) {
        it.CAT_Id = dto.category.id;
      }
      return it;
    }
  }
  </code></pre>
  <p>I know that these three methods to convert in both directions DTOs and entites feels pretty dirty, that's why I'm here ..</p>
  <p>My service for a simple create or get request know :</p>
  <pre><code>async create(dto: CableDto): Promise&lt;CableDto&gt; {
    const cable = await this.cablesRepository.save(CableDto.toEntity(dto));
    return await this.findById(cable.CAB_Id)
  }
  </code></pre>
  <p>I'm convince that there is simpler solution to achieve that, or at least a proper way to do it.</p>
  <p>Any idea ?</p>
  <p>Thank you.</p>