TypeORM custom repository: @EntityRepository and getCustomRepository stopped working
@EntityRepository is flagged as deprecatedShort answer
TypeORM 0.3 dropped the old way of making custom repositories. The @EntityRepository decorator, getCustomRepository and AbstractRepository are gone or deprecated. Build the custom repository with dataSource.getRepository(Entity).extend({...}), or in NestJS wrap that call in a provider. Inside a transaction, use manager.withRepository.
Fix it fast
1Find out which cause you have
Find every place that still uses the old API, so you know how many repositories you must rebuild.
grep -rn "EntityRepository\|getCustomRepository\|AbstractRepository" src/2Match what you see, then jump to its fix
- 1
'EntityRepository' is deprecated, or the class is never picked upA class decorated with @EntityRepository is still in the codeFix - 2
EntityManager's getCustomRepository used within transactions is deprecatedgetCustomRepository is still called inside a transactionFix - 3
AbstractRepository is deprecatedCode still extends AbstractRepositoryFix
- 1
What does the error look like?
@EntityRepository is flagged as deprecatedEntityManager's getCustomRepository ... is deprecated. Use withRepository method instead.
Cause 1 of 3
A class decorated with @EntityRepository is still in the code
1Check
The TypeORM 0.3.0 release notes say old ways of custom repository creation were dropped. A class that extends Repository and carries @EntityRepository belongs to that old way.
grep -rn "@EntityRepository" src/2Fix
Remove the decorator and build the repository from the data source with extend(). Methods you add go into the object you pass; inside them, this is the repository.
// user.repository.ts
import { dataSource } from './data-source';
import { User } from './user.entity';
export const UserRepository = dataSource.getRepository(User).extend({
findByName(firstName: string, lastName: string) {
return this.createQueryBuilder('user')
.where('user.firstName = :firstName', { firstName })
.andWhere('user.lastName = :lastName', { lastName })
.getMany();
},
});3Verify
Compile the project and call the new method once from a script or test.
npx tsc --noEmitConfirmed by typeorm.io
Cause 2 of 3
getCustomRepository is still called inside a transaction
1Check
A transaction has its own query runner and entity manager, so a repository created at module level does not run inside it. Search for getCustomRepository in transaction callbacks.
grep -rn "getCustomRepository" src/2Fix
Wrap the extended repository with the manager the transaction gives you.
await dataSource.transaction(async (manager) => {
const userRepository = manager.withRepository(UserRepository);
const timber = await userRepository.findByName('Timber', 'Saw');
});3Verify
Run the transaction and check that rolling it back also undoes the repository writes.
npm testConfirmed by typeorm.io
Cause 3 of 3
Code still extends AbstractRepository
1Check
The 0.3.0 release notes list AbstractRepository as deprecated, with the new custom repository style as the replacement.
grep -rn "extends AbstractRepository" src/2Fix
Move its methods into an extend() call on the entity repository, as in the first cause, and drop the base class.
export const PostRepository = dataSource.getRepository(Post).extend({
findPublished() {
return this.find({ where: { published: true } });
},
});3Verify
Compile again and run the tests that touch this repository.
npx tsc --noEmit && npm testConfirmed by github.com
How do I stop it coming back?
- Read the TypeORM release notes before raising the major or minor version, and search the code for each deprecated name.
- Keep one exported DataSource and build repositories from it, not from global helper functions.
- Run the compiler in CI so a deprecated or removed API fails the build.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
TypeORM warns that createConnection is deprecated
'createConnection' is deprecated.
TypeORM replaced Connection with DataSource.
2 causes, each with a fixBack end
TypeScript cannot find module '@nestjs/core' or its type declarations
error TS2307: Cannot find module '@nestjs/common' or its corresponding type declarations.
TS2307 means TypeScript searched node_modules and found neither the package nor its types.
3 causes, each with a fixBack end
Node.js in Docker Compose cannot reach MySQL on 127.0.0.1:3306
Error: connect ECONNREFUSED 127.0.0.1:3306
Inside a container, 127.0.0.1 is that container, not MySQL.
3 causes, each with a fixBack end
NestJS GraphQL cannot work out the output type of a field or query
Cannot determine a GraphQL output type for the "<name>". Make sure your class is decorated with an appropriate decorator.
In code-first mode Nest reads TypeScript types through decorators, and arrays, promises and unions disappear at runtime.
2 causes, each with a fixBack end
IntelliJ IDEA breakpoints are ignored in a NestJS app
No 'Debugger listening' line appears when the NestJS app starts
IntelliJ can only debug a NestJS app when the Node.js process runs with the inspector on and the IDE attaches to its port.
4 causes, each with a fixBack end
The nest command is not found in the terminal
bash: nest: command not found
The shell cannot find a nest executable on its PATH.
3 causes, each with a fix