코딩/TypeORM

Methods

춘 몽 2024. 10. 15. 01:06
@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 값이랑 다름
    });
  }

 

 

https://www.inflearn.com/course/lecture?courseSlug=nestjs-%EB%B0%B1%EC%97%94%EB%93%9C-%EC%99%84%EC%A0%84%EC%A0%95%EB%B3%B5-%EB%A7%88%EC%8A%A4%ED%84%B0-%ED%81%B4%EB%9E%98%EC%8A%A4-1&unitId=184158&tab=curriculum&category=questionDetail

 

학습 페이지

 

www.inflearn.com

 

728x90

'코딩 > TypeORM' 카테고리의 다른 글

Find Options  (0) 2024.10.15
Relationship  (0) 2024.10.15
[Entity] Table Inheritance - 클래스 상속을 이용한 테이블 늘리기  (1) 2024.10.11
Transaction  (0) 2024.04.24
사용 방법 예시들  (0) 2024.04.22