반응형
SMALL
GraphQLModule.forRoot()
: root module 설정typescript나 NestJS에서 데이터베이스와 통신하기 위해서는 ORM 필요
.env: 환경 변수 이용
entity: 데이터베이스에 저장되는 데이터의 형태를 보여주는 모델 ( DB 테이블에 매핑되는 클래스)
@ObjectTypes()
자동으로 스키마를 빌드하기 위해 사용하는 decorator- Entity(): TypeORM이 DB에 저장하도록 해줌
Data Mapper vs Active Record
- DB랑 상호작용할 때 쓰는 패턴
- Active Record : 소규모 앱에서 단순하게 사용
- Data Mapper : 유지 관리에 도움, 대규모 앱에서 유용 (NestJS에서 이용 방법)
- Repository: entity와 상호작용을 담당
DB 구현
TypeOrmModule.forRoot({entities:[Restaurant]})
=> (in app.module.ts) Restaurants가 DB가 되도록 함- repository를 inject하고 나면 restaurants.module에서 모든게 돌아감
imports:[TypeOrmModule.forFeature([Restaurant])]
=> restaurants.module.ts에서 repository를 import- forFeature: TypeOrmModule이 특정 feature를 import할 수 있게 해줌
- restaurants.module.ts의 providers에 RestaurantsService 추가 =>service에 접근 가능해짐
constructor(private readonly restaurantService:RestaurantService){}
=> restaurantservice를 restaurantresolver에 import
=> Restaurant entity의 repository를 inject하는 것, 이름은 restaurants이고 class는 Restaurant entity를 가진 repository (this.restaurants.__ 으로 접근 가능해짐 )constructor( @InjectRepository(Restaurant) private readonly restaurants: Repository<Restaurant>, ){}
Mapped types
- base type을 바탕으로 다른 버전들을 만들 수 있게 해줌
- partialtype: 모든 property가 옵션사항
- omitType : base class에서 class를 만드는데 몇몇 field를 제외하고 만듦
- 2번째인자로 type 전달
=> Restaurant는 objectType인데 omittype은 InputType허용 => 2번째 인자로 decorator 전달해주면 그 decorator로 변환시켜줌@InputType() export class CreateRestaurantDto extends OmitType(Restaurant, ["id"],InputType) {}
- restaurants.entity.ts에
@InputType({isAbstract:true})
추가
=> @InputType()만 쓴다면 ObjectType이랑 동시에 쓸 수 없음 => ()안에 {isAbstract:true}를 적용하면 이 InputType이 스키마에 포함되지 않고 어디선가 복사해서 씀(직접 쓴다는게 아니라 어떤것으로 확장시킨다는 의미)
User
enum: enumerable(열거한다)
enum UserRole{ Owner, Client, Delivery }
listners: entity에 무슨 일이 생길 때 실행됨
- 특정 entity event를 listen하는 사용자 로직이 있는 method를 가질 수 있음
@AfterLoad()
:post를 load할 때마다, load한 다음에 뭔가 다른 것을 할 수 있음@BeforeInsert()
: Entity안에 어떤 이름이든지 메서드를 정의하고, BeforeInsert라고 mark할 수 있음 (typeORM은 entity가 insert 되기 전에 이걸 불러줌 )
Authentication
- json webtoken의 목적은 비밀 유지가 아닌 우리만이 유효한 인증을 할 수 있게 하는 것임 (정보의 진위 여부가 중요)
npm i jsonwebtoken
,npm i @types/jsonwebtoken --only-dev
token을 만들기 위함- config service 적용: dependency injection에 의함
- dependency injection: 우리가 원하는 것의 class만 적어주면 nestjs가 우리를 위해서 그 정보를 가져다줌
- uers.module에서 ConfigService를 적어준 것만으로 우리가 원하는 것을 불러올 수 있게 됨
- Static Module vs Dynamic Module
- static module: 어떠한 설정도 적용되어 있지 않음
- dynamic module: GraphQLModule처럼 설정이 존재함
반응형
LIST