# Find 选项

# 基础选项

所有存储库和管理器find方法都接受可用于查询所需数据的特殊选项,而无需使用QueryBuilder

  • select - 表示必须选择对象的哪些属性
userRepository.find({ select: ["firstName", "lastName"] });
1
  • relations - 关系需要加载主体。 也可以加载子关系(joinleftJoinAndSelect 的简写)
userRepository.find({ relations: ["profile", "photos", "videos"] });
userRepository.find({ relations: ["profile", "photos", "videos", "videos.video_attributes"] });
1
2
  • join - 需要为实体执行联接,扩展版对的"relations"。
userRepository.find({
    join: {
        alias: "user",
        leftJoinAndSelect: {
            profile: "user.profile",
            photo: "user.photos",
            video: "user.videos"
        }
    }
});
1
2
3
4
5
6
7
8
9
10
  • where -查询实体的简单条件。
userRepository.find({ where: { firstName: "Timber", lastName: "Saw" } });
1

查询嵌入实体列应该根据定义它的层次结构来完成。 例:

userRepository.find({ where: { name: { first: "Timber", last: "Saw" } } });
1

使用 OR 运算符查询:

userRepository.find({
    where: [{ firstName: "Timber", lastName: "Saw" }, { firstName: "Stan", lastName: "Lee" }]
});
1
2
3

将执行以下查询:

SELECT * FROM "user" WHERE ("firstName" = 'Timber' AND "lastName" = 'Saw') OR ("firstName" = 'Stan' AND "lastName" = 'Lee')
1
  • order - 选择排序
userRepository.find({
    order: {
        name: "ASC",
        id: "DESC"
    }
});
1
2
3
4
5
6

返回多个实体的find方法(findfindAndCountfindByIds),同时也接受以下选项:

  • skip - 偏移(分页)
userRepository.find({
    skip: 5
});
1
2
3
  • take - limit (分页) - 得到的最大实体数。
userRepository.find({
    take: 10
});
1
2
3

** 如果你正在使用带有 MSSQL 的 typeorm,并且想要使用takelimit,你必须正确使用 order,否则将会收到以下错误:'FETCH语句中NEXT选项的使用无效。'

userRepository.find({
    order: {
        columnName: "ASC"
    },
    skip: 0,
    take: 10
});
1
2
3
4
5
6
7
  • cache - 启用或禁用查询结果缓存。 有关更多信息和选项,请参见caching
userRepository.find({
    cache: true
});
1
2
3
  • lock - 启用锁查询。 只能在findOne方法中使用。 lock是一个对象,可以定义为:
{ mode: "optimistic", version: number|Date }
1

或者

{ mode: "pessimistic_read"|"pessimistic_write"|"dirty_read" }
1

例如:

userRepository.findOne(1, {
    lock: { mode: "optimistic", version: 1 }
})
1
2
3

find 选项的完整示例:

userRepository.find({
    select: ["firstName", "lastName"],
    relations: ["profile", "photos", "videos"],
    where: {
        firstName: "Timber",
        lastName: "Saw"
    },
    order: {
        name: "ASC",
        id: "DESC"
    },
    skip: 5,
    take: 10,
    cache: true
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 进阶选项

TypeORM 提供了许多内置运算符,可用于创建更复杂的查询:

  • Not
import { Not } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: Not("About #1")
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE "title" != 'About #1'
1
  • LessThan
import { LessThan } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: LessThan(10)
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE "likes" < 10
1
  • LessThanOrEqual
import { LessThanOrEqual } from "typeorm";
const loadedPosts = await connection.getRepository(Post).find({
    likes: LessThanOrEqual(10)
});
1
2
3
4

将执行以下查询:

SELECT * FROM "post" WHERE "likes" <= 10
1
  • MoreThan
import { MoreThan } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: MoreThan(10)
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE "likes" > 10
1
  • MoreThanOrEqual
import { MoreThanOrEqual } from "typeorm";
const loadedPosts = await connection.getRepository(Post).find({
    likes: MoreThanOrEqual(10)
});
1
2
3
4

将执行以下查询:

SELECT * FROM "post" WHERE "likes" >= 10
1
  • Equal
import { Equal } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: Equal("About #2")
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE "title" = 'About #2'
1
  • Like
import { Like } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: Like("%out #%")
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE "title" LIKE '%out #%'
1
  • ILike
import { ILike } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: ILike("%out #%")
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE "title" ILIKE '%out #%'
1
  • Between
import { Between } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: Between(1, 10)
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE "likes" BETWEEN 1 AND 10
1
  • In
import { In } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: In(["About #2", "About #3"])
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE "title" IN ('About #2','About #3')
1
  • Any
import { Any } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: Any(["About #2", "About #3"])
});
1
2
3
4
5

将执行以下查询: (Postgres notation):

SELECT * FROM "post" WHERE "title" = ANY(['About #2','About #3'])
1
  • IsNull
import { IsNull } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: IsNull()
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE "title" IS NULL
1
  • Raw
import { Raw } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: Raw("1 + likes = 4")
});
1
2
3
4
5

将执行以下查询:

SELECT * FROM "post" WHERE 1 + "likes" = 4
1

注意:注意Raw操作符。 它应该从提供的表达式执行纯 SQL,而不能包含用户输入,否则将导致 SQL 注入。

你还可以将这些运算符与Not运算符组合使用:

import { Not, MoreThan, Equal } from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    likes: Not(MoreThan(10)),
    title: Not(Equal("About #2"))
});
1
2
3
4
5
6

将执行以下查询:

SELECT * FROM "post" WHERE NOT("likes" > 10) AND NOT("title" = 'About #2')
1