[Nest.js] TypeORM @EntityRepository is Deprecated

2023. 12. 1. 17:47ใ†Back-End ์ž‘์—…์‹ค/Nest.js

728x90
๋ฐ˜์‘ํ˜•

 

 

 

NestJS๋กœ ๋ฐฐ์šฐ๋Š” ๋ฐฑ์—”๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ:ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํ™˜๊ฒฝ์˜ ์ฐจ์„ธ๋Œ€ ์„œ๋ฒ„ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋งŒ๋‚˜๋‹ค

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

 

https://github.com/junyharang-coding-study/GraphQL-Study/tree/master/type-script-nest-graph-ql-%EC%8B%A4%EC%8A%B5

 

 

๐Ÿ‘ท‍โ™‚๏ธ ์ž‘์—… ์ค‘์ธ ๋‚ด์šฉ

TypeORM์„ ์ด์šฉํ•œ Repository ๋งŒ๋“œ๋Š” ๋„ ์ค‘


TypeORM์„ ์ด์šฉํ•˜์—ฌ Repository๋ฅผ ๋งŒ๋“œ๋Š” ์ค‘ @EntityRepository๊ฐ€ Deprecated๋œ Issue๋ฅผ ํ™•์ธํ•˜๊ฒŒ ๋˜์—ˆ์–ด์š”.

์ถœ์ฒ˜: https://github.com/junyharang-personal-project/development-community-service-nodejs/blob/master/src/board/dev-inquery/dev-inquery.repository.ts


์˜ˆ์ „์— ์ฒ˜์Œ Nest.js๋ฅผ ๊ณต๋ถ€ํ•  ๋•Œ๋Š” TypeORM 0.2.45 Version์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๊ฐ€ ์—†์—ˆ์ง€๋งŒ,
์ง€๊ธˆ์€ TypeORM 0.3.17 Version์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ด๋Ÿฐ ๋ฌธ์ œ๋ฅผ ๋งž๋‹ฅ๋œจ๋ฆฌ๊ฒŒ ๋˜์—ˆ์–ด์š”.






๐Ÿค” ์ด์œ ๋Š”?

@EntityRepository๋ฅผ Deprecate ์‹œํ‚จ ์ด์œ ๋Š” ๋ฌด์—‡์ผ๊นŒ์š”?

TypeORM์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฐœ๋ฐœ์ž๋“ค์ด Service Layer์—์„œ Business Logic๋งŒ ์ž‘์„ฑํ•˜๊ณ , Repository Layer์—์„œ Query Logic์„ ์ž‘์„ฑ(Data Mapper Pattern)ํ•˜๊ฒŒ ๋ผ์š”.

์ด ๋ถ€๋ถ„์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์–ด์š”.


์œ„์™€ ๊ฐ™์ด ๋ชจ๋“  User๋ฅผ ์กฐํšŒํ•˜๋Š” Logic์„ ๋งŒ๋“ ๋‹ค๊ณ  ํ•  ๋•Œ, TypeORM์—์„œ ํ‘œ์ค€์œผ๋กœ ์ง€์›ํ•˜๋Š” find()๊ฐ€ ์žˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ , ๋™์ผ ์—ญํ• ์„ ํ•˜๋Š” getAllUsers()๋ฅผ Repository Class ๋‚ด๋ถ€์— ๋งŒ๋“ค์–ด์„œ ์ƒ์„ฑํ•˜์˜€์–ด์š”.

์œ„์™€ ๊ฐ™์ด ํ–ˆ์„ ๋•Œ, ์—ฌ๋Ÿฌ ๊ฐœ๋ฐœ์ž๋“ค์ด ์—„๊ฒฉํ•œ Data Mapper Pattern์˜ ์‚ฌ์šฉ์ด ์ข‹์ง€ ์•Š๋‹ค๊ณ  ๋ชฉ์†Œ๋ฆฌ๋ฅผ ๋ƒˆ๊ณ ,
์ด๋ฅผ ํ†ตํ•ด TypeORM ๊ฐœ๋ฐœ์ž๋“ค์€ @EntityRepository Decorator๋ฅผ Deprecated ํ•˜๋ฉฐ, Active Record Pattern ์‚ฌ์šฉ์„ ๊ถŒ์žฅ ํ•˜๋Š” ๋“ฏ ํ•ด ๋ณด์—ฌ์š”.

๋˜ํ•œ, Nest.js์˜ Module์„ฑ๊ณผ ํ™•์žฅ์„ฑ์„ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•จ๋„ ์žˆ์–ด์š”.
Nest.js๋Š” Version Update๋ฅผ ํ†ตํ•ด Framework์˜ ๊ธฐ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๊ณ , ์‚ฌ์šฉ์ž๊ฐ€ ๋” ํšจ๊ณผ์ ์œผ๋กœ Code๋ฅผ ์ž‘์„ฑํ•˜๊ณ ,
์œ ์ง€ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ๋…ธ๋ ฅํ•˜๊ณ  ์žˆ์–ด์š”.

EntityRepository Decorator๋Š” TypeORM์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ Nest.js์—์„œ ์ถ”๊ฐ€๋œ ๊ฒƒ์ด์—์š”.
Nest.js๋Š” ์ฃผ๋กœ ๊ฐ„ํŽธํ•œ Code ๊ตฌ์„ฑ์„ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ Custom Decorator๋ฅผ ์ œ๊ณตํ–ˆ์—ˆ๋Š”๋ฐ,

์ด ์ค‘ ์ผ๋ถ€๋Š” ํ–ฅ ํ›„ Release์—์„œ ๋ณ€๊ฒฝ ๋˜๋Š” Deprecated ๋˜๊ธฐ๋กœ ๊ฒฐ์ • ๋˜์—ˆ๋‹ค๊ณ  ํ•ด์š”.

Nest.js์—์„œ๋Š” ๋ณด๋‹ค ์ผ๋ฐ˜์ ์ด๊ณ , ํ‘œ์ค€์— ๋ถ€ํ•ฉํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ TypeORM์„ ์‚ฌ์šฉํ•˜๊ธธ ๋ฐ”๋ผ๊ณ  ์žˆ์–ด์š”.

๋˜ํ•œ, TypeORM์ด Update ๋˜๋ฉด์„œ Framework์˜
Lifecycle๊ณผ ํ†ตํ•ฉ๋˜๋„๋ก ์„ค๊ณ„๋œ Decorator ๋ฐ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์•„์š”.

Update๋œ Library Version์€ ์ƒˆ๋กœ์šด ๊ธฐ๋Šฅ๊ณผ ๊ฐœ์„  ์‚ฌํ•ญ, ๋ณด์•ˆ ํŒจ์น˜ ๋“ฑ์„ ํฌํ•จํ•˜๊ณ  ์žˆ์„ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๊ธฐ ๋•Œ๋ฌธ์ด์—์š”.


 

 

 

๐Ÿป ๋ฌธ์ œ ํ•ด๊ฒฐ!

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ง์ ‘ Decorator๋ฅผ ๋งŒ๋“ค๊ณ , ํ•ด๋‹น Decorator๊ฐ€ ์ ์šฉ๋œ Repository๋ฅผ ๋ฐ›์•„์ค„ Module์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ํ•ด๊ฒฐ์„ ํ•ด๋ณด๋ ค๊ณ  ํ•ด์š”.


src/app/common/custom/decorator/typeorm-repository.decorator.ts

728x90


์ตœ์ดˆ ์œ„์™€ ๊ฐ™์ด @TypeormRepository Decorator๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์–ด์š”.

์ด ์ฝ”๋“œ๋Š” Nest.js์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ„๋‹จํ•œ Custom Decorator ๋ฐ Metadata ์„ค์ •์„ ์œ„ํ•œ Code์—์š”.

setMetadata()๋Š” Nest.js์—์„œ ์ œ๊ณตํ•˜๋Š” Decorator ์ƒ์„ฑ ํ•จ์ˆ˜์—์š”.
์ด๊ฒƒ์„ ์ด์šฉํ•ด์„œ Metadata ์ƒ์„ฑ์„ ํ•  ์ˆ˜ ์žˆ์–ด์š”.

metadata Key๋กœ ์‚ฌ์šฉ๋œ ๋ฌธ์ž์—ด์€ Metadata์˜ Key๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ƒ์ˆ˜๊ฐ’์ธ๋ฐ,
์ด Key๋ฅผ ํ†ตํ•ด Metadata์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์–ด์š”.
ํ•ด๋‹น Code์—์„œ๋Š” TypeORM๊ณผ ๊ด€๋ จ๋œ Custom Repository์— ๋Œ€ํ•œ Metadata ์„ค์ •์„ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•ด์š”.

TypeormRepository()๋Š” Class Decorator๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋ผ์š”.
์ด ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ฃผ์–ด์ง„ Eneity์— ๋Œ€ํ•œ Custom Repository๋ฅผ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•ด SetMetadata()๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋ผ์š”.
์ฆ‰, ํ•ด๋‹น Class๋ฅผ ์ง€์ •ํ•œ Custom Repository๋กœ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ, ์ด Decorator๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ผ์š”.



src/app/common/custom/module/typeorm-custom.module.ts


์œ„ ์ฝ”๋“œ๋Š” Nest.js์—์„œ TypeORM์˜ Custom Repository๋ฅผ Module๋กœ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ Code์—์š”.

DynamicMoule๊ณผ Provider๋Š” Nest.js์—์„œ Module๊ณผ Provider๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•œ Interface์—์š”.

TypeormCustomMoule Class๋Š” Modul์„ ์ •์˜ ํ•ด์ฃผ๋Š” ์—ญํ• ์„ํ•ด์š”.

forCustomRepository()๋Š” ๋™์ ์œผ๋กœ Custom Repository๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ, ์‚ฌ์šฉํ•˜๋ ค๊ณ  ๋งŒ๋“ค์—ˆ์–ด์š”.
์ด Method๋Š” repositories[]๋ฅผ ๋ง™๋‹ค ํ•ด๋‹น Repository Module์— ์ถ”๊ฐ€ํ•˜๋Š” ์—ญํ• ์„ ํ•ด์š”.

providers[]๋Š” Module์—์„œ ์‚ฌ์šฉํ•  Provider๋ฅผ ๋‹ด๋Š” ๋ฐฐ์—ด์ด์—์š”.

for...of Loop๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „๋‹ฌ๋œ repositories[]๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ Repository์— ๋Œ€ํ•œ Provider๋ฅผ ์ƒ์„ฑํ•ด์ค˜์š”.

Reflect.getMetadata(...) ์ด ๋ถ€๋ถ„์€ TypeormRepository Metadata๋ฅผ ์ฝ์–ด์˜ค๋Š” ๋ถ€๋ถ„์ด์—์š”.
์ด Metadata๋Š” @TypeormRepository Decorator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •๋œ Entity๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ต๋‹ˆ๋‹ค.
๋˜ํ•œ, Metadata Key ๊ฐ’์ธ TYPEORM_CUSTOM_REPOSITORY์— ํ•ด๋‹น ํ•˜๋Š” Entity๋ฅผ ๊ฐ€์ ธ์˜ค๊ฒŒ ๋ผ์š”.

13 ~ 24๋ฒˆ์งธ ์ค„์€ providers[]์— ์œ ์š”ํ•œ Entity์— ๋Œ€ํ•œ Provider๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ์„ ํ™•์ธํ•˜๋Š”๋ฐ,
๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ์— ๊ฑด๋„ˆ๋›ฐ๊ฒŒ ๋˜์–ด์žˆ์–ด์š”.

useFafactory()๋Š” ํ•ด๋‹น Repository์— ๋Œ€ํ•œ Provider๋ฅผ ์ƒ์„ฑํ•˜๋Š” Factory ํ•จ์ˆ˜์—์š”.
์—ฌ๊ธฐ์„œ๋Š” Data Source๋ฅผ ์ฃผ์ž… ๋ฐ›์•„ ํ•ด๋‹น Entity์— ๊ธฐ๋ณธ Repository๋ฅผ ์ƒ์„ฑํ•˜๊ณ ,
์ด๋ฅผ ์ด์šฉํ•ด ์ƒˆ๋กœ์šด Repository๋ฅผ ๋งŒ๋“ค๊ฒŒ ํ•ด ์ฃผ์—ˆ์–ด์š”.

Module์€ DynamicModule ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋˜๊ณ , providers[]๋ฅผ ๋‚ด๋ณด๋‚ด๋ฉฐ,
TypeormCustomModule์„ Module๋กœ ์ง€์ •ํ•˜๊ฒŒ ํ•ด์ฃผ์—ˆ์–ด์š”.

์ด Module์€ forCustomRepository()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ค Custom Repository๋“  ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ ,
์ด๋ฅผ Nest.js Application์— ํ†ตํ•ฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ๊ฒƒ์ด์—์š”.


src/app/equipment/repository/equipment.repository.ts

๋ฐ˜์‘ํ˜•


Deprecated๋œ Decorator๋ฅผ ์‚ญ์ œํ•˜๊ณ , ์œ„์™€ ๊ฐ™์ด Custom Decorator๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์—ˆ์–ด์š”.


src/app.module.ts


๊ทธ๋ฆฌ๊ณ  ์œ„ ์ฝ”๋“œ 30๋ฒˆ์งธ ์ค„๊ณผ ๊ฐ™์ด Custom Module์„ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ์–ด์š”.


src/app/equipment/service/equipment-service-impl.service.ts


๊ธฐ์กด์— @EntityRepository๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค๋ฉด @InjectRepository๋ฅผ Service Layer์—์„œ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ,
์ด์ œ๋Š” ์‚ฌ์šฉํ•˜๋ฉด Error๊ฐ€ ํ„ฐ์งˆ๊ฑฐ์—์š”.

๊ทธ๋ž˜์„œ @InjectRepository Decorator๋ฅผ ์ง€์›Œ์ฃผ์–ด์•ผ ํ•ด์š”.


src/app/equipment/service/equipment-service-impl.service.ts

 

 

 

 

 

NestJS๋กœ ๋ฐฐ์šฐ๋Š” ๋ฐฑ์—”๋“œ ํ”„๋กœ๊ทธ๋ž˜๋ฐ:ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํ™˜๊ฒฝ์˜ ์ฐจ์„ธ๋Œ€ ์„œ๋ฒ„ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋งŒ๋‚˜๋‹ค

COUPANG

www.coupang.com

"์ด ํฌ์ŠคํŒ…์€ ์ฟ ํŒก ํŒŒํŠธ๋„ˆ์Šค ํ™œ๋™์˜ ์ผํ™˜์œผ๋กœ, ์ด์— ๋”ฐ๋ฅธ ์ผ์ •์•ก์˜ ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ์ œ๊ณต๋ฐ›์Šต๋‹ˆ๋‹ค."

 

 

 

https://github.com/junyharang-coding-study/GraphQL-Study/tree/master/type-script-nest-graph-ql-%EC%8B%A4%EC%8A%B5



 


 

๐Ÿง ์ฐธ๊ณ  ์ž๋ฃŒ

 

How to workraound this TypeORM error, "EntityRepository is deprecated , use Repository.extend function instead"?

However, I can't find any Repository.extend method in Repository class and there's nothing about it in the documentation. How to solve this? typeorm version: "^0.3.0" I'm using nest js and

stackoverflow.com

 

 

 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•