[TIL] Custom Pipe
06/03/23
![[TIL] Custom Pipe](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1688368917629%2F54a91a6d-f82f-434d-9ce4-92a254778fa7.png&w=3840&q=75)
Issue #1
When resident registration number data is received, I want to use a pipe to determine the gender and pass it to the database.
๐ What I tried
To determine the gender based on the resident registration number and assign it to the DTO, I tried using the @Transform() decorator.
- Import
Transformfrom'class-transformer':
import { Transform } from 'class-transformer';
In the DTO, I used @Transform() to determine the gender based on the resident registration number and assign it to the gender property:
@IsNotEmpty()
@Transform(({ obj }) => {
const patientRrn = String(obj.patient_rrn);
return patientRrn[7] === '1' || patientRrn[7] === '3' ? 'M' : 'F';
})
gender: string;
However, it didn't work as expected. It turns out I misunderstood the purpose of the @Transform() decorator.
Transform #
You can perform additional data transformation using the @Transform() decorator.
@Transform(({ value }) => value.name)
role: RoleEntity;
For example, the following construct returns the name property of the RoleEntity instead of returning the whole object.
According to the official documentation, when using @Transform(), it extracts only the name property of the RoleEntity assigned to the role variable in the request. In other words, it returns only role.name.
Since @Transform() didn't meet my requirements, I thought of another approach.
- Using a custom pipe: The official documentation on pipes provides a clear explanation. What I wanted to achieve was that when the resident registration number data is passed as the body data in the controller, it would be processed by the pipe first to determine the gender and add the gender to the
dataobject before returning it.
In the controller, I used the custom pipe GenderFromRrnPipe() to automatically determine the gender when the resident registration number is received:
// patients.controller.ts
@Post('/:report_id')
createPatientInfo(
@Param('report_id') report_id: number,
@Body(new GenderFromRrnPipe()) createPatientInfo: CreatePatientDto,
): Promise<Patients> {
this.logger.verbose('Create Patient Information POST API');
return this.patientsService.createPatientInfo(report_id, createPatientInfo);
}
// custom pipe
import { PipeTransform, Injectable } from '@nestjs/common';
@Injectable()
export class GenderFromRrnPipe implements PipeTransform {
transform(value: any): object {
const rrn = value.patient_rrn;
const gender =
rrn[7] === '1' || rrn[7] === '3'
? 'M'
: rrn[7] === '2' || rrn[7] === '4'
? 'F'
: null;
return {
...value,
gender,
};
}
}
By passing through the pipe, the data is transformed accordingly.
![[์ฝํ
] ๊ทธ๋ฆฌ๋ ๋ฌธ์ - ๋ฌด์ง์ ๋จน๋ฐฉ ๋ผ์ด๋ธ](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1712215455263%2F1ac1f35a-8862-4e42-8d0c-e2bea01e04c0.png&w=3840&q=75)
![[์ฝํ
] Bfs ํ ๋งํ](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709032619170%2F70056896-c857-444b-9c99-45bfcb466806.png&w=3840&q=75)
![[์ฝํ
] Dfs ๋ฌธ์ ์ ํ - ๊ทธ๋ํ ๋ด์์ ๊ตฌ๋ถํ์ฌ ์นด์ดํธ ํ๊ธฐ](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709019361383%2Fb0585d72-c808-4169-83a9-2724f312e927.png&w=3840&q=75)
![[์ฝํ
] DFS vs BFS](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971211123%2F71f9386c-6a62-43b2-a602-4d084c24d6cf.png&w=3840&q=75)
![[์ฝํ
] ์ฌํ๊ฒฝ๋ก](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971251412%2F27ce72ed-8ee7-4d13-a02f-ff4bbe50c4be.png&w=3840&q=75)