210830 ๊ฐœ๋ฐœ๊ธฐ๋ก: nestjs ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…

2021. 8. 31. 07:30ใ†๐Ÿ“” TIL

๐Ÿ—“ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์˜ ํ•„์š”์„ฑ

@Injectable()
export class HospitalService {
  constructor(
    @InjectRepository(Questions)
    private questionRepository: Repository<Questions>,
    @InjectRepository(Result) // ์ด๊ฒŒ ์—†์œผ๋ฉด dist ํŒŒ์ผ์— ์—”ํ‹ฐํ‹ฐ ์ƒ์„ฑ์•ˆ๋œ๋‹ค.
    private resultRepository: Repository<Result>,
    @InjectRepository(Answers)
    private answersRepository: Repository<Answers>
  ) {
  }

typeorm model generator ๋ฅผ ํ†ตํ•ด DB์˜ ํ…Œ์ด๋ธ”์„ nestjs์˜ ์—”ํ‹ฐํ‹ฐ๋กœ ๊ฐ€์ ธ์™”๋‹ค.

๊ณต์‹๋ฌธ์„œ์—์„œ @InjectRepository() ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š”, ๋ชจ๋“ˆ์—์„œ forFeature()์— ๋“ฑ๋ก๋œ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์„œ๋น„์Šค์— ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•˜๋‹ค๊ณ  ์„ค๋ช…ํ•œ๋‹ค.

This module uses the forFeature() method to define which repositories are registered in the current scope. With that in place, we can inject the UsersRepository into the UsersService using the @InjectRepository() decorator:

 

 

 

 

๐Ÿ—’  ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ return ์ด ํ•„์š”ํ•˜๋‹ค

๋„ˆ๋ฌด ์–ด์ด์—†๋Š” ์‹ค์ˆ˜์ธ๋ฐ, result ๋ณ€์ˆ˜์— filter์˜ ์กฐ๊ฑด์— ํ•ด๋‹น๋˜๋Š” ์š”์†Œ๊ฐ€ ๋‹ด๊ธฐ์ง€ ์•Š๊ณ , ๊ณ„์† ๋นˆ ๋ฐฐ์—ด๋งŒ ๋ฆฌํ„ด๋˜์—ˆ๋‹ค. 

์ฒ˜์Œ์—” Object.keys(obj) ๊ฐ€ ๋ฐฐ์—ด ํ˜•ํƒœ๊ฐ€ ์•„๋‹ˆ์ธ๊ฐ€..? ํ•˜๋Š” ์˜์‹ฌ์ด ๋“ค์—ˆ๋Š”๋ฐ, ๊ทธ๋ƒฅ return ์„ ๋นผ๋จน์€์ฑ„ ์กฐ๊ฑด๋ฌธ(obj[key] > 1) ๋งŒ ์ ์–ด๋†”์„œ ์ƒ๊ธด ๋ฌธ์ œ์˜€๋‹ค.

const result = Object.keys(obj).filter(key => {
    return obj[key] > 1;  // ๋ˆ forget return
});

์–ด์ด์—†๋Š” ์‹ค์ˆ˜๋Š” ๋‹ค์‹œ ๋ฒ”ํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ธฐ๋ก  โœ๏ธ

 

 

 

๐Ÿ“Œ  ํด๋ฆฐ์ฝ”๋“œ 

์งฌ์งฌ์ด ์‹œ๊ฐ„์„ ๋‚ด์„œ ํด๋ฆฐ์ฝ”๋“œ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๊ธ€์„ ์ฝ๊ณ ์žˆ๋Š”๋ฐ, ์‹ค์ œ ํ”„๋กœ์ ํŠธ์— ๋„์ž…ํ•  ๋งŒํ•œ ๋‚ด์šฉ์ด ๋งŽ๋‹ค.

์ž˜ ๋ชจ๋ฅด๋Š” enum์„ ๋„์ž…ํ•ด๋ณธ๋‹ค๋˜๊ฐ€, ๊ธ€์—์„œ ์„ค๋ช…ํ•˜๋Š” bad case ๊ฐ€ ๋‚ด ์ฝ”๋“œ์— ์žˆ์ง„ ์•Š์€์ง€, ์ข‹์€ ์•„ํ‹ฐํด๊ณผ ์†Œ์Šค๋ฅผ ์ฝ๋‹ค๋ณด๋‹ˆ ์ฝ”๋“œ์˜ ๊ฐœ์„  ์—ฌ์ง€๋ฅผ ๋Š์ž„์—†์ด ํƒ๊ตฌํ•˜๊ฒŒ ๋œ๋‹ค. ์‹ค์ œ๋กœ ๊ธฐ์กด์ฝ”๋“œ์—์„œ ํด๋ฆฐ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝํ•œ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ „๋ถ€ ๊ธฐ๋กํ•ด๋†“๊ณ  ๊ธ€๋กœ ์ž‘์„ฑํ•ด๋ด์•ผ๊ฒ ๋‹ค.