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.
userRepository.find({ withDeleted:true,})
find* methods which return multiple entities (find, findBy, findAndCount, findAndCountBy) also accept following options:
skip - offset (paginated) from where entities should be taken.
userRepository.find({ skip:5,})
SELECT*FROM"user"OFFSET 5
take - limit (paginated) - max number of entities that should be taken.
userRepository.find({ take:10,})
will execute following query:
SELECT*FROM"user"LIMIT10
** 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.'
cache - Enables or disables query result caching. See caching for more information and options.
userRepository.find({ cache:true,})
lock - Enables locking mechanism for query. Can be used only in findOne and findOneBy methods. lock is an object which can be defined as:
{ mode:"optimistic", version: number | Date }
or
{ mode:"pessimistic_read"|"pessimistic_write"|"dirty_read"|/* "pessimistic_partial_write" and "pessimistic_write_or_fail" are deprecated and will be removed in a future version. Use onLocked instead. */"pessimistic_partial_write"|"pessimistic_write_or_fail"|"for_no_key_update"|"for_key_share", tables: string[], onLocked:"nowait"|"skip_locked"}
import { Raw } from"typeorm"constloadedPosts=awaitdataSource.getRepository(Post).findBy({ likes:Raw("dislikes - 4"),})
will execute following query:
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.
import { Raw } from"typeorm"constloadedPosts=awaitdataSource.getRepository(Post).findBy({ currentDate:Raw((alias) =>`${alias} > NOW()`),})
will execute following query:
SELECT*FROM"post"WHERE"currentDate">NOW()
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.
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:
import { Raw } from"typeorm"constloadedPosts=awaitdataSource.getRepository(Post).findby({ title:Raw((alias) =>`${alias} IN (:...titles)`, { titles: ["Go To Statement Considered Harmful","Structured Programming", ], }),})
will execute following query:
SELECT*FROM"post"WHERE"titles"IN ('Go To Statement Considered Harmful', 'Structured Programming')