PracticeEveryday
TypeORM 본문
Find Options
1. Basic Options
- 모든 Repository와 Manager는 QueryBuilder를 사용하지 않고 find 메서드를 사용해서 필요한 데이터를 쿼리하는 데
사용할 수 있습니다.
- Select : 선택해야 하는 기본 개체의 속성을 나타냅니다.
userRepository.find({
select: {
firstName: true,
lastName: true,
}
})
SELECT "firstname", "lastName" FROM "user";
- Relations : 관계는 메인 엔티티와 함께 로드되어야 합니다. 하위 관계와도 로드 가능합니다.
( join은 leftJoinAndSelect의 약어입니다. )
userRepository.find({
relations: {
profile: true,
phtos: true,
videos: true,
},
})
SELECT * FROM "user"
LEFT JOIN "profile ON "profile"."id" = "user".profileId"
LEFT JOIN "photos" ON "photos"."id" = "user"."photoId"
LEFT JOIN "videos" ON "videos"."id" = "user"."videoId"
userRepository.find({
relations: {
profile: true,
photos: true,
videos: {
videoAttributes: true,
},
},
})
SELECT * FROM "user"
LEFT JOIN "profile" ON "profile"."id" = "user"."profileId"
LEFT JOIN "photos" ON "photos"."id" = "user"."photoId"
LEFT JOIN "videos" ON "videos"."id" = "user"."videoId"
LEFT JOIN "video_attributes" ON "video_attributes"."id" = "videos"."video_attributesId"
- where : 엔티티가 쿼리되어야 하는 간단한 조건을 지정합니다.
userRepository.find({
where: {
firstName: "Timber",
lastName: "Saw",
},
})
SELECT * FROM "user"
WHERE "firstName" = "Timber" AND "lastName" = "Saw";
- OR 문을 사용하는 쿼리
userRepository.find({
where: [
{ firstName: "Timber", lastName: "Saw" },
{ firstNmae: "Stan", lastName: "Lee" },
]
})
SELECT * FROM "user" WHERE ("firstName" = "Timber" AND "listName" = "Saw")
OR ("firstName" = "Stan AND "lastName" = "Lee")
- Order : 선택 정렬
userRepository.find({
order: {
name: "ASC",
id: "DESC",
},
})
SELECT * FROM "user"
ORDER BY "name" ASC, "id" DESC;
- withDeleted : 소프트 삭제된 엔터티를 포함합니다. 기본적으로 소프트 삭제된 엔터티는 포함되지 않습니다.
userRepository.find({
withDeleted: true,
})
- find, findBy, findAndCount, findAndCountBy 여러개의 엔터티를 반환하는 메서드는 다음의 옵션도 포함합니다.
- skip : 엔터티를 가져와야하는 위치에 오프셋 설정을 합니다.
userRepository.find({
skip: 5,
})
SELECT * FROM "user" OFFSET 5
- take : 최대로 가져와야 하는 엔터티 개수를 지정합니다.
userRepository.find({
take: 10,
})
SELECT * FROM "user"
LIMIT 10
- skip과 tack는 함께 사용해야 합니다.
- 만약 MSSQL에서 take나 limit을 하나만 사용하면 에러를 마주하게 될 것입니다.
userRepository.find({
order: {
columnName: "ASC",
},
skip: 0,
take: 10,
})
SELECT * FROM "user"
ORDER BY "columnName" ASC
LIMIT 10 OFFSET 0
'DB' 카테고리의 다른 글
TypeORM 1:1 (0) | 2022.08.23 |
---|---|
TypeORM (0) | 2022.08.13 |
논리적 데이터 모델링 (0) | 2022.08.09 |
개념적 데이터 모델링 (0) | 2022.08.09 |
RDB Modeling (0) | 2022.08.08 |
Comments