공식 문서 : https://typeorm.io/find-options
모든 find로 시작하는 함수에 가능한 옵션들
userRepository.find({
select: {
firstName: true,
lastName: true,
},
})
userRepository.find({
relations: {
profile: true,
photos: true,
videos: true,
},
})
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"
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"
userRepository.find({
where: {
firstName: "Timber",
lastName: "Saw",
},
})
userRepository.find({
relations: {
project: true,
},
where: {
project: {
name: "TypeORM",
initials: "TORM",
},
},
})
이 것은
SELECT * FROM "user"
LEFT JOIN "project" ON "project"."id" = "user"."projectId"
WHERE "project"."name" = 'TypeORM' AND "project"."initials" = 'TORM'
와 같다고 한다.
userRepository.find({
where: [
{ firstName: "Timber", lastName: "Saw" },
{ firstName: "Stan", lastName: "Lee" },
],
})
userRepository.find({
order: {
name: "ASC",
id: "DESC",
},
})
userRepository.find({
skip: 5,
})
userRepository.find({
take: 10,
})
userRepository.find({
order: {
columnName: "ASC",
},
skip: 0,
take: 10,
})
import { Not } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: Not("About #1"),
})
import { LessThan } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
likes: LessThan(10),
})
import { Equal } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: Equal("About #2"),
})
import { Like } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: Like("%out #%"),
})
import { Between } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
likes: Between(1, 10),
})
import { In } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: In(["About #2", "About #3"]),
})
import { IsNull } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: IsNull(),
})