728x90
@Post('sample')
async sample() {
// 모델에 해당되는 객체 생성 - 저장은 안함
const user1 = this.userRepository.create({
email: 'test@codefactory.ai',
});
// 저장
const user2 = await this.userRepository.save({
email: 'test@codefactory.ai',
});
// preload
// 입력된 값을 기반으로 데이터베이스에 있는 데이터를 불러오고
// 추가 입력된 값으로 데이터베이스에서 가져온 값들을 대체함
// 저장하지는 않음
// find + create
const user3 = await this.userRepository.preload({
id: 101,
email: 'codefactory@codefactory.ai',
});
// 삭제하기
await this.userRepository.delete(101);
// 값을 증가시킴
await this.userRepository.increment(
{
id: 1,
},
'count',
2,
);
// 값을 감소시킴
await this.userRepository.decrement(
{
id: 1,
},
'count',
1,
);
// 갯수 카운팅하기
const count = await this.userRepository.count({
where: {
email: ILike('%0%'),
},
});
// sum
const sum = await this.userRepository.sum('count', {
email: ILike('%0%'),
});
// average
const average = await this.userRepository.average('count', {
id: LessThan(4),
});
// 최소값
const min = await this.userRepository.minimum('count', {
id: LessThanOrEqual(4),
});
// 최대값
const max = await this.userRepository.maximum('count', {
id: MoreThan(5),
});
// find
const user = await this.userRepository.find({});
// findOne
const userOne = await this.userRepository.findOne({
where: {
id: 3,
},
});
// 페이지 네이션 => 가져오는 데이터 수 제한걸어서 가져오고, 전체 데이터 수 같이 알려줌.
const usersAndCount = await this.userRepository.findAndCount({
take: 5, // 가져오는 갯수 제한이라 count 값이랑 다름
});
}
++
- exists()
중복체크용 - find문과 똑같고, 결과값으로 있으면 true 반환
- upsert()
update + insert, save는 찾은 후 넣는식(왔다갔다 통신), upsert는 바로 넣는 식(바로)
put 요청이랑 맞을 듯
728x90
'코딩 > TypeORM' 카테고리의 다른 글
Query Builder (0) | 2024.10.22 |
---|---|
통계 (0) | 2024.10.22 |
Find Options (0) | 2024.10.15 |
Relationship (0) | 2024.10.15 |
[Entity] Table Inheritance - 클래스 상속을 이용한 테이블 늘리기 (1) | 2024.10.11 |