• Find 选项
    • 基础选项
    • 进阶选项

    Find 选项

    基础选项

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

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

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

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

    使用 OR 运算符查询:

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

    将执行以下查询:

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

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

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

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

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

    例如:

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

    find 选项的完整示例:

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

    进阶选项

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

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

    将执行以下查询:

    1. SELECT * FROM "post" WHERE "title" != 'About #1'
    • LessThan
    1. import { LessThan } from "typeorm";
    2. const loadedPosts = await connection.getRepository(Post).find({
    3. likes: LessThan(10)
    4. });

    将执行以下查询:

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

    将执行以下查询:

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

    将执行以下查询:

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

    将执行以下查询:

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

    将执行以下查询:

    1. SELECT * FROM "post" WHERE "title" = 'About #2'
    • Like
    1. import { Like } from "typeorm";
    2. const loadedPosts = await connection.getRepository(Post).find({
    3. title: Like("%out #%")
    4. });

    将执行以下查询:

    1. SELECT * FROM "post" WHERE "title" LIKE '%out #%'
    • Between
    1. import { Between } from "typeorm";
    2. const loadedPosts = await connection.getRepository(Post).find({
    3. likes: Between(1, 10)
    4. });

    将执行以下查询:

    1. SELECT * FROM "post" WHERE "likes" BETWEEN 1 AND 10
    • In
    1. import { In } from "typeorm";
    2. const loadedPosts = await connection.getRepository(Post).find({
    3. title: In(["About #2", "About #3"])
    4. });

    将执行以下查询:

    1. SELECT * FROM "post" WHERE "title" IN ('About #2','About #3')
    • Any
    1. import { Any } from "typeorm";
    2. const loadedPosts = await connection.getRepository(Post).find({
    3. title: Any(["About #2", "About #3"])
    4. });

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

    1. SELECT * FROM "post" WHERE "title" = ANY(['About #2','About #3'])
    • IsNull
    1. import { IsNull } from "typeorm";
    2. const loadedPosts = await connection.getRepository(Post).find({
    3. title: IsNull()
    4. });

    将执行以下查询:

    1. SELECT * FROM "post" WHERE "title" IS NULL
    • Raw
    1. import { Raw } from "typeorm";
    2. const loadedPosts = await connection.getRepository(Post).find({
    3. likes: Raw("1 + likes = 4")
    4. });

    将执行以下查询:

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

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

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

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

    将执行以下查询:

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