typeORM : soft-delete

김세겸·2023년 1월 3일
0

code-camp

목록 보기
9/10

1. resolver

@Mutation(() => Boolean)
deleteProduct(
	@Args('productId') productId: string
) {
	return this.productService.delete({productId});
}

2. service

async delete({productId}) {
// 1. 실제 삭제
	const result = await this.productRepository.delete({productID});
    return result.affected ? true : false;
    
// 2. 소프트 삭제 - isDeleted
	this.productRepository.update({id: productId}, {isDeleted: true});
    
// 3. 소프트 삭제 - deletedAt : Date
	this.productRepository.update({id: productId}, {deletedAt: new Date()});
    
// 4. TypeORM 제공 - softRemove // id 로만 삭제 가능
	this.productRepository.softRemove({id: productId});
   	// 조회 할 때 또한 자동으로 걸러서 가져와줌
    
// 4. TypeORM 제공 - softDelete // 다른조건 가능
	this.productRepository.softDelete({id: productId});
   	// 조회 할 때 또한 자동으로 걸러서 가져와줌
}

3. soft-delete

isDeleted => boolean
deletedAt => Date
TypeORM 제공 - softRemove => @DeleteDateColumn()
TypeORM 제공 - softDelete => @DeleteDateColumn()

4. save vs update

save는 수정 및 생성 후 객체 자체를 받아옴
update는 영향을 받은 아이가 몇인지 알려줌

0개의 댓글