ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Nestjs-TypeOrm에서 getRawMany 와 getMany
    Nest.js 2023. 10. 11. 15:13
    728x90

    getRawMany

    페이징 처리시 : .limit, .offset 사용

     

    getMany

    페이징 처리시 : .take, .skip 사용

     

    TypeORM의 getRawMany() 메서드는 쿼리 결과를 직접적으로 반환하는 반면, getMany() 메서드는 Entity 객체를 반환한다.

    TypeORM의 Tree 방식으로 출력가능한 getMany() 메소드 대신 alias 못씀

     

    getMany

    let query = await this.postsRepository
    			.createQueryBuilder('a')
    			.select(['a.id AS "id"'])
    			.leftJoin('a.category', 'category')
    			.leftJoin('a.user', 'user')
    			.where('a.title LIKE :keyword', { keyword: `%${keyword}%` })
    			.getMany();

    as alias 안먹음

     

    getRawMany

    let query = await this.postsRepository
    			.createQueryBuilder('a')
    			.select(['a.id AS "tid"'])
    			.leftJoin('a.category', 'category')
    			.leftJoin('a.user', 'user')
    			.where('a.title LIKE :keyword', { keyword: `%${keyword}%` })
    			.getRawMany();

    as alias 먹음

     

     

    getMany

    관계형 entity 그대로 가져올때 사용

    {
                "id": "b6fc6b0e-7ac9-48ff-aff7-015b8ff02282",
                "createdAt": "2023-10-10T06:58:11.086Z",
                "updatedAt": "2023-10-10T06:58:11.086Z",
                "contents": "contenst10",
                "isPublic": true,
                "groupId": "75aca3da-1dac-48ef-84b8-cdf1be8fe37d",
                "memberId": "31a3ff5a-3715-43ca-a419-2b949a0dee53",
                "medias": [
                    {
                        "id": "4e292e04-715d-45a0-b5e0-d95261ce0551",
                        "createdAt": "2023-10-10T06:58:11.112Z",
                        "updatedAt": "2023-10-10T06:58:11.112Z",
                        "feedId": "b6fc6b0e-7ac9-48ff-aff7-015b8ff02282",
                        "url": "test2",
                        "position": "1"
                    },
                    {
                        "id": "686b03b8-28bd-45c8-aa7b-f67e8d917f69",
                        "createdAt": "2023-10-10T06:58:11.111Z",
                        "updatedAt": "2023-10-10T06:58:11.111Z",
                        "feedId": "b6fc6b0e-7ac9-48ff-aff7-015b8ff02282",
                        "url": "test",
                        "position": "0"
                    }
                ]
            }

     

    getRawMany

    쿼리 결과 그대로 가져오고 싶을때 사용

      {
                "a_id": "b6fc6b0e-7ac9-48ff-aff7-015b8ff02282",
                "a_createdAt": "2023-10-10T06:58:11.086Z",
                "a_updatedAt": "2023-10-10T06:58:11.086Z",
                "a_contents": "contenst10",
                "a_isPublic": true,
                "a_groupId": "75aca3da-1dac-48ef-84b8-cdf1be8fe37d",
                "a_memberId": "31a3ff5a-3715-43ca-a419-2b949a0dee53",
                "medias_id": "4e292e04-715d-45a0-b5e0-d95261ce0551",
                "medias_createdAt": "2023-10-10T06:58:11.112Z",
                "medias_updatedAt": "2023-10-10T06:58:11.112Z",
                "medias_feedId": "b6fc6b0e-7ac9-48ff-aff7-015b8ff02282",
                "medias_url": "test2",
                "medias_position": "1"
            },
            {
                "a_id": "b6fc6b0e-7ac9-48ff-aff7-015b8ff02282",
                "a_createdAt": "2023-10-10T06:58:11.086Z",
                "a_updatedAt": "2023-10-10T06:58:11.086Z",
                "a_contents": "contenst10",
                "a_isPublic": true,
                "a_groupId": "75aca3da-1dac-48ef-84b8-cdf1be8fe37d",
                "a_memberId": "31a3ff5a-3715-43ca-a419-2b949a0dee53",
                "medias_id": "686b03b8-28bd-45c8-aa7b-f67e8d917f69",
                "medias_createdAt": "2023-10-10T06:58:11.111Z",
                "medias_updatedAt": "2023-10-10T06:58:11.111Z",
                "medias_feedId": "b6fc6b0e-7ac9-48ff-aff7-015b8ff02282",
                "medias_url": "test",
                "medias_position": "0"
            }

     

     

    getMany

    서브쿼리 안먹음

    .addSelect((qb) => {
            return qb
              .select('b.url')
              .from(FeedMediaEntity, 'b')
              .where('b.feedId = a.id')
              .orderBy('b.position', 'ASC')
              .limit(1);
          }, 'thumbnail')

    아무것도 결과에 안나옴. 당연한듯 반환값이 FeedEntity니까...

     

    getRawMany

    .addSelect((qb) => {
            return qb
              .select('b.url')
              .from(FeedMediaEntity, 'b')
              .where('b.feedId = a.id')
              .orderBy('b.position', 'ASC')
              .limit(1);
          }, 'thumbnail')

    thumbnail 반환 잘나옴. 당연함 쿼리 결과 그대로 출력 해주니까

    728x90
Designed by Tistory.