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

userRepository.find({
    select: {
        firstName: true,
        lastName: true,
    },
})

will execute following query:

SELECT "firstName", "lastName" FROM "user"
  • relations - relations needs to be loaded with the main entity. Sub-relations can also be loaded (shorthand for join and leftJoinAndSelect)

userRepository.find({
    relations: {
        profile: true,
        photos: true,
        videos: true,
    },
})
userRepository.find({
    relations: {
        profile: true,
        photos: true,
        videos: {
            videoAttributes: true,
        },
    },
})

will execute following queries:

  • where - simple conditions by which entity should be queried.

will execute following query:

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

will execute following query:

Querying with OR operator:

will execute following query:

  • order - selection order.

will execute following query:

  • withDeleted - include entities which have been soft deleted with softDelete or softRemove, e.g. have their @DeleteDateColumn column set. By default, soft deleted entities are not included.

find* methods which return multiple entities (find, findBy, findAndCount, findAndCountBy) also accept following options:

  • skip - offset (paginated) from where entities should be taken.

  • take - limit (paginated) - max number of entities that should be taken.

will execute following query:

** skip and take should be used together

** 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.'

will execute following query:

  • cache - Enables or disables query result caching. See caching for more information and options.

  • lock - Enables locking mechanism for query. Can be used only in findOne and findOneBy methods. lock is an object which can be defined as:

or

for example:

See lock modes for more information

Complete example of find options:

Find without arguments:

will execute following query:

Advanced options

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

  • Not

will execute following query:

  • LessThan

will execute following query:

  • LessThanOrEqual

will execute following query:

  • MoreThan

will execute following query:

  • MoreThanOrEqual

will execute following query:

  • Equal

will execute following query:

  • Like

will execute following query:

  • ILike

will execute following query:

  • Between

will execute following query:

  • In

will execute following query:

  • Any

will execute following query (Postgres notation):

  • IsNull

will execute following query:

  • ArrayContains

will execute following query:

  • ArrayContainedBy

will execute following query:

  • ArrayOverlap

will execute following query:

  • Raw

will execute following query:

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.

will execute following query:

If you need to provide user input, you should not include the user input directly in your query as this may create a SQL injection vulnerability. Instead, you can use the second argument of the Raw function to provide a list of parameters to bind to the query.

will execute following query:

If you need to provide user input that is an array, you can bind them as a list of values in the SQL statement by using the special expression syntax:

will execute following query:

Combining Advanced Options

Also you can combine these operators with below:

  • Not

will execute following query:

  • Or

will execute following query:

  • And

will execute following query:

Last updated