Find Options

Basic options

All repository and manager find methods accept special options you can use to query data you need without using QueryBuilder:

  • select - indicates which properties of the main object must be selected
  1. userRepository.find({ select: ["firstName", "lastName"] });
  • relations - relations needs to be loaded with the main entity. Sub-relations can also be loaded (shorthand for join and leftJoinAndSelect)
  1. userRepository.find({ relations: ["profile", "photos", "videos"] });
  2. userRepository.find({ relations: ["profile", "photos", "videos", "videos.video_attributes"] });
  • join - joins needs to be performed for the entity. Extended version of “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 - simple conditions by which entity should be queried.
  1. userRepository.find({ where: { firstName: "Timber", lastName: "Saw" } });

Querying a column from an embedded entity should be done with respect to the hierarchy in which it was defined. Example:

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

Querying with OR operator:

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

will execute following query:

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

find methods which return multiple entities (find, findAndCount, findByIds) also accept following options:

  • skip - offset (paginated) from where entities should be taken.
  1. userRepository.find({
  2. skip: 5
  3. });
  • take - limit (paginated) - max number of entities that should be taken.
  1. userRepository.find({
  2. take: 10
  3. });

** If you are using typeorm with MSSQL, and want to use take or limit, you need to use order as well or you will receive the following error: 'Invalid usage of the option NEXT in the FETCH statement.'

  1. userRepository.find({
  2. order: {
  3. columnName: 'ASC'
  4. },
  5. skip: 0,
  6. take: 10
  7. })
  • cache - Enables or disables query result caching. See caching for more information and options.
  1. userRepository.find({
  2. cache: true
  3. })
  • lock - Enables locking mechanism for query. Can be used only in findOne method. lock is an object which can be defined as:
    1. { mode: "optimistic", version: number|Date }
    or
    1. { mode: "pessimistic_read"|"pessimistic_write"|"dirty_read" }

for example:

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

Complete example of find options:

  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. });

Advanced options

TypeORM provides a lot of built-in operators that can be used to create more complex comparisons:

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

will execute following query:

  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. });

will execute following query:

  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. });

will execute following query:

  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. });

will execute following query:

  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. });

will execute following query:

  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. });

will execute following query:

  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. });

will execute following query:

  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. });

will execute following query:

  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. });

will execute following query:

  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. });

will execute following query (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. });

will execute following query:

  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("dislikes - 4")
  4. });

will execute following query:

  1. SELECT * FROM "post" WHERE "likes" = "dislikes" - 4

In the simplest case, a raw query is inserted immediately after the equal symbol. But you can also completely rewrite the comparison logic using the function.

  1. import {Raw} from "typeorm";
  2. const loadedPosts = await connection.getRepository(Post).find({
  3. currentDate: Raw(alias =>`${alias} > NOW()`)
  4. });

will execute following query:

  1. SELECT * FROM "post" WHERE "currentDate" > NOW()

Note: beware with Raw operator. It executes pure SQL from supplied expression and should not contain a user input, otherwise it will lead to SQL-injection.

Also you can combine these operators with Not operator:

  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. });

will execute following query:

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