查找选项
基本选项
所有仓库和管理器的 .find*
方法接受一些特殊选项,您可以在不使用 QueryBuilder
的情况下使用这些选项查询所需的数据:
select
- 指示必须选择主对象的哪些属性
userRepository.find({
select: {
firstName: true,
lastName: true,
},
})
将执行以下查询:
SELECT "firstName", "lastName" FROM "user"
relations
- 需要与主实体一起加载的关系。也可以加载子关系(join
和leftJoinAndSelect
的简写)
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"
where
- 应该根据哪些简单条件查询实体。
userRepository.find({
where: {
firstName: "Timber",
lastName: "Saw",
},
})
将执行以下查询:
SELECT * FROM "user"
WHERE "firstName" = 'Timber' AND "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'
使用 OR 运算符进行查询:
userRepository.find({
where: [
{ firstName: "Timber", lastName: "Saw" },
{ firstName: "Stan", lastName: "Lee" },
],
})
将执行以下查询:
SELECT * FROM "user" WHERE ("firstName" = 'Timber' AND "lastName" = '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
- 包括已使用softDelete
或softRemove
软删除的实体,例如设置了@DeleteDateColumn
列的实体。默认情况下,软删除的实体不包括在内。
userRepository.find({
withDeleted: true,
})
返回多个实体的 find*
方法(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
和 take
应该一起使用
** 如果您正在使用带有 MSSQL 的 typeorm,并希望使用 take
或 limit
,您需要使用 order,否则您将收到以下错误:'Invalid usage of the option NEXT in the FETCH statement.'
userRepository.find({
order: {
columnName: "ASC",
},
skip: 0,
take: 10,
})
将执行以下查询:
SELECT * FROM "user"
ORDER BY "columnName" ASC
LIMIT 10 OFFSET 0
cache
- 启用或禁用查询结果缓存。有关更多信息和选项,请参阅 缓存。
userRepository.find({
cache: true,
})
lock
- 为查询启用锁定机制。只能在findOne
和findOneBy
方法中使用。lock
是一个可以定义为:
{ mode: "optimistic", version: number | Date }
或
{
mode: "pessimistic_read" |
"pessimistic_write" |
"dirty_read" |
/*
"pessimistic_partial_write" and "pessimistic_write_or_fail" are deprecated and
will be removed in a future version.
Use onLocked instead.
*/
"pessimistic_partial_write" |
"pessimistic_write_or_fail" |
"for_no_key_update" |
"for_key_share",
tables: string[],
onLocked: "nowait" | "skip_locked"
}
例如:
userRepository.findOne({
where: {
id: 1,
},
lock: { mode: "optimistic", version: 1 },
})
有关更多信息,请参阅 锁定模式
查找选项的完整示例:
userRepository.find({
select: {
firstName: true,
lastName: true,
},
relations: {
profile: true,
photos: true,
videos: true,
},
where: {
firstName: "Timber",
lastName: "Saw",
profile: {
userName: "tshaw",
},
},
order: {
name: "ASC",
id: "DESC",
},
skip: 5,
take: 10,
cache: true,
})
无参数查找:
userRepository.find()
将执行以下查询:
SELECT * FROM "user"
高级选项
TypeORM 提供了许多内置运算符,可用于创建更复杂的比较:
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')
Any
import { Any } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
title: Any(["About #2", "About #3"]),
})
将执行以下查询(Postgres 符号):
```sql
SELECT * FROM "post" WHERE "title" = ANY(['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
ArrayContains
import { ArrayContains } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
categories: ArrayContains(["TypeScript"]),
})
将执行以下查询:
SELECT * FROM "post" WHERE "categories" @> '{TypeScript}'
ArrayContainedBy
import { ArrayContainedBy } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
categories: ArrayContainedBy(["TypeScript"]),
})
将执行以下查询:
SELECT * FROM "post" WHERE "categories" <@ '{TypeScript}'
ArrayOverlap
import { ArrayOverlap } from "typeorm"
const loadedPosts = await dataSource.getRepository(Post).findBy({
categories: ArrayOverlap(["TypeScript"]),
})
将执行以下查询:
SELECT * FROM "post" WHERE "categories" && '{TypeScript}'
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()
如果您需要提供用户输入,您不应将用户输入直接包含在查询中,因为这可能会产生 SQL 注入漏洞。相反,您可以使用 Raw
函数的第二个参数提供一个参数列表以绑定到查询。
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'
如果您需要提供的用户输入是数组,则可以使用特殊表达式语法将它们绑定为 SQL 语句中的值列表:
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')
组合高级选项
您还可以将这些运算符与 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')