https://orkhan.gitbook.io/typeorm/docs/find-options
TypeORM은 많은 build in 연산자를 지원한다.
- Not
import { Not } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: Not("About #1"),
})
SELECT * FROM "post" WHERE "title" != 'About #1'
- LessThan
import { LessThan } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
likes: LessThan(10),
})
SELECT * FROM "post" WHERE "likes" < 10
- LessThanOrEqual
import { LessThanOrEqual } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
likes: LessThanOrEqual(10),
})
SELECT * FROM "post" WHERE "likes" <= 10
- MoreThan
import { MoreThan } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
likes: MoreThan(10),
})
SELECT * FROM "post" WHERE "likes" > 10
- MoreThanOrEqual
import { MoreThanOrEqual } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
likes: MoreThanOrEqual(10),
})
SELECT * FROM "post" WHERE "likes" >= 10
- Equal
import { Equal } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: Equal("About #2"),
})
SELECT * FROM "post" WHERE "title" = 'About #2'
- Like
import { Like } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: Like("%out #%"),
})
SELECT * FROM "post" WHERE "title" LIKE '%out #%'
- ILike
import { ILike } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: ILike("%out #%"),
})
SELECT * FROM "post" WHERE "title" ILIKE '%out #%'
- Between
import { Between } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
likes: Between(1, 10),
})
SELECT * FROM "post" WHERE "likes" BETWEEN 1 AND 10
- In
import { In } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: In(["About #2", "About #3"]),
})
SELECT * FROM "post" WHERE "title" IN ('About #2','About #3')
- IsNull
import { IsNull } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: IsNull(),
})
SELECT * FROM "post" WHERE "title" IS NULL
- Raw
import { Raw } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
likes: Raw("dislikes - 4"),
})
SELECT * FROM "post" WHERE "likes" = "dislikes" - 4
함수를 사용하여 비교 구분을 넣을 수도 있다.
import { Raw } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
currentDate: Raw((alias) => `${alias} > NOW()`),
})
SELECT * FROM "post" WHERE "currentDate" > NOW()
유저 입력을 그대로 Query로 받지 않도록 해야 한다. (취약점 문제)
대신 Query에 원하는 파라미터를 리스트로 제공할 수 있다.
import { Raw } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
currentDate: Raw((alias) => `${alias} > :date`, { date: "2020-10-06" }),
})
SELECT * FROM "post" WHERE "currentDate" > '2020-10-06'
배열인 입력을 받아야 한다면, 다음과 같이 처리할 수 있다.
import { Raw } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findby({
title: Raw((alias) => `${alias} IN (:...titles)`, {
titles: [
"Go To Statement Considered Harmful",
"Structured Programming",
],
}),
})
SELECT * FROM "post" WHERE "titles" IN ('Go To Statement Considered Harmful', 'Structured Programming')
Combining Advanced Options
not 연산자와 함께 사용할 수 있다.
import { Not, MoreThan, Equal } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
likes: Not(MoreThan(10)),
title: Not(Equal("About #2")),
})
SELECT * FROM "post" WHERE NOT("likes" > 10) AND NOT("title" = 'About #2')
'프로그래밍 > TypeORM' 카테고리의 다른 글
[TypeORM] Many-to-one / one-to-many relations (0) | 2023.09.04 |
---|---|
[TypeORM] One-to-one relations (0) | 2023.09.03 |
[TypeORM] Find Options - Basic Options (0) | 2023.09.02 |
[TypeORM] EntityManager API (0) | 2023.09.02 |
[TypeORM] Delete using Query Builder (0) | 2023.09.02 |
댓글