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", "lastName"] });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", "photos", "videos"] });
userRepository.find({ relations: ["profile", "photos", "videos", "videos.video_attributes"] });join- joins needs to be performed for the entity. Extended version of "relations".
userRepository.find({
join: {
alias: "user",
leftJoinAndSelect: {
profile: "user.profile",
photo: "user.photos",
video: "user.videos"
}
}
});where- simple conditions by which entity should be queried.
Querying a column from an embedded entity should be done with respect to the hierarchy in which it was defined. Example:
Querying with OR operator:
will execute following query:
order- selection order.
withDeleted- include entities which have been soft deleted withsoftDeleteorsoftRemove, e.g. have their@DeleteDateColumncolumn set. By default, soft deleted entities are not included.
find methods which return multiple entities (find, findAndCount, findByIds) also accept following options:
skip- offset (paginated) from where entities should be taken.
take- limit (paginated) - max number of entities that should be taken.
** 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.'
cache- Enables or disables query result caching. See caching for more information and options.
lock- Enables locking mechanism for query. Can be used only infindOnemethod.lockis an object which can be defined as:or
for example:
pessimistic_partial_write and pessimistic_write_or_fail are supported only on Postgres and are equivalents of SELECT .. FOR UPDATE SKIP LOCKED and SELECT .. FOR UPDATE NOWAIT, accordingly.
Complete example of find options:
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:
Between
will execute following query:
In
will execute following query:
Any
will execute following query (Postgres notation):
IsNull
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 Not operator:
will execute following query:
Last updated
Was this helpful?