Custom repositories
You can create a custom repository which should contain methods to work with your database. For example, let's say we want to have a method called findByName(firstName: string, lastName: string)
which will search for users by a given first and last names. The best place for this method is a Repository
, so we could call it like userRepository.findByName(...)
. You can achieve this using custom repositories.
There are several ways how custom repositories can be created.
How to create custom repository
It's common practice assigning a repository instance to a globally exported variable, and use this variable across your app, for example:
In order to extend UserRepository
functionality you can use .extend
method of Repository
class:
Using custom repositories in transactions
Transactions have their own scope of execution: they have their own query runner, entity manager and repository instances. That's why using global (data source's) entity manager and repositories won't work in transactions. In order to execute queries properly in scope of transaction you must use provided entity manager and it's getRepository
method. In order to use custom repositories within transaction, you must use withRepository
method of the provided entity manager instance:
Last updated