Skip to main content

Command Palette

Search for a command to run...

[TIL] Custom Pipe

06/03/23

Updated
[TIL] Custom Pipe

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.

  1. Import Transform from '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.

  1. 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 data object 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.

More from this blog

[์ฝ”ํ…Œ] ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ - ๋ฌด์ง€์˜ ๋จน๋ฐฉ ๋ผ์ด๋ธŒ

https://school.programmers.co.kr/learn/courses/30/lessons/42891 ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ์— ์‹ ๊ฒฝ์จ์•ผ ํ•˜๋Š” ๋ฌธ์ œ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ํ™œ์šฉํ•ด์„œ ๋จน๋Š” ์‹œ๊ฐ„์ด ์งง์€ ์Œ์‹๋ถ€ํ„ฐ ํ์—์„œ ๋นผ๊ธฐ import heapq # ์šฐ์„ ์ˆœ์œ„ํ ํ™œ์šฉ: food_time์ด ์งง์€ ์Œ์‹๋ถ€ํ„ฐ ์‚ญ์ œ def solution(food_times, k): if sum(food_times) <= k: return -1 ...

Apr 4, 2024
[์ฝ”ํ…Œ] ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ - ๋ฌด์ง€์˜ ๋จน๋ฐฉ ๋ผ์ด๋ธŒ

[์ฝ”ํ…Œ] ์—ฌํ–‰๊ฒฝ๋กœ

๐Ÿ’ก [์ถœ๋ฐœ์ง€, ๋„์ฐฉ์ง€] ํ˜•ํƒœ๋กœ ์ฃผ์–ด์ง„ ๋น„ํ–‰๊ธฐ ํ‹ฐ์ผ“์„ ํ†ตํ•ด ๋ชจ๋“  ํ‹ฐ์ผ“์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ๊ณตํ•ญ์„ ๋ฐฉ๋ฌธ ์ˆœ์„œ ๊ตฌํ•˜๊ธฐ (๋‹จ, ์—ฌ๋Ÿฌ ๊ณตํ•ญ์„ ๋ฐฉ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ ์•ŒํŒŒ๋ฒณ์ด ๋น ๋ฅธ ๊ณตํ•ญ๋ถ€ํ„ฐ ๋ฐฉ๋ฌธํ•œ๋‹ค.) ํ‹€๋ ธ๋˜ ์ฝ”๋“œ from collections import defaultdict def dfs(graph, route, depart): if graph[depart]: connected = graph[depart][0] ...

Feb 26, 2024
[์ฝ”ํ…Œ] ์—ฌํ–‰๊ฒฝ๋กœ

siwon.log

161 posts