In my project, I'm currently experimenting with utilizing DTOs and Entities in a clever manner. However, I find it more challenging than expected as I develop a backend system for inventory management using NestJs and TypeOrm.
When my client sends me a set of data via a POST request, the structure looks something like this:
{
"length": 25,
"quantity": 100,
"connector_A": {
"id": "9244e41c-9da7-45b4-a1e4-4498bb9de6de"
},
"connector_B": {
"id": "48426cf0-de41-499b-9c02-94c224392448"
},
"category": {
"id": "f961d67f-aea0-48a3-b298-b2f78be18f1f"
}
}
My controller's responsibility is to validate the incoming data using a custom ValidationPipe:
@Post()
@UsePipes(new ValidationPipe())
create(@Body() data: CableDto) {
return this.cablesService.create(data);
}
In accordance with best practices, it is recommended to convert raw data into DTOs before casting them into TypeOrm entities during data insertion.
While I understand and agree with this approach, I must admit that I find it quite complex, especially when dealing with table relations and prefix nouns.
Here is an example of my cable Entity:
@Entity('t_cable')
export class Cable {
@PrimaryGeneratedColumn('uuid')
CAB_Id: string;
@Column({
type: "double"
})
CAB_Length: number;
@Column({
type: "int"
})
CAB_Quantity: number;
@Column()
CON_Id_A: string
@Column()
CON_Id_B: string
@Column()
CAT_Id: string
@ManyToOne(type => Connector, connector => connector.CON_Id_A)
@JoinColumn({ name: "CON_Id_A" })
CON_A: Connector;
@ManyToOne(type => Connector, connector => connector.CON_Id_B)
@JoinColumn({ name: "CON_Id_B" })
CON_B: Connector;
@ManyToOne(type => Category, category => category.CAB_CAT_Id)
@JoinColumn({ name: "CAT_Id" })
CAT: Category;
}
And here is the corresponding DTO for cable interactions:
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<CableDto>) {
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<CableDto>) {
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;
}
}
While these conversion methods between DTOs and entities may seem cumbersome, I believe there might be a simpler or more efficient solution out there.
Do you have any suggestions or insights on how to streamline this process better?
Thank you!